Here is my next solution for HITB CTF 2009 Daemon1. Similar to daemon 6, the flag is the content of errorcode.txt file located in the same directory with daemon’s binary.
home suto # netstat -tulpan Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:4444 0.0.0.0:* LISTEN 6174/daemon1
So you can see it listens on port 4444. Next I tried to find where the daemon processes my input.
.text:080494F1 push eax .text:080494F2 call _recv .text:080494F7 add esp, 10h .text:080494FA cmp eax, 0 .text:080494FD jle loc_80495D2 .text:08049503 push esi .text:08049504 push eax .text:08049505 lea esi, [ebp-538h] .text:0804950B push esi .text:0804950C mov ecx, [ebp-548h] .text:08049512 push ecx .text:08049513 call sub_804A2B0 .text:08049518 mov eax, offset aIcvykbmukcrwdp ; "iCvYkBMuKcrwDPkAqmCFgOKVeV34" .text:0804951D mov ecx, 1Ch .text:08049522 cld .text:08049523 mov esi, [ebp-560h] .text:08049529 mov edi, eax .text:0804952B repe cmpsb .text:0804952D setnbe dl .text:08049530 setb al .text:08049533 add esp, 10h .text:08049536 cmp dl, al .text:08049538 jnz loc_80495FF .text:0804953E call sub_8048F10 .text:08049543 push 0 .text:08049545 sub esp, 8 .text:08049548 push offset s .text:0804954D call _strlen .text:08049552 add esp, 0Ch .text:08049555 push eax .text:08049556 push offset s .text:0804955B .text:0804955B loc_804955B: ; CODE XREF: .text:08049608j .text:0804955B mov edx, [ebp-548h] .text:08049561 push edx .text:08049562 call _send
And here is what sub_8048F10 does:
lea edi, [ebp+var_40] mov esi, offset unk_80553D2 mov ecx, edx rep movsd mov ax, ds:word_80553EA mov [edi], ax push (offset aSocketError+0Bh) ; modes push offset filename ; "/home/d1/errorcode.txt" call _fopen <snip>
The code compares “iCvYkBMuKcrwDPkAqmCFgOKVeV34” with the input string. If it’s matched, the encrypted content of errorcode.txt will be returned.
home suto #nc localhost 4444 iCvYkBMuKcrwDPkAqmCFgOKVeV34 ddddddddddPfddddfdssqpfdddddddddhfh
“ddddddddddPfddddfdssqpfdddddddddhfh” is the return data. It’s the encrypted content of errorcode.txt (which is “1″ in this case).
After few hours trying to reverse the binary, I got stuck with the encoding algorithm so I tried to analysis the output data instead.
Input: 1
Ouput: ddddddddddPfddddfdssqpfdddddddddhfh
Input: 2
Output: ddddddddddPfdddddfdssqpfhfh
Input: 3
Output: ddddddddddPfdddddfdssqpfdhfh
Input: 4
Output: ddddddddddPfdddddfdssqpfddhfh
==>Output string begins with ddddddddddPfdddddfdssqpf and ends with hfh, number 1 is the special case.
9
ddddddddddPfdddddfdssqpfdddddddhfh
Next, we test with 2 numbers:
24
ddddddddddPfdddddfdssqpfhddhfh
3 numbers:
247
ddddddddddPfdddddfdssqpfhddhdddhfh
We can see that the string with red color is the same as the output for 24, and the green part is addition part for 7, so I guess h is character to begin a new number, let’s see with 6 numbers:
247398
ddddddddddPfdddddfdssqpfhddhdddhqqqqhddddddhqhfh
Now the algorithm is more clear :), the length of input number is the number of ‘h’ in the encoded data + 1 (we don’t count the last ‘hfh’). But how about q and d?
From 247398:
ddddddddddPfdddddfdssqpfhddhdddhqqqqhddddddhqhfh
4 is hdd
7 is hddd
3 is hqqqq
9 is hdddddd
8 is hq
Yeah! when the next number is increased, it uses a d for +1 (7 = 4 + 3 = hddd).
q is used for decrease (-1).
35896742
ddddddddddPfdddddfdssqp fd[3] hdd[5] hddd[8] hd[9] hqqq[6] hd[7] hqqq[4] hqq[2]hfh
Why 3? You answer yourself !
Now we come back to special cases for number 1 and 0
358967421
ddddddddddPfdddddfddddfdsssqpfdhddhdddhdhqqqhdhqqqhqqhfdddddddddhfh
Here is output for 35896742
ddddddddddPfdddddfdssqpfdhddhdddhdhqqqhdhqqqhqqhfh
The different parts are marked with Red color.
Put 1 in the middle:
3589617421
ddddddddddPfdddddfddddfdsssqpfdhddhdddhdhqqqhfdddddddddhsdhqqqhqqhfhfh
358967421
ddddddddddPfdddddfddddfdsssqpfdhddhdddhdhqqqhdhqqqhqqhfdddddddddhfh
35896742
ddddddddddPfdddddfdssqpfdhddhdddhdhqqqhdhqqqhqqhfh
So the output will be fdddddddddh for number 1. If 1 is in the middle, it will be dddfds.
And another notes is hsd , one “d” character because it is calculated from the number before “1″ – 6- and increases it to -7-.
Another test:
4668981445134
ddddddddddPfdddddfddddfdsssqpfdd(4)hdd(6)h(6)hdd(8)hd(9)hq(8)hfddddddddd(1)hsqqqq(4)h(4)
hd(5) hf(1)hs qq(3) hd(4) hffh
Now replace the number 1 with 0 from previous input:
ddddddddddPfdddddfddddfdsssqpfdd(4)hdd(6)h(6)hdd(8)hd(9)hq(8)hfdddddddd(0)hsqqqq(4)h(4)hd(5)
hf(0)hsqq(3) hd(4)hffh
We see 0 is quite similar to 1 with one ‘d’ less.
Now it’s just a simple task to decode the return content of errorcode.txt (flag) from the daemon.
And it’s all about daemon1 in HITB CTF 2009!