shellcode是如何从C生成的? - 附代码示例
How is shellcode generated from C? - With code example
我开始学习有关软件安全的在线课程。在其中一个部分中,我被指示使用缓冲区溢出执行一个隐藏的 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', '[=11=]'};
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 , %esp
call .L3
.L3:
popl %ebx
addl $_GLOBAL_OFFSET_TABLE_+[.-.L3], %ebx
leal -23(%ebp), %edi
leal C.0.751@GOTOFF(%ebx), %esi
movl , %ecx
rep movsb
pushl
leal -23(%ebp), %eax
pushl %eax
pushl
call write@PLT
movl [=13=], (%esp)
call exit@PLT
addl , %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 程序创建 shell 代码的问题不是您无法控制生成的程序集,也不是与代码生成相关的问题。
从 C 程序创建 shell 代码的问题是 符号解析 或 重定位 ,随便你怎么称呼它。
你的做法,据我了解,是对的,你只是使用了错误的代码或者换了个角度,你想要的太多了。
我不打算解释图像的加载是如何工作的,但是当你使用像 write
这样的函数时,生成的程序集是一个 call ADDRESS
指令,但地址尚未编译,它只是一个加载程序将在 运行 时间借助图像上找到的结构(参见 PE、ELF)解析相对偏移量。
shell 代码不会被 OS 加载(它是程序中的程序),因此它的符号不会被解析。看看这个:
这是对 hello.c 中 write
函数的调用,因为它是通过 GDB 步进执行的。请注意,呼叫位于 28cbfch
,被呼叫者应位于 28cbfd
,即指令开始后的一个字节。这是不可能的,因为指令本身占用 5 个字节,这意味着对 write
的调用被编码为 call -4
,即相对地址 尚未由装载机.
您将在课程中了解到,shell 代码通常直接在 Linux 和 int 80h
IA32 平台上使用系统调用。如果您用系统调用调用替换对 write
的调用,您的 shell 代码 应该 (可能是其他问题,请不要相信我)工作。
有趣的事实:我原以为堆栈在默认情况下不可执行(有关更多信息,请查找 NX 位),但在 cygwin 中确实如此。对于 ELF 文件,您可以使用 --execstack
来确定堆栈是可执行的。
我开始学习有关软件安全的在线课程。在其中一个部分中,我被指示使用缓冲区溢出执行一个隐藏的 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', '[=11=]'};
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 , %esp
call .L3
.L3:
popl %ebx
addl $_GLOBAL_OFFSET_TABLE_+[.-.L3], %ebx
leal -23(%ebp), %edi
leal C.0.751@GOTOFF(%ebx), %esi
movl , %ecx
rep movsb
pushl
leal -23(%ebp), %eax
pushl %eax
pushl
call write@PLT
movl [=13=], (%esp)
call exit@PLT
addl , %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 程序创建 shell 代码的问题不是您无法控制生成的程序集,也不是与代码生成相关的问题。
从 C 程序创建 shell 代码的问题是 符号解析 或 重定位 ,随便你怎么称呼它。
你的做法,据我了解,是对的,你只是使用了错误的代码或者换了个角度,你想要的太多了。
我不打算解释图像的加载是如何工作的,但是当你使用像 write
这样的函数时,生成的程序集是一个 call ADDRESS
指令,但地址尚未编译,它只是一个加载程序将在 运行 时间借助图像上找到的结构(参见 PE、ELF)解析相对偏移量。
shell 代码不会被 OS 加载(它是程序中的程序),因此它的符号不会被解析。看看这个:
这是对 hello.c 中 write
函数的调用,因为它是通过 GDB 步进执行的。请注意,呼叫位于 28cbfch
,被呼叫者应位于 28cbfd
,即指令开始后的一个字节。这是不可能的,因为指令本身占用 5 个字节,这意味着对 write
的调用被编码为 call -4
,即相对地址 尚未由装载机.
您将在课程中了解到,shell 代码通常直接在 Linux 和 int 80h
IA32 平台上使用系统调用。如果您用系统调用调用替换对 write
的调用,您的 shell 代码 应该 (可能是其他问题,请不要相信我)工作。
有趣的事实:我原以为堆栈在默认情况下不可执行(有关更多信息,请查找 NX 位),但在 cygwin 中确实如此。对于 ELF 文件,您可以使用 --execstack
来确定堆栈是可执行的。