- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我开始学习有关软件安全的在线类(class)。在其中一节中,我被指示使用缓冲区溢出来执行隐藏的 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
使用 shellforge2.py
从 C 源代码转换为指令数组,并将其粘贴到 testshell.c
中。 testshell.c
被编译并执行。
$ ./testshell
Illegal instruction
我希望我把这个问题说清楚了。
最佳答案
从 C 程序创建 shellcode 的问题不是你无法控制生成的程序集,也不是与代码生成相关的东西。
从 C 程序创建 shellcode 的问题是符号解析或重定位,随便你怎么调用它。
据我所知,您的方法是正确的,您只是使用了错误的代码或处于不同的 View ,您想要太多。
我不会解释图像的加载是如何工作的,但简单地说,当您使用像 write
这样的函数时,生成的程序集是一个 call ADDRESS
指令,但地址不是尚未编译,它只是一个相对偏移量,加载器将在运行时借助镜像上找到的结构来解析(参见 PE、ELF)。
您的 shell 代码不会被操作系统加载(它是程序中的程序),因此它的符号无法解析。看看这个:
这是对 hello.c 中 write
函数的调用,因为它是通过 GDB 单步执行的。请注意,调用位于 28cbfch
,被调用者应位于 28cbfd
,即指令开始后仅一个字节。这是不可能的,因为指令本身需要 5 个字节,这意味着对 write
的调用被编码为 call -4
,即相对地址加载程序尚未解决。
您将在类(class)中了解到,shell 代码通常在 Linux 中直接使用 IA32 平台上的 int 80h
系统调用。如果您用系统调用替换对 write
的调用,您的 shell 代码应该(可能是其他问题,不要相信我)可以工作。
有趣的事实:我原以为堆栈默认情况下不可执行(查找 NX 位以获取更多信息),但在 cygwin 中确实如此。对于 ELF 文件,您可以使用 --execstack
来确保堆栈是可执行的。
关于python - shellcode是如何从C生成的? - 带有代码示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30877635/
我尝试理解[c代码 -> 汇编]代码 void node::Check( data & _data1, vector& _data2) { -> push ebp -> mov ebp,esp ->
我需要在当前表单(代码)的上下文中运行文本文件中的代码。其中一项要求是让代码创建新控件并将其添加到当前窗体。 例如,在Form1.cs中: using System.Windows.Forms; ..
我有此 C++ 代码并将其转换为 C# (.net Framework 4) 代码。有没有人给我一些关于 malloc、free 和 sprintf 方法的提示? int monate = ee; d
我的网络服务器代码有问题 #include #include #include #include #include #include #include int
给定以下 html 代码,将列表中的第三个元素(即“美丽”一词)以斜体显示的 CSS 代码是什么?当然,我可以给这个元素一个 id 或一个 class,但 html 代码必须保持不变。谢谢
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我试图制作一个宏来避免重复代码和注释。 我试过这个: #define GrowOnPage(any Page, any Component) Component.Width := Page.Surfa
我正在尝试将我的旧 C++ 代码“翻译”成头条新闻所暗示的 C# 代码。问题是我是 C# 中的新手,并不是所有的东西都像 C++ 中那样。在 C++ 中这些解决方案运行良好,但在 C# 中只是不能。我
在 Windows 10 上工作,R 语言的格式化程序似乎没有在 Visual Studio Code 中完成它的工作。我试过R support for Visual Studio Code和 R-T
我正在处理一些报告(计数),我必须获取不同参数的计数。非常简单但乏味。 一个参数的示例查询: qCountsEmployee = ( "select count(*) from %s wher
最近几天我尝试从 d00m 调试网络错误。我开始用尽想法/线索,我希望其他 SO 用户拥有可能有用的宝贵经验。我希望能够提供所有相关信息,但我个人无法控制服务器环境。 整个事情始于用户注意到我们应用程
我有一个 app.js 文件,其中包含如下 dojo amd 模式代码: require(["dojo/dom", ..], function(dom){ dom.byId('someId').i
我对“-gencode”语句中的“code=sm_X”选项有点困惑。 一个例子:NVCC 编译器选项有什么作用 -gencode arch=compute_13,code=sm_13 嵌入库中? 只有
我为我的表格使用 X-editable 框架。 但是我有一些问题。 $(document).ready(function() { $('.access').editable({
我一直在通过本教程学习 flask/python http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-wo
我想将 Vim 和 EMACS 用于 CNC、G 代码和 M 代码。 Vim 或 EMACS 是否有任何语法或模式来处理这种类型的代码? 最佳答案 一些快速搜索使我找到了 this vim 和 thi
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve this
这个问题在这里已经有了答案: Enabling markdown highlighting in Vim (5 个回答) 6年前关闭。 当我在 Vim 中编辑包含 Markdown 代码的 READM
我正在 Swift3 iOS 中开发视频应用程序。基本上我必须将视频 Assets 和音频与淡入淡出效果合并为一个并将其保存到 iPhone 画廊。为此,我使用以下方法: private func d
pipeline { agent any stages { stage('Build') { steps { e
我是一名优秀的程序员,十分优秀!