delphiassemblyshellcodebasm

How do I get the machine code of an assembly instruction known at compile time?


I want to be able to convert a single line of ASM into shellcode. I.E:

CALL EBX

How do I go about doing this, and also being able to properly convert this shellcode so that I can store it in a variable in a delphi application. I.E:

var ShellCodeArray:  array[0..3] of Byte = ($55,$8B,$EC,$81);

Solution

  • If I get you right, you want to obtain a machine code of a single assembler instruction CALL EBX using Delphi built-in assembler.

    function CodeSize: Integer;
    asm
        lea EAX, @@end
        lea EDX, @@start
        sub EAX, EDX
        JMP @@end
    @@start:
        call EBX
    @@end:
    end;
    
    procedure Code;
    asm
        call EBX
    end;
    
    function CodeToBytes: TBytes;
    var
      I, N: Integer;
      P: PByte;
    
    begin
      N:= CodeSize;
      SetLength(Result, N);
      P:= @Code;
      for I:= 0 to N - 1 do begin
        Result[I]:= P^;
        Inc(P);
      end;
    end;