О том, что осталось за кадром

О том, что осталось за кадром

В жизни каждого "правильного" исследователя всегда есть момент, когда ты берешься за что-то, что вероятно тебе нахрен не нужно практически, но крайне интересно с образовательной точки зрения. Интересно решать задачи для расширения кругозора и повышения собственной квалификации, особенно при наличии придурковатого состояния одержимости какой-то фантастической идеей. В моем случае одной из главных одержимостей в 2002 году было желание сделать то, чего еще не существовало, а если и существовало, то работало не так как я хотел, и конечно же было сделано не мной (damn it). Эта же одержимость сподвигла на создание интерцептера, но данный пост о совсем другом проекте. В то время я желал освоить ассемблер, т.к. это язык настоящих гуру и на тот момент идея о том, что настоящий хакер должен знать ассемблер, сидела в моем сознании довольно глубоко. Слепая замена jz на jnz,
сидя в softice конечно давала некие результаты на простых приложеинях, но хотелось большего. Ничего лучше, чем написание собственного дизассемблера, с целью разобраться в этой компьютерной магии, я не придумал. Опять же, дизассемблер дизассемблеру рознь. Простой листинг инструкций меня не устраивал, я хотел получить на выходе исходник, который можно было бы тут же собрать и получить рабочий бинарник, идентичный оригинальному. Не буду вдаваться в такие специфичные проблемы как отделение данных от кода и т.д., а просто покажу что в итоге получилось.

Демо 1 - дизасссемблирование моего же порт-сканера на асме. Все символы (имена переменных и подфункций) присутствуют в эльфе.

/*
*|--------------------------------------
*|0x4553_Reconstructor
*| ELF format detected
*| Start disassembling of [test]
*| Entry point is.........[8048074]
*| .text segment size.....[564]
*| .data segment size.....[0]
*| .data segment addr.....[80492a8]
*| .symtab found
*| .strtab found
*|--------------------------------------
*/

/* Reading constants values...(if presents) */
STDOUT=1
AF_INET=2
SOCK_STREAM=1
IPPROTO_IP=0
NULL=0
SYS_exit=1
SYS_read=3
SYS_write=4
SYS_socketcall=102
SYS_socket=1
SYS_connect=3
sys=128
SIZEOF_SA=16
SIN_FAMILY=-16
SIN_PORT=-14
SIN_ADDR=-12
SIN_ZERO0=-8
SIN_ZERO1=-4
SA=-16
ARGS=-32
SOCK=-32
ARG0=-32
ARG1=-28
ARG2=-24
ARG3=-20
SOCK_BUF=-544
PORT=1
END_PORT=100
X=-16
counter=-20
uselen=31
__bss_start=134517416
_edata=134517416
_end=134517440

/* Reading objects ...(if presents) */

/* 80492a8: */.bss /*section*/
/* 80492a8: */.lcomm ip_addr,16
/* 80492b8: */.lcomm this_digit,4
/* 80492bc: */.lcomm ip_number,4

/* 8048074: */.text /*section*/
.globl _start
/* 8048074: */_start:
/* 8048074:5b */pop %ebx
/* 8048075:83 fb 01 */cmp $0x1,%ebx
/* 8048078:0f 84 aa 01 00 00 */jz usage
/* 804807e:5b */pop %ebx
/* 804807f:5b */pop %ebx
/* 8048080:85 db */test %ebx,%ebx
/* 8048082:0f 84 a3 00 00 00 */jz done_cmd
/* 8048088:8a 03 */movb (%ebx),%al
/* 804808a:43 */inc %ebx
/* 804808b:3c 2d */cmp $0x2d,%al
/* 804808d:0f 85 95 01 00 00 */jnz usage
/* 8048093:8a 03 */movb (%ebx),%al
/* 8048095:3c 74 */cmp $0x74,%al
/* 8048097:0f 85 8b 01 00 00 */jnz usage
/* 804809d:43 */inc %ebx
/* 804809e:43 */inc %ebx
/* 804809f:8d 3d a8 92 04 08 */leal ip_addr,%edi
/* 80480a5:b0 2e */movb $0x2e,%al
/* 80480a7:aa */stosb

