VirtualAlloc分配虚拟内存 执行其中动态加入的代码

来源:互联网 发布:淘宝利用老顾客刷销量 编辑:程序博客网 时间:2024/05/22 08:16
利用VirtualAlloc分配虚拟内存,并指定这块内存为可读写并可执行,然后在该块内存中加入代码(机器语言),利用嵌入ASM跳转到刚才分配的内存地址,执行其中动态加入的代码

相应汇编代码对应的机器代码
call   :   $E8
xor   eax,   eax   :   $33C0
ret   :   $C3

type
    PCode   =   ^TCode;
    TCode   =   packed   record
        Code:   array[0..2]   of   Byte;
        Prc:   Pointer;
        Ret:   Byte;
    end;
const
    BlockCode:   array[0..2]   of   Byte   =   ($33,$C0,$E8);
var
    Block:PCode;

Block:=VirtualAlloc(nil,   7,   MEM_COMMIT,   PAGE_EXECUTE_READWRITE);
Move(BlockCode,   Block^.Code,   SizeOf(BlockCode));
Block^.Prc:=Pointer(Longint(@Showmessage)   -   (Longint(@Block^.Code[2])   +   5));//计算Showmessage的相对地址
Block^.Ret:=$C3;

//执行
asm
call   Block
end;
原创粉丝点击