【发布时间】:2015-06-16 20:33:40
【问题描述】:
我开始参加有关软件安全的在线课程。在其中一个部分中,我被指示使用缓冲区溢出执行隐藏的 C 函数。我开始思考:如果我可以将机器指令直接传递给堆栈不安全的可执行文件会发生什么?
我一直在尝试什么,或者我现在在哪里:
(Simple routine or function compiled or assembled into an object,
then printed to screen with `objdump`)
>>> x = "984579273698529424576299" # open("file.o").read()
>>> for i in range(0, len(x), 2):
... print "\\x" + x[i:i+2],
...
\x98 \x45 \x79 \x27 \x36 \x98 \x52 \x94 \x24 \x57 \x62 \x99
int main(void) {
unsigned char shellcode[] = { <formatted shellcode bytes from objdump> };
void (*fn)(void) = (void (*)(void))shellcode;
fn();
return 0;
}
我尝试过的一些序列的实际示例:
hello.c
int main(void)
{
char buf[] = {'H', 'e', 'l', 'l', 'o', '\n', '\0'};
write(1, buf, sizeof(buf));
exit(0);
}
shellforge2.py(正在进行中)
import os
import re
import sys
src = sys.argv[1]
asmsrc = src[:src.find(".")] + ".s"
binobj = src[:src.find(".")] + ".o"
call = "gcc -march=i386 -O3 -S -fPIC -Winline " + \
"-finline-functions -ffreestanding " + \
"-o %s -m32 %s" % (asmsrc, src)
print call
print
f = os.popen(call)
f.close()
asm = open(asmsrc).readlines()
ignores = (".file", ".def")
asm_stripped = []
for line in asm:
write = True
for ignore in ignores:
if ignore in line: write = False
if write: print line.replace("\n", "")
ret = os.system("gcc -c -o %s %s" % (binobj, asmsrc))
f = os.popen("objdump -j .text -s -z %s" % (binobj, ))
objdump = f.readlines()
f.close()
regx = re.compile("^ [0-9a-f]{4}")
regxret = ""
for line in objdump:
if regx.match(line):
regxret = regxret + "".join(line[:42].split()[1:])
dumphex = []
while regxret:
dumphex.append(regxret[:2])
regxret = regxret[2:]
print dumphex
result = ["unsigned char shellcode[] = {",]
for ch in dumphex[:-1]:
result.append("'\\x%s', " % ch)
result.append("'\\x%s' };" % dumphex[-1:][0])
print "".join(result)
shell 命令和输出:
$ python shellforge2.py hello.c
gcc -march=i386 -O3 -S -fPIC -Winline -finline-functions -ffreestanding -o hello.s -m32 hello.c
.text
.p2align 2,,3
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %edi
pushl %esi
pushl %ebx
pushl %ecx
subl $28, %esp
call .L3
.L3:
popl %ebx
addl $_GLOBAL_OFFSET_TABLE_+[.-.L3], %ebx
leal -23(%ebp), %edi
leal C.0.751@GOTOFF(%ebx), %esi
movl $7, %ecx
rep movsb
pushl $7
leal -23(%ebp), %eax
pushl %eax
pushl $1
call write@PLT
movl $0, (%esp)
call exit@PLT
addl $16, %esp
leal -16(%ebp), %esp
popl %ecx
popl %ebx
popl %esi
popl %edi
leave
leal -4(%ecx), %esp
ret
.size main, .-main
.section .rodata
.type C.0.751, @object
.size C.0.751, 7
C.0.751:
.byte 72
.byte 101
.byte 108
.byte 108
.byte 111
.byte 10
.byte 0
.ident "GCC: (Ubuntu 4.3.3-5ubuntu4) 4.3.3"
.section .note.GNU-stack,"",@progbits
['8d', '4c', '24', '04', '83', 'e4', 'f0', 'ff', '71', 'fc', '55', '89', 'e5', '57', '56', '53', '51', '83', 'ec', '1c', 'e8', '00', '00', '00', '00', '5b', '81', 'c3', '03', '00', '00', '00', '8d', '7d', 'e9', '8d', 'b3', '00', '00', '00', '00', 'b9', '07', '00', '00', '00', 'f3', 'a4', '6a', '07', '8d', '45', 'e9', '50', '6a', '01', 'e8', 'fc', 'ff', 'ff', 'ff', 'c7', '04', '24', '00', '00', '00', '00', 'e8', 'fc', 'ff', 'ff', 'ff', '83', 'c4', '10', '8d', '65', 'f0', '59', '5b', '5e', '5f', 'c9', '8d', '61', 'fc', 'c3']
unsigned char shellcode[] = {'\x8d', '\x4c', '\x24', '\x04', '\x83', '\xe4', '\xf0', '\xff', '\x71', '\xfc', '\x55', '\x89', '\xe5', '\x57', '\x56', '\x53', '\x51', '\x83', '\xec', '\x1c', '\xe8', '\x00', '\x00', '\x00', '\x00', '\x5b', '\x81', '\xc3', '\x03', '\x00', '\x00', '\x00', '\x8d', '\x7d', '\xe9', '\x8d', '\xb3', '\x00', '\x00', '\x00', '\x00', '\xb9', '\x07', '\x00', '\x00', '\x00', '\xf3', '\xa4', '\x6a', '\x07', '\x8d', '\x45', '\xe9', '\x50', '\x6a', '\x01', '\xe8', '\xfc', '\xff', '\xff', '\xff', '\xc7', '\x04', '\x24', '\x00', '\x00', '\x00', '\x00', '\xe8', '\xfc', '\xff', '\xff', '\xff', '\x83', '\xc4', '\x10', '\x8d', '\x65', '\xf0', '\x59', '\x5b', '\x5e', '\x5f', '\xc9', '\x8d', '\x61', '\xfc', '\xc3' };
testshell.c
int main(void) {
unsigned char shellcode[] = {'\x8d', '\x4c', '\x24', '\x04', '\x83', '\xe4', '\xf0', '\xff', '\x71', '\xfc', '\x55', '\x89', '\xe5', '\x57', '\x56', '\x53', '\x51', '\x83', '\xec', '\x1c', '\xe8', '\x00', '\x00', '\x00', '\x00', '\x5b', '\x81', '\xc3', '\x03', '\x00', '\x00', '\x00', '\x8d', '\x7d', '\xe9', '\x8d', '\xb3', '\x00', '\x00', '\x00', '\x00', '\xb9', '\x07', '\x00', '\x00', '\x00', '\xf3', '\xa4', '\x6a', '\x07', '\x8d', '\x45', '\xe9', '\x50', '\x6a', '\x01', '\xe8', '\xfc', '\xff', '\xff', '\xff', '\xc7', '\x04', '\x24', '\x00', '\x00', '\x00', '\x00', '\xe8', '\xfc', '\xff', '\xff', '\xff', '\x83', '\xc4', '\x10', '\x8d', '\x65', '\xf0', '\x59', '\x5b', '\x5e', '\x5f', '\xc9', '\x8d', '\x61', '\xfc', '\xc3' };
int (*_main)(void) = (int (*)(void))shellcode;
_main();
return 0;
}
hello.c 从 C 源代码转换为带有 shellforge2.py 的指令数组,并粘贴到 testshell.c 中。 testshell.c 被编译并执行。
$ ./testshell
Illegal instruction
我希望我把这个问题说清楚了。
【问题讨论】:
-
你真的应该提供一个你在说什么的例子。并描述它有什么问题。
-
首先,您通常使用汇编器来创建shellcode,而不是编译器。其次,您将无法通过覆盖任何现代操作系统中的堆栈来执行 shellcode,因为它们具有防止执行可写内存的保护。阅读 ROP(面向返回的编程:en.wikipedia.org/wiki/Return-oriented_programming),这是执行此操作的现代方式。
-
@Matthew 我一直在使用汇编器和编译器来创建目标文件。所以基本上我的问题是“离题”,因为无法给出正确答案?
-
投票重新开放,但仍然缺乏信息。太笼统了。举一个应该起作用的例子并描述实际结果是什么。
-
@MotokoKusanagi 我建议您删除此内容并进行更多研究。我将您的示例 .c 中的 shellcode 转换回汇编指令(参见此处:pastie.org/10244014),它甚至永远不会单独运行。不要使用“objdump”来制作奇怪的 shellcode,实际学习汇编并使用 nasm 或可以创建平面目标文件的汇编程序。您不会通过覆盖任何现代操作系统上的堆栈来运行 shellcode,期间。如果您想快速修复,只需将您的 shellcode 声明为
const static unsigned char,因为它保持只读状态,但仍会出现段错误。
标签: python c gcc assembly shellcode