/* 80480a8: */nextchar:
/* 80480a8:8a 03 */movb (%ebx),%al
/* 80480aa:aa */stosb
/* 80480ab:84 c0 */test %al,%al
/* 80480ad:74 03 */jz breakchar
/* 80480af:43 */inc %ebx
/* 80480b0:e2 f6 */loop nextchar

/* 80480b2: */breakchar:
/* 80480b2:4f */dec %edi
/* 80480b3:4f */dec %edi
/* 80480b4:89 fe */mov %edi,%esi
/* 80480b6:b9 04 00 00 00 */mov $SYS_write,%ecx
/* 80480bb:c7 05 bc 92 04 08 00 00 00 00 */movl $0x0000,ip_number
/* 80480c5:fd */std

/* 80480c6: */next_ip_byte:
/* 80480c6:c7 05 b8 92 04 08 00 00 00 00 */movl $0x0000,this_digit
/* 80480d0:ac */lodsb
/* 80480d1:3c 2e */cmp $0x2e,%al
/* 80480d3:75 01 */jnz not_dot
/* 80480d5:ac */lodsb

/* 80480d6: */not_dot:
/* 80480d6:2c 30 */sub $0x30,%al
/* 80480d8:00 05 b8 92 04 08 */add %al, this_digit
/* 80480de:ac */lodsb
/* 80480df:3c 2e */cmp $0x2e,%al
/* 80480e1:74 2f */jz dot
/* 80480e3:2c 30 */sub $0x30,%al
/* 80480e5:89 c2 */mov %eax,%edx
/* 80480e7:d1 e0 */shl $1,%eax
/* 80480e9:c1 e2 03 */shl $0x3,%edx
/* 80480ec:00 d0 */add %dl,%al
/* 80480ee:00 05 b8 92 04 08 */add %al, this_digit
/* 80480f4:ac */lodsb
/* 80480f5:3c 2e */cmp $0x2e,%al
/* 80480f7:74 19 */jz dot
/* 80480f9:2c 30 */sub $0x30,%al
/* 80480fb:89 c3 */mov %eax,%ebx
/* 80480fd:89 c2 */mov %eax,%edx
/* 80480ff:c1 e0 02 */shl $0x2,%eax
/* 8048102:c1 e3 05 */shl $0x5,%ebx
/* 8048105:c1 e2 06 */shl $0x6,%edx
/* 8048108:00 d8 */add %bl,%al
/* 804810a:00 d0 */add %dl,%al
/* 804810c:00 05 b8 92 04 08 */add %al, this_digit

/* 8048112: */dot:
/* 8048112:89 c8 */mov %ecx,%eax
/* 8048114:49 */dec %ecx
/* 8048115:c1 e1 03 */shl $0x3,%ecx
/* 8048118:8b 15 b8 92 04 08 */mov this_digit, %edx
/* 804811e:d3 e2 */shl %cl,%edx
/* 8048120:01 15 bc 92 04 08 */add %edx, ip_number
/* 8048126:89 c1 */mov %eax,%ecx
/* 8048128:e2 9c */loop next_ip_byte
/* 804812a:fc */cld

/* 804812b: */done_cmd:
/* 804812b:55 */push %ebp
/* 804812c:89 e5 */mov %esp,%ebp
/* 804812e:bf 01 00 00 00 */mov $STDOUT,%edi

