这是我写的AHK机器码生成器,不用设置复杂的编程环境,
只需一个热键就可以生成机器码(MCode),十分好用。
;===========================================
; AHK机器码生成器-v2.6 By FeiYue
;
; 使用方法:
;
; 1、下载安装【TDM-GCC】的64位版到D盘的TDM-GCC-64目录,下载网址为:
; https://sourceforge.net/projects/tdm-gcc/files/latest/download
;
; 2、下载安装【TCC】的32位和64位版到AHK的TCC-32和TCC-64目录,下载网址为:
; https://bellard.org/tcc/
;
; 3、选择C代码后,按【 Alt+C 】热键生成 GCC 编译的机器码,
; 或者按【 Ctrl+Alt+C 】热键生成 TCC 编译的机器码
;
;===========================================
s:="#include <windows.h>`n int aaa(){return sizeof(WINDOWPLACEMENT);}"
MsgBox % DllCall(Tcc(s).Ptr)
!c:: ; 选择C代码后用 GCC 编译
^!c:: ; 选择C代码后用 TCC 编译
Compile_Func() ; V1的热键函数必须有函数名
{
ClipSaved:=ClipboardAll, Clipboard:=""
Send % "{Ctrl Down}c{Ctrl Up}"
ClipWait, 3
s:=Clipboard, Clipboard:=ClipSaved
if (s="")
{
MsgBox, 4096, Tip, The contents of the copy are empty !
return
}
r:=[]
Loop 2
{
i:=A_Index-1, hex:=Tcc(s,i,A_ThisHotkey="!c").b64
, hex:=Trim(RegExReplace(hex,".{1,64}","`r`n . ""$0"""),"`r`n .")
, r[i]:="`r`n " (i?"x64:=":"x32:=") . StrReplace(hex,"/","@")
}
hex:=r[0] r[1] "`r`n MyFunc:=this.MCode(StrReplace((A_PtrSize=8?x64:x32),""@"",""/""))"
MsgBox, 4096, MCode has been generated! (32 + 64), % Clipboard:=hex
s:=hex:=r:=""
}
Tcc(args*)
{
static init, obj
if !VarSetCapacity(init) && (init:="1")
obj:=new TccClass()
return !args.Length() ? obj : obj.Tcc(args*)
}
Class TccClass
{ ;// Class Begin
Tcc(s:="", win64:="", gcc:=1, add:=0)
{
local
if (s="")
return
(win64="" && win64:=A_PtrSize=8)
if (gcc)
exe1:="D:\TDM-GCC-64\bin\gcc.exe"
else
exe1:=RegExReplace((!A_IsCompiled ? A_AhkPath : A_ScriptFullPath)
, "[^\\]+$", (win64 ? "TCC-64":"TCC-32") "\tcc.exe")
if !FileExist(exe1)
{
MsgBox, 4096, Tip, Can't Find %exe1% !
return
}
if (add)
{
r:=[]
Loop 4
r[A_Index]:="int _add" A_Index "_() { return 0x11111111; }"
s:=r[1] "`n" r[2] "`n" r[3] "`n" s "`n" r[4] "`n"
}
dir:=A_Temp, cpp:=dir "\~5.c", obj:=dir "\~5.obj", log:=dir "\~5.log"
For k,v in [cpp, obj, log]
Try FileDelete % v
FileAppend % StrReplace(s,"`r"), % cpp
size:=s:="", q:=Chr(34), arg:=(win64 ? " -m64 ":" -m32 ") " -O2 "
cmd:=q exe1 q " " arg " -c -o " q obj q " " q cpp q " 2>" q log q
RunWait % A_ComSpec " /c " q cmd q,, Hide
if FileExist(obj)
{
FileGetSize, size, % obj
FileRead, bin, % "*c " obj
}
Try FileRead, s, % log
For k,v in [cpp, obj, log]
Try FileDelete % v
if (!size || s)
{
MsgBox, 4096, Tip, % "C Compile Error`n`n" s
return
}
p:=&bin, hex:=""
if (add)
{
hex:=this.bin2hex(p,size,0)
p1:=RegExMatch(hex,"Oi)B811111111.{0,8}?C3",r), r:=r[0]
p2:=InStr(hex,r,0,p1+1), p3:=InStr(hex,r,0,p2+1)
p4:=InStr(hex " ",r,0,-1), len:=p3-p2, i:=0
Loop % (len-StrLen(r))//2
if !(SubStr(hex,p1-2-i,2)=SubStr(hex,p3-2-i,2)
&& SubStr(hex,p1-2-i,2)=SubStr(hex,p4-2-i,2) && (i+=2))
Break
if (p1 && p2 && p3)
hex:=SubStr(hex, p3+len-i, p4-p3-len)
}
else if (NumGet(p+0,"uchar")=0x7f && StrGet(p+1,3,"CP0")="ELF")
{ ; TCC use ELF
r:=(NumGet(p+4,"char")=2?8:4), ptr:=(r=8?"uint64":"uint")
e_shoff:=NumGet(p+24+2*r,ptr)
e_shentsize:=NumGet(p+34+3*r,"ushort")
e_shstrndx:=NumGet(p+38+3*r,"ushort")
sh:=e_shoff+e_shstrndx*e_shentsize
str_offset:=NumGet(p+sh+8+2*r,ptr)
Loop % NumGet(p+36+3*r,"ushort")
{
sh:=e_shoff+(A_Index-1)*e_shentsize
name:=StrGet(p+str_offset+NumGet(p+sh,"uint"),8,"CP0")
if (name=".text") && (NumGet(p+sh+8,ptr) & 0x4)
{
offset:=NumGet(p+sh+8+2*r,ptr), size:=NumGet(p+sh+8+3*r,ptr)
hex:=this.bin2hex(p+offset,size,0)
Break
}
}
}
else ; GCC use COFF
{
base:=p
if (StrGet(p,2,"CP0")="MZ" && StrGet(p+NumGet(p+0x3C,"uint"),4,"CP0")="PE")
p:=p+NumGet(p+0x3C,"uint")+4
Loop % NumGet(p+2,"ushort")
{
sh:=20+NumGet(p+16,"ushort")+(A_Index-1)*40
if (StrGet(p+sh,8,"CP0")=".text") && (NumGet(p+sh+36,"uint") & 0x20)
{
offset:=NumGet(p+sh+20,"uint"), size:=NumGet(p+sh+16,"uint")
hex:=this.bin2hex(base+offset,size,0)
Break
}
}
}
if !hex
return
code:=this.MCode(hex), code.hex:=hex
, code.b64:=this.bin2hex(code.Ptr,code.Size,1)
return this.code:=code
}
Buffer(size, FillByte:="")
{
local
buf:={}, buf.SetCapacity("_key", size), p:=buf.GetAddress("_key")
, (FillByte!="" && DllCall("RtlFillMemory","Ptr",p,"Ptr",size,"uchar",FillByte))
, buf.Ptr:=p, buf.Size:=size
return buf
}
MCode(hex)
{
local
flag:=((hex~="[^A-Fa-f\d\s]") ? 1:4), len:=0
Loop 2
if !DllCall("crypt32\CryptStringToBinary", "Str",hex, "uint",0, "uint",flag
, "Ptr",(A_Index=1?0:(p:=this.Buffer(len)).Ptr), "uint*",len, "Ptr",0, "Ptr",0)
return
if DllCall("VirtualProtect", "Ptr",p.Ptr, "Ptr",len, "uint",0x40, "uint*",0)
return p
}
bin2hex(addr, size, base64:=0)
{
local
flag:=(base64 ? 1:4)|0x40000000, len:=0
Loop 2
DllCall("crypt32\CryptBinaryToString", "Ptr",addr, "uint",size, "uint",flag
, "Ptr",(A_Index=1?0:(p:=this.Buffer(len*2)).Ptr), "uint*",len)
return RegExReplace(StrGet(p.Ptr, len), "\s+")
}
} ;// Class End