1
votes

I want to cross compile a Metasploit template (in assembly language) for 64 bit Windows on Kali Linux.

I am trying to compile from assembly to exe.

Here is the code -

; Author: Stephen Fewer (stephen_fewer[at]harmonysecurity[dot]com)
; Architecture: x64
;
; Assemble and link with the following command:
; "C:\Program Files\Microsoft Visual Studio 9.0\VC\bin\x86_amd64\ml64" template_x64_windows.asm /link /subsystem:windows /defaultlib:"C:\Program Files\Microsoft SDKs\Windows\v6.0A\Lib\x64\kernel32.lib" /entry:main 

extrn ExitProcess : proc
extrn VirtualAlloc : proc

.code

    main proc 
        sub rsp, 40        ;
        mov r9, 40h        ; 
        mov r8, 3000h      ; 
        mov rdx, 4096      ; 
        xor rcx, rcx       ; 
        call VirtualAlloc  ; lpPayload = VirtualAlloc( NULL, 4096, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE );
        mov rcx, 4096      ;
        mov rsi, payload   ;
        mov rdi, rax       ;
        rep movsb          ; memcpy( lpPayload, payload, 4096 );
        call rax           ; lpPayload();
        xor rcx, rcx       ;
        call ExitProcess   ; ExitProcess( 0 );
    main endp

    payload proc
        A byte 'PAYLOAD:'
        B db 4096-8 dup ( 0 )
    payload endp
end

I am using w64-mingw32, and I can compile 32 bit c files with the command i686-mingw32msvc-gcc xxx, but I am having trouble with compiling 64 bit assembly.

There are so many different options for w64-mingw32, I have tried numerous, all with error messages. To be honest, I haven't done assembly since I had a Commodore 64.

I tried:

x86_64-w64-mingw32-gcc -c template_x64_windows.asm -o file.o

And the error message is -

template_x64_windows.asm: linker input file unused because linking not done

I don't even know which option will compile assembly, I have checked online for howt-o's, nothing.

1
w64-mingw32 should take the same command line options as 32-bit MinGW. Are you trying to compile to assembly, or compile a .S asm source file to machine code? gcc -c foo.s just works, if your asm is written in GAS syntax. If not, you can't assemble it with gcc. We can't do any more than guess without seeing the source you're trying to build, or any error messages, or any of the command line options you tried. This isn't a minimal reproducible example of the problem you're having. - Peter Cordes
That's MASM source code. No version of gcc / mingw will assemble it. Compile it with MASM or maybe JWASM I think is compatible, or replace the directives with GAS directives and use .intel_syntax noprefix. Or port it to NASM directives. - Peter Cordes
Thank you very much. Worked as advertised. How do I close the thread? Thanks again folks. - Monkeybus

1 Answers

1
votes

You can install JWASM (a reasonably compatible MASM assembler) to assemble the 64-bit code. Get the source code with GIT, build and install it:

git clone https://github.com/JWasm/JWasm.git 
cd JWasm 
cmake . 
cp jwasm /usr/local/bin 

To install you may have to run the last command with sudo on Ubuntu based distros.

To assemble your code to a 64-bit COFF object file (.o) you use:

jwasm -win64 template_x64_windows.asm

Then build a Win64 PE executable using:

x86_64-w64-mingw32-gcc template_x64_windows.o -o file.exe