/* 8048133: */loop:
/* 8048133:89 f8 */mov %edi,%eax
/* 8048135:66 c1 c0 08 */rol $0x8,%ax
/* 8048139:c7 45 f0 02 00 00 00 */movl $0x00000002,0xfffffff0(%ebp)
/* 8048140:89 45 f2 */movl %eax,0xfffffff2(%ebp)
/* 8048143:a1 bc 92 04 08 */mov ip_number,%eax
/* 8048148:89 45 f4 */movl %eax,0xfffffff4(%ebp)
/* 804814b:c7 45 f8 00 00 00 00 */movl $0x00000000,0xfffffff8(%ebp)
/* 8048152:c7 45 fc 00 00 00 00 */movl $0x00000000,0xfffffffc(%ebp)
/* 8048159:b8 66 00 00 00 */mov $SYS_socketcall,%eax
/* 804815e:bb 01 00 00 00 */mov $STDOUT,%ebx
/* 8048163:c7 45 e0 02 00 00 00 */movl $0x00000002,0xffffffe0(%ebp)
/* 804816a:c7 45 e4 01 00 00 00 */movl $0x00000001,0xffffffe4(%ebp)
/* 8048171:c7 45 e8 00 00 00 00 */movl $0x00000000,0xffffffe8(%ebp)
/* 8048178:8d 4d e0 */leal 0xffffffe0(%ebp),%ecx
/* 804817b:cd 80 */int $0x80
/* 804817d:83 f8 00 */cmp $0x0,%eax
/* 8048180:0f 8c b1 00 00 00 */jl exit
/* 8048186:89 45 e0 */movl %eax,0xffffffe0(%ebp)
/* 8048189:40 */inc %eax
/* 804818a:b8 66 00 00 00 */mov $SYS_socketcall,%eax
/* 804818f:bb 03 00 00 00 */mov $SYS_read,%ebx
/* 8048194:8d 55 f0 */leal 0xfffffff0(%ebp),%edx
/* 8048197:89 55 e4 */movl %edx,0xffffffe4(%ebp)
/* 804819a:c7 45 e8 10 00 00 00 */movl $0x00000010,0xffffffe8(%ebp)
/* 80481a1:8d 4d e0 */leal 0xffffffe0(%ebp),%ecx
/* 80481a4:cd 80 */int $0x80
/* 80481a6:83 f8 00 */cmp $0x0,%eax
/* 80481a9:7c 59 */jl next
/* 80481ab:b9 75 82 04 08 */mov $str,%ecx
/* 80481b0:ba 06 00 00 00 */mov $0x00000006, %edx
/* 80481b5:e8 63 00 00 00 */call pr_sock
/* 80481ba:b8 03 00 00 00 */mov $SYS_read,%eax
/* 80481bf:8b 5d e0 */movl 0xffffffe0(%ebp),%ebx
/* 80481c2:8d 8d e0 fd ff ff */leal 0xfffffde0(%ebp),%ecx
/* 80481c8:ba 32 00 00 00 */mov $0x00000032, %edx
/* 80481cd:cd 80 */int $0x80
/* 80481cf:89 f8 */mov %edi,%eax
/* 80481d1:e8 68 00 00 00 */call int2ascii

/* 80481d6: */return:
/* 80481d6:b9 7c 82 04 08 */mov $open,%ecx
/* 80481db:ba 09 00 00 00 */mov $0x00000009, %edx
/* 80481e0:e8 2b 00 00 00 */call pr_std
/* 80481e5:8d 8d e0 fd ff ff */leal 0xfffffde0(%ebp),%ecx
/* 80481eb:ba 1e 00 00 00 */mov $0x0000001e, %edx
/* 80481f0:e8 1b 00 00 00 */call pr_std
/* 80481f5:b9 a5 82 04 08 */mov $cr,%ecx
/* 80481fa:ba 02 00 00 00 */mov $AF_INET,%edx
/* 80481ff:e8 0c 00 00 00 */call pr_std

/* 8048204: */next:
/* 8048204:47 */inc %edi
/* 8048205:89 f8 */mov %edi,%eax
/* 8048207:3c 65 */cmp $0x65,%al
/* 8048209:74 2c */jz exit
/* 804820b:e9 23 ff ff ff */jmp loop

/* 8048210: */pr_std:
/* 8048210:b8 04 00 00 00 */mov $SYS_write,%eax
/* 8048215:bb 01 00 00 00 */mov $STDOUT,%ebx
/* 804821a:cd 80 */int $0x80
/* 804821c:c3 */ret

/* 804821d: */pr_sock:
/* 804821d:b8 04 00 00 00 */mov $SYS_write,%eax
/* 8048222:8b 5d e0 */movl 0xffffffe0(%ebp),%ebx
/* 8048225:cd 80 */int $0x80
/* 8048227:c3 */ret

