如何从C生成shellcode? - 使用代码示例
我开始参加有关软件安全性的在线课程。在其中一节中,我被指示使用缓冲区溢出来执行隐藏的C函数。我开始思考:如果我可以将机器指令直接传递给堆不安全的可执行文件,会发生什么?如何从C生成shellcode? - 使用代码示例
我一直在努力,或者我在现在:
(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)
外壳命令和输出:
$ 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 [email protected](%ebx), %esi
movl $7, %ecx
rep movsb
pushl $7
leal -23(%ebp), %eax
pushl %eax
pushl $1
call [email protected]
movl $0, (%esp)
call [email protected]
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
我希望我明确提出这个问题。
从C程序创建shellcode的问题不是你无法控制生成的程序集,也不是与代码生成相关的东西。
从C程序创建shellcode的问题是符号分辨率或重新定位,称之为任何你喜欢的。
你的方法,因为我明白,是正确的,你只是使用错误的代码或在不同的视图,你想太多。
我不打算解释如何加载一个图像的工作原理,但是当你使用一个像write
这样的函数时,生成的程序集是一个call ADDRESS
指令,但地址还没有被编译,它只是一个相对偏移量,将被解析通过装载程序在运行时借助图像上的结构(请参阅PE,ELF)。
您的shell代码不会被OS加载(它是程序中的程序),因此它的符号不会被解析。看看这个:
,因为它是用GDB加强执行这是您的hello.c调用到write
功能。请注意,该电话号码为28cbfch
,被叫方电话号码应为28cbfd
,即。只是在指令开始之后的一个字节。这是不可能的,因为指令本身需要5个字节,这意味着对write
的调用编码为call -4
,即一个相对地址尚未由装载机解决。
在您的课程中,您将了解到shellcode通常直接在Linux上使用系统调用,在IA32平台上使用int 80h
。如果用系统调用调用替代调用write
,那么你的shell代码应该是(可能是其他问题,不要相信我)的工作。有趣的事实:默认情况下,我预计堆栈不可执行(查找NX位以获取更多信息),但在cygwin中确实如此。对于ELF文件,您可以使用--execstack
来确保堆栈是可执行的。
你真的应该提供一个你正在谈论的例子。并描述它有什么问题。 –
首先,您通常使用汇编程序来创建shellcode,而不是编译器。其次,在任何现代操作系统中,您都无法通过覆盖堆栈来执行shellcode,因为它们可以防止执行可写入的内存。阅读ROP(面向返回的编程:https://en.wikipedia.org/wiki/Return-oriented_programming),这是现代化的方式。 – Matthew
@Mthethew我一直在使用汇编器和编译器来创建对象文件。所以基本上我的问题是“脱离主题”,因为没有正确的答案可以给出? – motoku