문제에 접속해보겠습니다.


소스 코드를 읽어보면 rand 함수를 random이라는 변수에 초기화 시켜줍니다.

사용자에게 입력을 받고 그 값을 random과 XOR 했을때 0xdeadbeef가 되면 flag파일을 읽도록 합니다.


key ^ random = 0xdeadbeef 인데 식을 바꾸면

key = random ^ 0xdeadbeef 로 key를 알아낼 수 있습니다. rand()함수에 seed값을 주지 않으면 항상 같은 값이 나옵니다.(랜덤이라고 할 수 없겠죠.)


gdb로 random에 들어간 값을 찾아보겠습니다.


브레이크 포인트를 main+43에 걸고 파일을 실행하여 rbp-0x4에 들어있는 값을 보면 0x6b8b4567 입니다.

이 값과 0xdeadbeef를 XOR 연산하여 key 값으로 입력하면 flag파일을 열 수 있습니다.

0x6b8b4567 ^ 0xdeadbeef = 0xb526fb88 이군요 이를 10진수로 변환하면

=> 3039230856 입니다.




(추가)다른 풀이가 존재하여 적습니다.


ltrace 라는 툴을 이용해 rand값을 찾을 수 있습니다.


rand 함수의 라이브러리를 호출합니다. 뒤의 0x6b8b4567이라는 random 값이 있습니다.




'Wargame > pwnable.kr' 카테고리의 다른 글

[pwnable.kr] mistake 1p  (0) 2016.08.19
[pwnable.kr] uaf 8p  (0) 2016.08.15
[pwnable.kr] passcode 10p  (0) 2016.07.29
[pwnable.kr 2번] collision 3pt  (0) 2016.07.27
[pwanble.kr 1번] fd 1pt  (0) 2016.07.27

이번 문제는 "컴파일러 경고를 누가 신경쓸까?" 라는 내용의 문제네요..

컴파일러 경고가 힌트인가봅니다.


소스코드를 자세히 살펴보면 scanf에 &가 없습니다. 여기서 compiler warning이 떴겠네요. 저 부분에서 문제를 해결 해보도록 하겠습니다.


하라는대로 했는데 seg fault에러가 뜹니다.

먼저 scanf 사용시 대상 변수가 char로 선언되어 있는 경우 초기 값은 주소와 동일하기 때문에 ‘&’ 없이 그냥 사용해도 됩니다. 반면 int로 선언되어 있는 경우 ‘&’ 없이 사용하면 변수자체가 주소가 되며 그 주소에 입력한 값이 들어갑니다.

그리고 리눅스에서 fflush의 인자로 표준 입력이 들어 오면 아무런 동작을 하지 않는다고 합니다.

즉, 리눅스에서는 fflush에 의해 ‘\n’이 버퍼에서 비워지지 않기 때문에 다음과 같이 프로그램을 실행하면 passcode1=338150, passcode2=’\n’이 됩니다.

welcome() 함수의 name은 ebp-0x70에 위치합니다. 100글자를 입력할 수 있습니다.


login()함수의 passcode1은  $ebp-0x10에 위치합니다. 0x70-0x10=0x60=96 이므로 총 4바이트를 조작할 수 있습니다. 

따라서 fflsuh의 GOT를 조작하여 system(“/bin/cat flag”)로 이동시키면 문제가 풀립니다.

("A"*96+exit@got) 값으로 name을 채우고 passcode에 system(“/bin/cat flag”) 주소를 주면 됩니다.


먼저 exit 함수의 주소는 readelf로 확인합니다.


exit => 0x0804a018


system("/bin/cat flag")의 주소는 gdb로 확인합니다.

=> 0x080485e3

이것을 10진수로 변환해서 입력해줍니다. 

그리고 seg fault가 발생하는것을 막기위해 passcode2에 문자를 입력하면 됩니다.


(python -c 'print "A"*96+"\x18\xa0\x04\x08"+"134514147\n"+"a\n"';cat) | ./passcode


flag가 나왔습니다. 

정말 어렵네요..


'Wargame > pwnable.kr' 카테고리의 다른 글

[pwnable.kr] mistake 1p  (0) 2016.08.19
[pwnable.kr] uaf 8p  (0) 2016.08.15
[pnwable.kr] random 1pt  (0) 2016.07.29
[pwnable.kr 2번] collision 3pt  (0) 2016.07.27
[pwanble.kr 1번] fd 1pt  (0) 2016.07.27

strings는 바이너리 파일에서 문자열을 추출하기 위한 툴입니다.


grep을 이용하여 원하는 문자열을 빠르게 확인할 수 있습니다.


ex)

 SSo@ubuntu: ~ $ stirngs [파일명] | grep [검색할 문자열]


1. 문자열의 위치 출력

-tx, -td, to 옵션을 사용하면 각각 16진수, 10진수, 8진수로 문자열의 위치를 출력함


-t 옵션 예

SSo@ubuntu: ~ $ stirngs -tx [파일명] | head -5

SSo@ubuntu: ~ $ stirngs -td [파일명] | head -5

SSo@ubuntu: ~ $ stirngs -to [파일명] | head -5



$man strings

NAME

       strings - find the printable strings in a object, or other binary, file


SYNOPSIS

       strings [ - ] [ -a ] [ -o ] [ -t format ] [ -number ] [ -n number ] [--] [file ...]


DESCRIPTION

       Strings  looks  for  ASCII  strings  in  a binary file or standard input.  Strings is useful for identifying random

       object files and many other things.  A string is any sequence of 4 (the default) or more printing characters ending

       with a newline or a null.  Unless the - flag is given, strings looks in all sections of the object files except the

       (__TEXT,__text) section.  If no files are specified standard input is read.


       The file arguments may be of the form libx.a(foo.o), to request information about only that object file and not the

       entire library.   (Typically this argument must be quoted, ``libx.a(foo.o)'', to get it past the shell.)


       The options to strings(1) are:


       -a     This  option  causes  strings  to  look  for  strings  in  all  sections  of  the object file (including the

              (__TEXT,__text) section.


       -      This option causes strings to look for strings in all bytes of the files (the default for non-object files).


       --     This option causes strings to treat all the following arguments as files.


       -o     Preceded each string by its offset in the file (in decimal).


       -t format

              Write  each string preceded by its byte offset from the start of the file.  The format shall be dependent on

              the single character used as the format option-argument:


       d      The offset shall be written in decimal.


       o      The offset shall be written in octal.


       x      The offset shall be written in hexadecimal.


       -number

              The decimal number is used as the minimum string length rather than the default of 4.


       -n number

              Specify the minimum string length, where the number argument is a  positive  decimal  integer.  The  default

              shall be 4.


       -arch arch_type

              Specifies the architecture, arch_type, of the file for strings(1) to operate on when the file is a universal

              file.  (See arch(3) for the currently know arch_types.)  The arch_type can be "all" to operate on all archi-

              tectures in the file.


SEE ALSO

       od(1)


BUGS

       The algorithm for identifying strings is extremely primitive.

'OS > linux' 카테고리의 다른 글

/proc/self/cwd  (0) 2019.05.09
프로세스에서 사용중인 파일 디스크립터 찾기  (0) 2018.07.20
리눅스 넘버링?  (0) 2018.03.27
리눅스 세션 연결 시 history 자동 삭제하기  (0) 2018.03.20
vim 화면 스크롤  (0) 2018.02.15

+ Recent posts