如何从C生成外壳代码?使用代码examp

2024-10-01 13:24:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我开始上一门关于软件安全的在线课程。在其中一节中,我被指示使用缓冲区溢出执行一个隐藏的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;
}

一个实际的我尝试过的一些序列的例子:

你好。c

^{pr2}$

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.ctestshell.c被编译和执行。在

$ ./testshell
Illegal instruction

我希望我把这个问题讲清楚。在


Tags: insrcmainbyteprintffespx00
1条回答
网友
1楼 · 发布于 2024-10-01 13:24:11

从C程序创建外壳代码的问题不是您无法控制生成的程序集,也不是与代码生成相关的内容。
从C程序创建外壳代码的问题是符号解析重新定位,可以随意调用它。在

你的做法,据我所知,是对的,你只是使用了错误的代码,或者用了不同的观点,你想要的太多了。
我不打算解释映像的加载是如何工作的,但是当您使用像write这样的函数时,生成的程序集是一条call ADDRESS指令,但地址还没有编译,它只是一个相对偏移量,它将由加载程序在运行时借助映像上找到的结构进行解析(请参见PE,ELF)。
您的shell代码不会被操作系统加载(它是程序中的一个程序),因此它的符号不会被解析。看看这个:

Unresolved symbol

这是对hello.c中的write函数的调用,因为它是通过单步执行GDB来执行的。请注意,调用位于28cbfch,被调用方应该位于28cbfd,即指令开始后一个字节。这是不可能的,因为指令本身需要5个字节,这意味着对write的调用被编码为call -4,即加载程序尚未解析的相对地址。在

您将在您的课程中了解到,shell代码通常直接在Linux中使用IA32平台上的int 80h进行系统调用。如果将对write的调用替换为系统调用,那么shell代码应该可以工作(可能是其他问题,不要相信我)。在

有趣的事实:我原以为堆栈在默认情况下是不可执行的(查找NX位以获取更多信息),但在cygwin中确实是这样。对于ELF文件,可以使用 execstack确保堆栈是可执行的。在

相关问题 更多 >