简体   繁体   English

从Python传递shellcode作为命令行参数

[英]Passing shellcode from Python as command line argument

I am currently preparing a small presentation about computer security among my fellow students. 我目前正在为我的同学们准备有关计算机安全的小型演讲。 To get them at least a bit excited I wanted to demonstrate how the wrong use of the strcpy-function inc C can be exploited. 为了使他们至少有点兴奋,我想演示如何利用strcpy-function inc C的错误用法。 This is the code of the vulnerable program: 这是易受攻击的程序的代码:

#include<string.h>
#include<stdio.h>
void function(char *buffer1)
{
 char buffer2[5];
 strcpy(buffer2,buffer1);
}

int main (int argc, char **argv)
{
 function(argv[1]);
 return 0;
}

Just using the command line I was able to crash the application by calling it with 只需使用命令行,我就可以通过调用它来使应用程序崩溃

test.exe ABCDEFGHIJKLMNOPQRSTUVWXYZ

Which gave me the correct 0x4D4C4B4A (MLKJ) for the EIP. 这给了我正确的EIP 0x4D4C4B4A(MLKJ)。 It also works if I call it from Python: 如果我从Python调用它,它也可以工作:

os.system("test.exe ABCDEFGHIJKLMNOPQRSTUVWXYZ")

However, if I want to put an address instead of JKLM, like this: 但是,如果我想放置一个地址而不是JKLM,如下所示:

 os.system("test.exe ABCDEFGHI\x75\x9a\x21\x1bNOPQRSTUVWXYZ")

It gives me following output near the ESP on the Stack: 它为我提供了堆栈上ESP附近的以下输出:

0028cca0  e4 21 d7 41 42 43 44 45-46 47 48 49 75 c2 9a 21  .!.ABCDEFGHIu..!
0028ccb0  1b 4e 4f 50 51 52 53 54-55 56 57 58 59 5a 00 00  .NOPQRSTUVWXYZ..

Here the 75 c2 9a 21 matters because it is almost what I expected, except the 0x1B, which is the ASCII Character for ESCAPE, is replaced by 0xC2. 这里的75 c2 9a 21很重要,因为它几乎是我所期望的,除了0x1B(它是ESCAPE的ASCII字符)被0xC2代替。 When I change the order of the address, so it looks like this: \\x21\\x1b\\x75\\x9a, the 9a gets replaced by the same mysterious C2. 当我更改地址的顺序时,它看起来像这样:\\ x21 \\ x1b \\ x75 \\ x9a,9a被相同的神秘C2代替。 Does anyone know whats the matter with the code? 有人知道代码有什么问题吗? Am I missing some basic point or is it some kind of protection against stack based buffer overflows? 我是否缺少一些基本要点,还是针对基于堆栈的缓冲区溢出提供了某种保护?

Thanks, 谢谢,

It looks like your text is undergoing UTF-8 conversion. 您的文字似乎正在进行UTF-8转换。 Note that your original bytes: 请注意您的原始字节:

75 9a 21 1b
   ^^

are replaced by 被替换

75 c2 9a 21 1b
   ^^^^^

I've highlighted the UTF-8 encoded byte. 我强调了UTF-8编码的字节。 If you're using Python 3, try: 如果您使用的是Python 3,请尝试:

os.system(b"test.exe ABCDEFGHI\x75\x9a\x21\x1bNOPQRSTUVWXYZ")

The b"" indicates that the data is a byte sequence, and shouldn't be converted from Unicode to the default encoding (which in your case seems to be UTF-8). b""表示数据是字节序列,不应将其从Unicode转换为默认编码(在您的情况下,它似乎是UTF-8)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM