기본 콘텐츠로 건너뛰기

힘내라 청춘. 20171229

vi-editor-new-line

vi-editor-new-line VI 에디터에서 특정문자 개행문자로 수정 VI 에디터에서 "문자 찾아 바꾸기"를 위해 %s 를 많이 사용하게 된다. 개행 문자 제거 혹은 수정 시 변경할 대상으로 \n 를 입력하면 수정이 가능하지만, 특정 문자를 개행문자로 수정하기 위해서 \n 를 사용하면 ^@ 가 삽입되는 것을 확인할 수 있다. 그럴 때는 \n 대신 \r 을 사용하면 개행문자 삽입이 가능하다. Example ; 을 ;[개행 문자] 로 수정 :%s/;/;\r/g

Insecure-programming-abo3

Insecure-programming-abo3 Insecure Programming by example Advanced Buffer Overflow 3 int main ( int argv , char * * argc ) { extern system , puts ; void ( * fn ) ( char * ) = ( void ( * ) ( char * ) ) & system ; char buf [ 256 ] ; fn = ( void ( * ) ( char * ) ) & puts ; strcpy ( buf , argc [ 1 ] ) ; fn ( argc [ 2 ] ) ; exit ( 1 ) ; } 이전 문제들에 비해 복잡성을 가진 것을 확인할 수 있다. 하나씩 살펴보면 extern 키워드를 이용하여 system 함수과 puts 함수를 포인터 변수에 선언하는 내용과 두 번째 인자 값( argc[2] )를 puts 함수의 인자로 설정하여 호출하는 것을 볼 수 있다. objdump 를 이용하여 어셈블리어를 살펴보자. 08048414 <main>: 8048414: 55 push ebp 8048415: 89 e5 mov ebp,esp 8048417: 81 ec 28 01 00 00 sub esp,0x128 804841d: 83 e4 f0 and esp,0xfffffff0 8048420: b8 00 00 00 00 mov eax,0x0 8048425: 29 c4 sub esp,eax 8048427: c7 45 f4 fc 82 04 08 mov DWORD PTR [ebp-12],0x80482fc 804842e:

Insecure-programming-abo2

Insecure-programming-abo2 Insecure Programming by example Advanced Buffer Overflow 2 int main ( int argv , char * * argc ) { char buf [ 256 ] ; strcpy ( buf , argc [ 1 ] ) ; exit ( 1 ) ; 지난번 Advanced Buffer Overflow 1 문제와는 다르게 exit() 함수가 추가된 것을 볼 수 있다. 이번 문제는 x86 시스템에서 exit() 함수 호출과 익스플로잇에 대한 영향을 살펴보는 것이 목적이다. objdump 를 이용하여 어셈블리어를 살펴보면 한 가지 특징을 볼 수 있다. # objdump -d -M intel ./abo2 | grep "<main>" -A 16 080483b4 < main > : 80483b4: 55 push ebp 80483b5: 89 e5 mov ebp,esp 80483b7: 81 ec 18 01 00 00 sub esp,0x118 80483bd: 83 e4 f0 and esp,0xfffffff0 80483c0: b8 00 00 00 00 mov eax,0x0 80483c5: 29 c4 sub esp,eax 80483c7: 8b 45 0c mov eax,DWORD PTR [ ebp+12 ] 80483ca: 83 c0 04 add eax,0x4 80483cd: 8b 00 m

Insecure-programming-abo1

Insecure-programming-abo1 Insecure Programming by example Advanced Buffer Overflow 1 int main ( int argv , char * * argc ) { char buf [ 256 ] ; strcpy ( buf , argc [ 1 ] ) ; } Advanced Buffer Overflow 첫 번째 문제는 사용자 인자 값 검증 없이 버퍼에 strcpy() 함수로 복사하여 발생되는 오버플로우 예이다. 먼저 objdump 를 이용하여 어셈블리어를 살펴보자. # objdump -d -M intel ./abo1 | grep -A 16 "<main>" 08048374 <main>: 8048374: 55 push ebp 8048375: 89 e5 mov ebp,esp 8048377: 81 ec 18 01 00 00 sub esp,0x118 804837d: 83 e4 f0 and esp,0xfffffff0 8048380: b8 00 00 00 00 mov eax,0x0 8048385: 29 c4 sub esp,eax 8048387: 8b 45 0c mov eax,DWORD PTR [ebp+12] 804838a: 83 c0 04 add eax,0x4 804838d: 8b 00 mov eax,DWORD PTR [eax] 804838f: 89 44 24 04 mov

Insecure-programming-stack5

Insecure-programming-stack5 Insecure Programming by example Stack 5 이번 문제에서는 조건절 안에 “you win!” 문구 대신 "you loose!"가 있는 것을 볼 수 있다. # include <stdio.h> int main ( ) { int cookie ; char buf [ 80 ] ; printf ( "buf: %08x cookie: %08xn" , & buf , & cookie ) ; gets ( buf ) ; if ( cookie == 0x000d0a00 ) printf ( "you loose!\n" ) ; } 따라서 이번 문제를 해결하기 위해서는 임의로 작성한 별도의 코드를 프로그램에서 호출하도록 하여 “you win!” 구문 출력을 이끌어 내야한다. "you win!"을 출력하고 종료하는 간단한 어셈블리어 코드를 작성한다. section .text global _start _start: jmp short ender starter: xor eax, eax xor ebx, ebx xor ecx, ecx xor edx, edx mov al, 4 mov dbl, 91 pop ecx mov bdl, 14 int 0x80 xor eax, eax inc eaxal dec bl int 0x80 ender: call starter db 'you win!\n' 작성이 완료되었다면 nasm 을 이용하여 오브젝트 파일을 생성한 후 ld 명령어로 실행 파일을 만들어 제대로 동작하는 지 확인해보하고 objdump 를 이용하여 코드 덤프를 만들자. # nasm -f elf -o stack54_sh_code.

Hackerrank_security-tutorial-functions2

Hackerrank_security-tutorial-functions2 Hackerrank - Security Security Functions 2 Problem Security Functions 2 또한 Security Concept 문제 시작에 앞서 워밍업을 위한 문제이다. 제곱 연산한 결과를 반환하는 함수를 작성하면 된다. 문제 보러 가기 Solution # include <math.h> /* * Complete the function below. */ int function ( int x ) { return pow ( x , 2 ) ; }