/* 8048228: */usage:
/* 8048228:b9 86 82 04 08 */mov $use,%ecx
/* 804822d:ba 1f 00 00 00 */mov $uselen,%edx
/* 8048232:e8 d9 ff ff ff */call pr_std

/* 8048237: */exit:
/* 8048237:b8 01 00 00 00 */mov $STDOUT,%eax
/* 804823c:cd 80 */int $0x80

/* 804823e: */int2ascii:
/* 804823e:31 f6 */xor %esi,%esi

/* 8048240: */loop1:
/* 8048240:ba 00 00 00 00 */mov $0x00000000, %edx
/* 8048245:bb 0a 00 00 00 */mov $0x0000000a, %ebx
/* 804824a:f7 f3 */div %ebx
/* 804824c:80 c2 30 */add $0x30,%dl
/* 804824f:52 */push %edx
/* 8048250:46 */inc %esi
/* 8048251:3c 00 */cmp $0x0,%al
/* 8048253:74 02 */jz print
/* 8048255:eb e9 */jmp loop1

/* 8048257: */print:
/* 8048257:8f 01 */popl (%ecx)
/* 8048259:85 f6 */test %esi,%esi
/* 804825b:0f 84 75 ff ff ff */jz return
/* 8048261:4e */dec %esi
/* 8048262:b8 04 00 00 00 */mov $SYS_write,%eax
/* 8048267:bb 01 00 00 00 */mov $STDOUT,%ebx
/* 804826c:ba 02 00 00 00 */mov $AF_INET,%edx
/* 8048271:cd 80 */int $0x80
/* 8048273:eb e2 */jmp print

/* 8048275: */str:
/* 8048275:data at .text detected! */
/* 8048275: */ .string "testr
"

/* 804827c: */open:
/* 804827c:data at .text detected! */
/* 804827c: */ .string " is open:"

/* 8048286: */use:
/* 8048286:data at .text detected! */
/* 8048286: */ .string "./0x4553_asm_portscan -t <ip>
"

/* 80482a5: */cr:
/* 80482a5:data at .text detected! */
/* 80482a5: */ .string "
"



Демо 2 - тестовое приложение на Си с зачисткой символов через strip. Как видите имена переменных, при их отсутствии, задаются автоматически.

$cat lol.c
int main()
{

write(1,"hello ",6);
printf("fucking ");
puts("world");
exit(0);

}

$gcc lol.c -o lol
$strip lol
$./0x4553_Reconstructor lol -C

/*|--------------------------------------
*|0x4553_Reconstructor
*| ELF format.............[detected]
*| Start disassembling of.[lol]
*| Entry point is.........[80483b0]
*| .text segment size.....[352]
*| .rodata section size...[30]
*| .rodata section addr...[804852c]
*| label mode.............[on]
*| .rel.plt...............[found]
*| .dynstr................[found]
*| string mode............[on]
*| binarie comlied with...[gcc]
*|--------------------------------------
*/

/* 80483b0: */.text /*segment*/
.globl main
/* main() function found at 0x8048490 */
main:
/* 8048490:55 */push %ebp
/* 8048491:89 e5 */mov %esp,%ebp
/* 8048493:83 ec 08 */sub $0x8,%esp
/* 8048496:83 c4 fc */add $0xfc,%esp
/* 8048499:6a 06 */push $0x6
/* 804849b:68 34 85 04 08 */push $String0/* pointing to .rodata */
/* 80484a0:6a 01 */push $0x1
/* 80484a2:e8 a5 fe ff ff */call write/*0x804834c*/
/* 80484a7:83 c4 10 */add $0x10,%esp
/* 80484aa:83 c4 f4 */add $0xf4,%esp
/* 80484ad:68 3b 85 04 08 */push $String1/* pointing to .rodata */
/* 80484b2:e8 d5 fe ff ff */call printf/*0x804838c*/
/* 80484b7:83 c4 10 */add $0x10,%esp
/* 80484ba:83 c4 f4 */add $0xf4,%esp
/* 80484bd:68 44 85 04 08 */push $String2/* pointing to .rodata */
/* 80484c2:e8 95 fe ff ff */call puts/*0x804835c*/
/* 80484c7:83 c4 10 */add $0x10,%esp
/* 80484ca:83 c4 f4 */add $0xf4,%esp
/* 80484cd:6a 00 */push $0x0
/* 80484cf:e8 c8 fe ff ff */call exit/*0x804839c*/
/* 80484d4:83 c4 10 */add $0x10,%esp
/* 80484d7:c9 */leave
/* 80484d8:c3 */ret


/* 804852c: */.section .rodata
/* 804852c: */ .string ""
/* 804852e: */ .string ""
/* 804852f: */ .string ""
/* 8048530: */ .string ""
/* 8048532: */ .string ""

/* 8048534: */String0:
/* 8048534: */ .string "hello "

/* 804853b: */String1:
/* 804853b: */ .string "fucking "

/* 8048544: */String2:
/* 8048544: */ .string "world"



Демо 3 - дизасм реального приложения /bin/true. Генерируются подфункции, на выходе готовый и рабочий сорц.

bash-2.05a# ls -la /bin/true
-rwxr-xr-x 1 root bin 4720 Feb 19 2002 /bin/true

bash-2.05a# ltrace /bin/true
__libc_start_main(0x08048678, 1, 0xbffff794, 0x0804843c, 0x080487ec <unfinished ...>
__register_frame_info(0x08049aec, 0x08049c10, 0xbffff738, 0x4004bcab, 0x4013ce58) = 0x08049c10
setlocale(6, "") = "C"
bindtextdomain("sh-utils", "/usr/share/locale") = "/usr/share/locale"
textdomain("sh-utils") = "sh-utils"
exit(0 <unfinished ...>
__deregister_frame_info(0x08049aec, 0x40009c7a, 0x40014a7c, 0x40142020, 1) = 0
+++ exited (status 0) +++

bash-2.05a# ./0x4553_Reconstructor -C /bin/true > true.s
bash-2.05a# cat true.s
/*|--------------------------------------
*|0x4553_Reconstructor
*| ELF format.............[detected]
*| Start disassembling of.[/bin/true]
*| Entry point is.........[8048550]
*| .text segment size.....[668]
*| .rodata segment size...[704]
*| .rodata segment addr...[8048820]
*| label mode.............[on]
*| .rel.plt...............[found]
*| .dynstr................[found]
*| string mode............[on]
*| binarie comlied with...[gcc]
*|--------------------------------------
*/

/* 8048550: */.text /*segment*/
.globl main
/* main() function found at 0x8048678 */
/* main() size - 316 */
main:
/* 8048678:55 */push %ebp
/* 8048679:89 e5 */mov %esp,%ebp
/* 804867b:83 ec 0c */sub $0xc,%esp
/* 804867e:57 */push %edi
/* 804867f:56 */push %esi
/* 8048680:53 */push %ebx
/* 8048681:8b 5d 08 */movl 0x8(%ebp),%ebx
/* 8048684:8b 55 0c */movl 0xc(%ebp),%edx
/* 8048687:8b 02 */movl (%edx),%eax
/* 8048689:a3 28 9c 04 08 */mov %eax, Object0/* pointing to .bss */
/* 804868e:83 c4 f8 */add $0xfffffff8,%esp
/* 8048691:68 68 89 04 08 */push $P_String0/* pointing to .rodata */
/* 8048696:6a 06 */push $0x6
/* 8048698:e8 17 fe ff ff */call setlocale/*0x80484b4*/
/* 804869d:83 c4 f8 */add $0xfffffff8,%esp
/* 80486a0:68 69 89 04 08 */push $P_String1/* pointing to .rodata */
/* 80486a5:68 7b 89 04 08 */push $P_String2/* pointing to .rodata */
/* 80486aa:e8 35 fe ff ff */call bindtextdomain/*0x80484e4*/
/* 80486af:83 c4 20 */add $0x20,%esp
/* 80486b2:83 c4 f4 */add $0xfffffff4,%esp
/* 80486b5:68 7b 89 04 08 */push $P_String2/* pointing to .rodata */
/* 80486ba:e8 55 fe ff ff */call textdomain/*0x8048514*/
/* 80486bf:83 c4 10 */add $0x10,%esp
/* 80486c2:83 fb 02 */cmp $0x2,%ebx
/* 80486c5:75 74 */jnz Label3 /*0x804873b*/
/* 80486c7:83 c4 f4 */add $0xfffffff4,%esp
/* 80486ca:68 84 89 04 08 */push $P_String4/* pointing to .rodata */
/* 80486cf:e8 c0 fd ff ff */call getenv/*0x8048494*/
/* 80486d4:83 c4 10 */add $0x10,%esp
/* 80486d7:85 c0 */test %eax,%eax
/* 80486d9:75 60 */jnz Label3 /*0x804873b*/
/* 80486db:8b 45 0c */movl 0xc(%ebp),%eax
/* 80486de:8b 70 04 */movl 0x4(%eax),%esi
/* 80486e1:bf 94 89 04 08 */mov $M_String0, %edi
/* 80486e6:b9 07 00 00 00 */mov $0x7, %ecx
/* 80486eb:fc */cld
/* 80486ec:a8 00 */test $0x0,%al
/* 80486ee:f3 */repz
/* 80486ef:a6 */cmpsb
/* 80486f0:75 0d */jnz Label6 /*0x80486ff*/
/* 80486f2:83 c4 f4 */add $0xfffffff4,%esp
/* 80486f5:6a 00 */push $0x0
/* 80486f7:e8 28 ff ff ff */call Label7 /*0x8048624*/
/* 80486fc:83 c4 10 */add $0x10,%esp

/* 80486ff: */Label6:
/* 80486ff:8b 55 0c */movl 0xc(%ebp),%edx
/* 8048702:8b 72 04 */movl 0x4(%edx),%esi
/* 8048705:bf 9b 89 04 08 */mov $M_String1, %edi
/* 804870a:b9 0a 00 00 00 */mov $0xa, %ecx
/* 804870f:fc */cld
/* 8048710:a8 00 */test $0x0,%al
/* 8048712:f3 */repz
/* 8048713:a6 */cmpsb
/* 8048714:75 25 */jnz Label3 /*0x804873b*/
/* 8048716:83 c4 f4 */add $0xfffffff4,%esp
/* 8048719:68 a5 89 04 08 */push $P_String5/* pointing to .rodata */
/* 804871e:68 ac 89 04 08 */push $P_String6/* pointing to .rodata */
/* 8048723:68 b0 89 04 08 */push $P_String7/* pointing to .rodata */
/* 8048728:68 bd 89 04 08 */push $P_String8/* pointing to .rodata */
/* 804872d:ff 35 0c 9c 04 08 */incl 0x08049c0c
/* 8048733:e8 10 00 00 00 */call Label9 /*0x8048748*/
/* 8048738:83 c4 20 */add $0x20,%esp

/* 804873b: */Label3:
/* 804873b:83 c4 f4 */add $0xfffffff4,%esp
/* 804873e:6a 00 */push $0x0
/* 8048740:e8 ef fd ff ff */call exit/*0x8048534*/
/* 8048745:8d 76 00 */leal 0x0(%esi),%esi

/* 8048748: */Label9:
/* 8048748:55 */push %ebp
/* 8048749:89 e5 */mov %esp,%ebp
/* 804874b:83 ec 10 */sub $0x10,%esp
/* 804874e:56 */push %esi
/* 804874f:53 */push %ebx
/* 8048750:8b 5d 08 */movl 0x8(%ebp),%ebx
/* 8048753:8b 75 18 */movl 0x18(%ebp),%esi
/* 8048756:83 c4 f4 */add $0xfffffff4,%esp
/* 8048759:ff 75 14 */pushl 0x14(%ebp)
/* 804875c:


Alt text

Если вам нравится играть в опасную игру, присоединитесь к нам - мы научим вас правилам!

Подписаться