工作 Linux 程序集 XOR 编码器/解码器在 Windows 上失败

逆向工程 部件 开发 外壳代码 重新组装 纳姆
2021-06-24 10:01:13

我是否应该能够从一个基本的(经过测试和工作的)Win7-64 消息框应用程序中提取 shellcode 并将提取的 shellcode 放入经过测试和工作的汇编语言编码器/解码器中并期望它能够工作?(Windows 的组装和链接,而不是 linux 的链接)

我已经使用下面列出的步骤在 linux 上成功测试了一个简单的 XOR 编码器/解码器。简而言之,我有一个 WORKING XOR 编码器/解码器系统,我尝试将 win764 消息框 shellcode 与我的编码/解码系统一起使用。

我只是将 Win7 消息框中提取的 shellcode 替换到我的编码/解码系统中。与 nasm -fwin64 组装,然后与 Windows 上的 golink 链接以获取 exe,并且每次都崩溃。(没有编码器的测试步骤和组装/链接/执行工作完美)

  1. 我正在为 windows 组装 XOR 解码器 - nasm -fwin64(粘贴了消息框 shellcode)
  2. 使用 golink 链接 Windows
  3. 无法在 Windows 上执行

这是非常有效的 Linux XOR 编码/解码方法


  1. 使用 HelloWorld.nasm
  2. 组装 - nasm -felf64 HelloWorld.nasm -o HelloWorld.o
  3. 使用 - for i in $(objdump -d [binary-or-objectfile] |grep "^ " |cut -f2); 提取 shellcode; 做回声 -n '\x'$i; 完成;回声
  4. 将 HelloWorld shellcode 放在 C 包装器中

    #include<stdio.h>
    #include<string.h>
    
    unsigned char code[] = \
    "\xeb\x1e\x5e\x48\x31\xc0\xb0\x01\x48\x89\xc7\x48\x89\xfa\x48\x83\xc2\x22\x0f\x05\x48\x31\xc0\x48\x83\xc0\x3c\x48\x31\xff\x0f\x05\xe8\xdd\xff\xff\xff\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x20\x0a";
    
    int  main()
    {
    
            printf("Shellcode Length:  %d\n", (int)strlen(code));
    
            int (*ret)() = (int(*)())code;
    
            ret();
    
    }
    
  5. 运行 ./HelloWorld(验证 shellcode 在 C 包装器中工作)


现在,我使用带有 python 的简单 XOR 编码器对 HelloWorld shellcode 进行 XOR。


1.python XOREncoder.py

为 nasm 输出格式化的异或 HelloWorld shellcode:

          0x41,0xb4,0xf4,0xe2,0x9b,0x6a,0x1a,0xab,0xe2,0x23,0x6d,0xe2,0x23,0x50,0xe2,0x29,0x68,0x88,0xa5,0xaf,0xe2,0x9b,0x6a,0xe2,0x29,0x6a,0x96,0xe2,0x9b,0x55,0xa5,0xaf,0x42,0x77,0x55,0x55,0x55,0xe2,0xcf,0xc6,0xc6,0xc5,0x8a,0xfd,0xc5,0xd8,0xc6,0xce,0x8a,0xa0
  1. 像这样将 XORed shellcode 放在 XORdecoder.nasm 中:

    global _start
    
    section .text
    
    _start:
    
    
    start:
            jmp find_address
    
    decoder:
            pop rdi
            xor rcx, rcx
            add cl, 50
    decode:
            xor byte [rdi], 0xAA
            inc rdi
            loop decode
    
            jmp short encoded_shellcode
    
    find_address:
            call decoder
    
            encoded_shellcode: db 0x41,0xb4,0xf4,0xe2,0x9b,0x6a,0x1a,0xab,0xe2,0x23,0x6d,0xe2,0x23,0x50,0xe2,0x29,0x68,0x88,0xa5,0xaf,0xe2,0x9b,0x6a,0xe2,0x29,0x6a,0x96,0xe2,0x9b,0x55,0xa5,0xaf,0x42,0x77,0x55,0x55,0x55,0xe2,0xcf,0xc6,0xc6,0xc5,0x8a,0xfd,0xc5,0xd8,0xc6,0xce,0x8a,0xa0
    
    1. 组装有 - nasm -felf64 HelloWorldEncoded.nasm -o HelloWorldEncoded.o
    2. 用 GCC 编译并测试。作品!

