【问题标题】:How to get the executable given only the hex code?仅给定十六进制代码,如何获取可执行文件?
【发布时间】:2013-08-31 03:55:58
【问题描述】:

我正在学习 Windows 汇编语言,masm 作为我的汇编器,link 作为我的链接器。我拿了以下汇编代码并获得了exe

.386                                                                                 
.model flat, stdcall                                                                 
option casemap :none                                                                 

extrn MessageBoxA@16 : PROC                                                          
extrn ExitProcess@4  : PROC                                                          

.code                                                                                
start:                                                                               
        mov eax, 0                                                                   
        push eax                                                                     
        jmp msg                                                                      
pgm:    pop ebx                                                                      
        push ebx                                                                     
        push ebx                                                                     
        push eax                                                                     
        call MessageBoxA@16                                                          
        push eax                                                                     
        call ExitProcess@4                                                           
msg:    call pgm                                                                     
        db "KingKong",0                                                              
end start

C:\Arena>ml /c /coff  a.asm                                                         
Microsoft (R) Macro Assembler Version 10.00.30319.01                                
Copyright (C) Microsoft Corporation.  All rights reserved.                          

 Assembling: a.asm   

C:\Arena>link /subsystem:windows /defaultlib:kernel32 /defaultlib:user32 a.obj      
Microsoft (R) Incremental Linker Version 10.00.30319.01                             
Copyright (C) Microsoft Corporation.  All rights reserved. 

程序运行良好并显示消息框,现在我运行objdump -d a.exe 并获取shellcode 并将其插入以获取可执行文件

.386
.model flat, stdcall
option casemap :none

extrn MessageBoxA@16 : PROC
extrn ExitProcess@4  : PROC

.code
start:
db 0xb8,0x00,0x00,0x00,0x00,0x50,0xeb,0x0f,0x5b,0x53,0x53,0x50,0xe8,0x1b,0x00,0x00,0x00,0x50,0xe8,0x0f,0x00,0x00,0x00,0xe8,0xec,0xff,0xff,0xff,0x4b,0x69,0x6e,0x67,0x4b,0x6f,0x6e,0x67,0x00,0xcc,0xff,0x25,0x00,0x20,0x40,0x00,0xff,0x25,0x08,0x20,0x40,0x00
end start

但是当我尝试组装它时,我得到了

C:\Arena>ml /c /coff b.asm                                                          
Microsoft (R) Macro Assembler Version 10.00.30319.01                                
Copyright (C) Microsoft Corporation.  All rights reserved.                          

 Assembling: b.asm                                                                  
b.asm(10) : error A2042:statement too complex 

我能够在 linux 上使用 hexdump 取回可执行文件,并且该线程是 here。我现在只需要使用我在 Windows 上获得的 hexdump 来取回可执行文件。我该怎么做?

编辑拆分字节是个好建议,我有

.386                                                                                 
.model flat, stdcall                                                                 
option casemap :none                                                                 

extrn MessageBoxA@16 : PROC                                                          
extrn ExitProcess@4  : PROC                                                          

.code                                                                                
start:                                                                               
db 0xb8,0x00,0x00,0x00,0x00,0x50,0xeb,0x0f                                           
db 0x5b,0x53,0x53,0x50,0xe8,0x1b,0x00,0x00                                           
db 0x00,0x50,0xe8,0x0f,0x00,0x00,0x00,0xe8                                           
db 0xec,0xff,0xff,0xff,0x4b,0x69,0x6e,0x67                                           
db 0x4b,0x6f,0x6e,0x67,0x00,0xcc,0xff,0x25                                           
db 0x00,0x20,0x40,0x00,0xff,0x25,0x08,0x20                                           
db 0x40,0x00                                                                         
end start 

但是当我将它提供给汇编器时,我得到了

C:\Arena>ml a.asm                                                                   
Microsoft (R) Macro Assembler Version 10.00.30319.01                                
Copyright (C) Microsoft Corporation.  All rights reserved.                          

 Assembling: a.asm                                                                  
a.asm(10) : error A2206:missing operator in expression                              
a.asm(11) : error A2206:missing operator in expression                              
a.asm(12) : error A2206:missing operator in expression                              
a.asm(13) : error A2206:missing operator in expression                              
a.asm(14) : error A2206:missing operator in expression                              
a.asm(15) : error A2206:missing operator in expression                              
a.asm(16) : error A2206:missing operator in expression

我如何取回仅给出十六进制代码的可执行文件?

【问题讨论】:

    标签: windows assembly binary hex masm


    【解决方案1】:

    MessageBoxA 和 ExitProcess 的解决方法应该是这样的(不幸的是我通常使用 GNU 汇编器所以我不知道这里的语法是否正确):

      db 0b8h, 000h, 000h, 000h, 000h, 050h, 0ebh, 00fh
      db 05bh, 053h, 053h, 050h, 0e8h
      dd MessageBoxA@16 - addr1
    addr1:
      db 050h, 0e8h
      dd ExitProcess@4 - addr2
    addr2:
      db 0e8h, 0ech, 0ffh, 0ffh, 0ffh, 04bh, 069h, 06eh
    ...
    

    您不能“只”使用十六进制。当你不知道你调用的函数的地址时的代码!

    也许 MASM 甚至不允许使用“dd”伪指令创建相对地址。在这种情况下,您有一种可能性:

    mov ECX, MessageBoxA@16
    call ECX
    

    这将产生一个绝对地址(“调用”使用相对地址),因此当转换为十六进制代码时,文件将如下所示:

    db 0b9h
    dd MessageBoxA@16
    db 0ffh, 0d1h
    

    【讨论】:

      【解决方案2】:

      我很确定 masm 有一个行长限制(内存中的 255 个,至少对于我使用的版本而言)。

      您需要将那个大喇叭db 语句拆分为多个。换句话说,执行类似的操作(您还会注意到我已将内容更改为我喜欢的样式 - 我不知道 masm 是否接受 C 样式的十六进制数字,我从未使用过它们):

      .code
      start:
          db 0b8h,  00h,  00h,  00h,  00h,  50h, 0ebh,  0fh
          db  5bh,  53h,  53h,  50h, 0e8h,  1bh,  00h,  00h
          ; and so on
      

      但是,我不完全相信对MessageBoxExitProcess 的外部调用将适用于基于字节的解决方案,您需要检查一下。

      【讨论】:

      • 看看我的编辑,我尝试了您的建议,但仍然无法正常工作!!!,请您为MessageBoxExitProcess 提出一些解决方法
      • @vikkyhacks,缩进你的db 语句并尝试更改格式。
      猜你喜欢
      • 2011-10-25
      • 2012-04-15
      • 2013-07-28
      • 1970-01-01
      • 2022-01-20
      • 2016-03-06
      • 1970-01-01
      • 2011-11-20
      相关资源
      最近更新 更多