现在,这是 Windows 7 64 上的问题

我找到了一个 WIN 7 64 位 messagebox.nasm 的一个很好的例子,它只是在这里弹出一个消息框所以很自然地我想测试我的 XOR 解码器。所以我尝试为这样的窗口组装和链接我的解码器。

我使用链接中描述的这些步骤对此进行了测试:

  1. NASM -f Win64平台messageBox64bit.asm -o messageBox64bit.obj

  2. golink /控制台 messageBox64bit.obj

  3. 在 Win764 上执行 ./messageBox64bit.exe

伟大的!messageBox64bit.exe 弹出消息框。现在是问题。

  1. 我用 nasm 组装了 messageBox64bit.nasm
  2. 通过 XOR 编码器运行提取的消息框 shellcode
  3. 将 asm 友好的 XOR 编码的 shellcode 粘贴到解码器中
  4. 为新的 shellcode 长度调整 RCX (cl) 计数器
  5. 带有 -fwin64 选项的带有 nasm 的组装解码器
  6. 与 golink /console messageBox64bit.obj 链接
  7. 尝试在 WIN 7 64 上执行。

    每次该死的时候它都会崩溃 这里有什么问题吗?

编辑1:

在 Windbg 中 XOR 函数崩溃:

Access violation - code c0000005 (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
xordecoder2+0x1009:
00000000`00401009 8037aa          xor     byte ptr [rdi],0AAh ds:00000000`00401018=e7
0:000> t
ds:00000000`00401018=e7
0:000> t

编辑2。按照下面 Igor 的建议,我编辑了 EXE 以使 .text 部分可写。显然 .text 部分对于 Windows EXE 是不可写的。现在错误更改为:

    (1f08.1af0): Unknown exception - code c0000096 (first chance)
    (1f08.1af0): Unknown exception - code c0000096 (!!! second chance !!!)
    *** ERROR: Module load completed but symbols could not be loaded for C:\data_section_xorencoder7.exe
    data_section_xorencoder7+0x1016:
    00000000`00401016 e7ff            out     0FFh,eax

编辑 Nasm 代码。异或 shellcode。

    bits 64
    section .text
    global start

    start:
            jmp find_address

    decoder:
            pop rdi
            xor rcx, rcx
            add cx, 260
    decode:
            xor byte [rdi], 0xAA
            inc rdi
            loop decode

            jmp short encoded_shellcode

    find_address:
            call decoder


            encoded_shellcode: 0xe2,0x29,0x46,0x82,0xe2,0x29,0x4e,0x5a,0xcf,0xe6,0x21,0x8e,0x8f,0xca,0xaa,0xaa,0xaa,0xe7,0x21,0xce,0x8e,0xb2,0xe7,0x21,0xce,0x8e,0x8a,0xe7,0x21,0x8e,0x8e,0xe7,0x21,0xd6,0x8e,0x8a,0xe7,0x21,0x8e,0x8e,0xe7,0x21,0xce,0x8e,0x8a,0x10,0x24,0xe4,0xa4,0x46,0xe6,0x23,0x4b,0x42,0xc2,0xaa,0xaa,0xaa,0x41,0x9e,0xf3,0x55,0x7a,0x10,0x02,0x08,0xe7,0x16,0xe2,0x23,0x6b,0x42,0xfc,0xaa,0xaa,0xaa,0xe2,0x23,0x69,0xe7,0x9b,0x63,0x41,0x94,0xeb,0xf2,0x41,0x82,0xf0,0xe2,0x9b,0x63,0x55,0x79,0x10,0xda,0x67,0x95,0x87,0xe6,0x23,0x53,0x42,0x9d,0xaa,0xaa,0xaa,0xe2,0x9b,0x63,0x55,0x7a,0x42,0x6d,0x55,0x55,0x55,0xdf,0xd9,0xcf,0xd8,0x99,0x98,0x84,0xce,0xc6,0xc6,0x42,0x79,0x55,0x55,0x55,0xfe,0xc2,0xc3,0xd9,0x8a,0xc3,0xd9,0x8a,0xcc,0xdf,0xc4,0x8b,0xaa,0x42,0x17,0x55,0x55,0x55,0x9a,0xd2,0xce,0xcf,0xcb,0xce,0xc8,0xcf,0xcf,0xcc,0xe3,0x23,0x67,0xcd,0xeb,0x21,0xef,0x96,0xcd,0xef,0x21,0x1e,0xaf,0x22,0xaa,0xaa,0xaa,0xef,0xab,0x44,0xcd,0xef,0x21,0xfc,0xb2,0xcd,0xeb,0x21,0xf4,0x8a,0xee,0xab,0x41,0xcd,0x49,0x95,0xeb,0x55,0x60,0xcd,0xe8,0x21,0x9e,0x39,0xee,0xab,0x44,0x9b,0x55,0x9b,0x6a,0x56,0x06,0x2e,0x6a,0xde,0xad,0x6b,0x65,0xa7,0xab,0x6d,0x41,0x5e,0x93,0x7d,0xdf,0x77,0xcd,0xeb,0x21,0xf4,0x8e,0xee,0xab,0x41,0x9b,0x63,0xcc,0xcd,0xe8,0x21,0xa6,0xf9,0xcd,0xeb,0x21,0xf4,0xb6,0xee,0xab,0x41,0xcd,0x21,0xae,0x21,0xee,0xab,0x42,0x69
2个回答

您的代码几乎没有问题。

第一个是Igor提到的 - .text 部分是 RO。这在这个答案中得到了解决

第二个是您没有正确复制字节或以其他方式丢失了其中的一些字节。你的 shellcode 有 260 个字节,但是如果我为你提供链接编译示例,那么我得到 262。我通过一个简短的 python 脚本运行它们来对它们进行异或,然后我得到这个:

db 0xe2, 0x29, 0x46, 0x82, 0xe2, 0x29, 0x4e, 0x5a, 0xcf, 0xe6, 0x21, 0x8e, 0x8f, 0xca, 0xaa, 0xaa, 0xaa, 0xe7, 0x21, 0xce, 0x8e, 0xb2, 0xe7, 0x21, 0xce, 0x8e, 0x8a, 0xe7, 0x21, 0x8e, 0x8e, 0xe7, 0x21, 0xd6, 0x8e, 0x8a, 0xe7, 0x21, 0x8e, 0x8e, 0xe7, 0x21, 0xce, 0x8e, 0x8a, 0x10, 0x24, 0xe4, 0xa4, 0x46, 0xe6, 0x23, 0x4b, 0x42, 0xc2, 0xaa, 0xaa, 0xaa, 0x41, 0x9e, 0xf3, 0x55, 0x7a, 0x10, 0x2, 0x8, 0xe7, 0x16, 0xe2, 0x23, 0x6b, 0x42, 0xfc, 0xaa, 0xaa, 0xaa, 0xe2, 0x23, 0x69, 0xe7, 0x9b, 0x63, 0x41, 0x94, 0xeb, 0xf2, 0x41, 0x82, 0xf0, 0xe2, 0x9b, 0x63, 0x55, 0x79, 0x10, 0xda, 0x67, 0x95, 0x87, 0xe6, 0x23, 0x53, 0x42, 0x9d, 0xaa, 0xaa, 0xaa, 0xe2, 0x9b, 0x63, 0x55, 0x7a, 0x42, 0x6d, 0x55, 0x55, 0x55, 0xdf, 0xd9, 0xcf, 0xd8, 0x99, 0x98, 0x84, 0xce, 0xc6, 0xc6, 0xaa, 0x42, 0x79, 0x55, 0x55, 0x55, 0xfe, 0xc2, 0xc3, 0xd9, 0x8a, 0xc3, 0xd9, 0x8a, 0xcc, 0xdf, 0xc4, 0x8b, 0xaa, 0x42, 0x17, 0x55, 0x55, 0x55, 0x9a, 0xd2, 0xce, 0xcf, 0xcb, 0xce, 0xc8, 0xcf, 0xcf, 0xcc, 0xaa, 0xe3, 0x23, 0x67, 0xcd, 0xeb, 0x21, 0xef, 0x96, 0xcd, 0xef, 0x21, 0x1e, 0xaf, 0x22, 0xaa, 0xaa, 0xaa, 0xef, 0xab, 0x44, 0xcd, 0xef, 0x21, 0xfc, 0xb2, 0xcd, 0xeb, 0x21, 0xf4, 0x8a, 0xee, 0xab, 0x41, 0xcd, 0x49, 0x95, 0xeb, 0x55, 0x60, 0xcd, 0xe8, 0x21, 0x9e, 0x39, 0xee, 0xab, 0x44, 0x9b, 0x55, 0x9b, 0x6a, 0x56, 0x6, 0x2e, 0x6a, 0xde, 0xad, 0x6b, 0x65, 0xa7, 0xab, 0x6d, 0x41, 0x5e, 0x93, 0x7d, 0xdf, 0x77, 0xcd, 0xeb, 0x21, 0xf4, 0x8e, 0xee, 0xab, 0x41, 0x9b, 0x63, 0xcc, 0xcd, 0xe8, 0x21, 0xa6, 0xf9, 0xcd, 0xeb, 0x21, 0xf4, 0xb6, 0xee, 0xab, 0x41, 0xcd, 0x21, 0xae, 0x21, 0xee, 0xab, 0x42, 0x69

执行此操作后的结果是

在此处输入图片说明

完整代码:

bits 64
section .text
global start

start:
        jmp find_address

decoder:
        pop rdi
        xor rcx, rcx
        add cx, 262
decode:
        xor byte [rdi],0xAA
        inc rdi
        loop decode

        jmp short encoded_shellcode

find_address:
        call decoder


encoded_shellcode: 
       db 0xe2, 0x29, 0x46, 0x82, 0xe2, 0x29, 0x4e, 0x5a, 0xcf, 0xe6, 0x21, 0x8e, 0x8f, 0xca, 0xaa, 0xaa, 0xaa, 0xe7, 0x21, 0xce, 0x8e, 0xb2, 0xe7, 0x21, 0xce, 0x8e, 0x8a, 0xe7, 0x21, 0x8e, 0x8e, 0xe7, 0x21, 0xd6, 0x8e, 0x8a, 0xe7, 0x21, 0x8e, 0x8e, 0xe7, 0x21, 0xce, 0x8e, 0x8a, 0x10, 0x24, 0xe4, 0xa4, 0x46, 0xe6, 0x23, 0x4b, 0x42, 0xc2, 0xaa, 0xaa, 0xaa, 0x41, 0x9e, 0xf3, 0x55, 0x7a, 0x10, 0x2, 0x8, 0xe7, 0x16, 0xe2, 0x23, 0x6b, 0x42, 0xfc, 0xaa, 0xaa, 0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x69

我认为问题是可执行代码在 Windows 上默认是不可写的,因此在xor尝试修改代码时会失败您应该研究如何在程序集/链接时或运行时修改 .text 部分权限。

另一种解决方案可能是在解密之前将 shellcode 复制到可写内存,但这可能会导致相反的问题——它可能是不可执行的。

其它你可能感兴趣的问题