Autohotkey 中文论坛
AHK源代码加密器 v2.0 - 可打印的版本

+- Autohotkey 中文论坛 (http://ahkcn.net)
+-- 版块: AutoHotkey (/forum-3.html)
+--- 版块: 作品展示 (/forum-7.html)
+--- 主题: AHK源代码加密器 v2.0 (/thread-6588.html)



AHK源代码加密器 v2.0 - feiyue - 2017-12-09 13 : 43

这是AHK源代码加密工具,作为第一层加密,之后可以用Ahk2Exe编译为程序。
由于AHK官方支持开源,结果Ahk2Exe编译的程序(不使用mpress压缩),
在程序末尾明文保存了源代码,不利于发布私有成果。即使压缩、加壳也有
对应的脱壳工具。这个工具可以使Ahk2Exe生成的程序被破解的源代码已加密。Shy

代码:
/*
;-------------------------------
  AHK源代码加密器 v2.0  By FeiYue

  脚本说明:

  1、实现方式为机器码内存解码,然后直接发送到AHK主程序执行。

  2、本工具只是将AHK脚本转换为加密后的自解压脚本。
     之后可用Ahk2Exe结合mpress、upx加壳来生成程序。

  3、脚本生成的程序必须在同目录存在AutoHotkey.exe主程序。
     如果将AutoHotkey.exe改名为abc.exe,必须修改代码中
     的“Ahk=”为“Ahk=%A_ScriptDir%\abc.exe”。

  4、由于内存解码后的代码直接发送到AHK主程序执行,不生成
     磁盘源文件,所以Reload命令无效,可以用Reload()代替。

;-------------------------------
*/

#NoEnv
#SingleInstance force
ListLines, Off
SetBatchLines, -1

fs=
(` %

Exec(str, Ahk="", arg="") {
  static WriteFile, h, ScriptName:=%True%
  s:=RegExReplace(str,"\s"), StrReplace(s,"u","",size)
  VarSetCapacity(str,(size+1)*4,0), s:=SubStr(s,InStr(s,"u")+1)
  Loop, Parse, s, u
    NumPut(A_LoopField, str, (A_Index-1)*4, "uint")
  ;-----------------------------
  Ptr:=A_PtrSize ? "UPtr":"UInt"
  Ahk:=Ahk ? Ahk : A_IsCompiled ? A_ScriptDir "\AutoHotkey.exe":A_AhkPath
  IfNotExist, %Ahk%
  {
    MsgBox, 4096, Error!, `n`nCan't Find: %Ahk% !`n`n
    return, 0
  }
  Random, n, 1, 1000000
  name:="\\.\pipe\AHK" . A_TickCount . n
  Loop, 2
    if (p%A_Index% := DllCall("CreateNamedPipe","str",name,"uint",2
    ,"uint",0,"uint",255,"uint",0,"uint",0,Ptr,0,Ptr,0))=-1
      return, 0
  ScriptName:=ScriptName ? ScriptName : A_ScriptFullPath
  Run, %Ahk% "%name%" "%ScriptName%" %arg%
  DllCall("ConnectNamedPipe", Ptr,p1, Ptr,0)
  DllCall("CloseHandle", Ptr,p1)
  DllCall("ConnectNamedPipe", Ptr,p2, Ptr,0)
  if !WriteFile
  {
    x32:="5589E581ECA8000000C745F0000000008B450883C03C8B1"
    . "08B450801D08945E88B45080FB6003C4D75248B450883C0010"
    . "FB6003C5A75178B45E80FB6003C50750D8B45E883C0010FB60"
    . "03C45740AB8FFFFFFFFE92E0300008B45E883C0648B0085C07"
    . "40C8B45E883C0608B0085C07509C745F488000000EB07C745F"
    . "4780000008B55E88B45F401D08B108B450801D08945E48B45E"
    . "48B40188945E08B45E483C01C8B108B450801D08945DC8B45E"
    . "483C0208B108B450801D08945D88B45E483C0248B108B45080"
    . "1D08945D4C745F400000000EB618B45F48D1485000000008B4"
    . "5D801D08B008945D08B55088B45D001D08B003D47657450753"
    . "88B45D08D50048B450801D08B003D726F634175248B45F48D1"
    . "4008B45D401D00FB7000FB7C08D1485000000008B45DC01D08"
    . "B008945F0EB0C8345F4018B45F43B45E07297837DF000750AB"
    . "8FEFFFFFFE9390200008B55088B45F001D08945CCC745AA577"
    . "26974C745AE6546696C66C745B26500C7459E476C6F62C745A"
    . "2616C416CC745A66C6F6300C74593476C6F62C74597616C467"
    . "266C7459B6565C6459D008D45AA894424048B45088904248B4"
    . "5CCFFD08945C8C7442410000000008B45188944240CC744240"
    . "8000000008D45AA89442404C70424000000008B45C8FFD085C"
    . "0740AB8FDFFFFFFE9A0010000C7442410000000008B4518894"
    . "4240CC7442408000000008D45AA894424048B450C8904248B4"
    . "5C8FFD085C0750AB8FCFFFFFFE9690100008D459E894424048"
    . "B45088904248B45CCFFD08945C48D4593894424048B4508890"
    . "4248B45CCFFD08945C08B451483C001C1E00289442404C7042"
    . "4000000008B45C4FFD08945BC837DBC00750AB8FBFFFFFFE91"
    . "3010000C745800B000000C745840D000000C7458811000000C"
    . "7458C13000000C745EC00000000EB268B45EC83E0038945B88"
    . "B45B88B44858069D0830000008B45EC01C28B45B8895485808"
    . "345EC01837DEC6376D4C745EC00000000EB5A8B45EC83E0038"
    . "945B88B45B88B44858069D0830000008B45EC01C28B45B8895"
    . "485808B45EC8D1485000000008B451001D08B108B45B88B448"
    . "58031D08945B48B45EC8D1485000000008B45BC01D08B55B4C"
    . "1CA0889108345EC018B45EC3B4514729E8B45EC8D148500000"
    . "0008B45BC01D0C700000000008B451483C0018D14850000000"
    . "0C7442410000000008B45188944240C895424088B45BC89442"
    . "4048B450C8904248B45C8FFD08B45BC8904248B45C0FFD0B80"
    . "0000000C9C214009090"
    x64:="554889E54881ECF000000048894D10488955184C8945204"
    . "4894D28C745F800000000488B45104883C03C8B0089C2488B4"
    . "5104801D0488945E8488B45100FB6003C4D7529488B4510488"
    . "3C0010FB6003C5A751A488B45E80FB6003C50750F488B45E84"
    . "883C0010FB6003C45740AB8FFFFFFFFE996030000488B45E84"
    . "883C0648B0085C0740E488B45E84883C0608B0085C07509C74"
    . "5FC88000000EB07C745FC780000008B55FC488B45E84801D08"
    . "B0089C2488B45104801D0488945E0488B45E08B40188945DC4"
    . "88B45E04883C01C8B0089C2488B45104801D0488945D0488B4"
    . "5E04883C0208B0089C2488B45104801D0488945C8488B45E04"
    . "883C0248B0089C2488B45104801D0488945C0C745FC0000000"
    . "0EB6F8B45FC488D148500000000488B45C84801D08B008945B"
    . "C8B55BC488B45104801D08B003D4765745075418B45BC488D5"
    . "004488B45104801D08B003D726F6341752A8B45FC488D14004"
    . "88B45C04801D00FB7000FB7C0488D148500000000488B45D04"
    . "801D08B008945F8EB0C8345FC018B45FC3B45DC7289837DF80"
    . "0750AB8FEFFFFFFE972020000488B55108B45F84801D048894"
    . "5B048B8577269746546696C48898570FFFFFF66C78578FFFFF"
    . "F650048B8476C6F62616C416C48898560FFFFFFC78568FFFFF"
    . "F6C6F630048B8476C6F62616C467248898550FFFFFF66C7855"
    . "8FFFFFF6565C6855AFFFFFF00488D9570FFFFFF488B45B0488"
    . "B4D10FFD0488945A8488B4D30488D9570FFFFFF48C74424200"
    . "0000000488B45A84989C941B800000000B900000000FFD085C"
    . "0740AB8FDFFFFFFE9C30100004C8B4530488D9570FFFFFF488"
    . "B4D1848C744242000000000488B45A84D89C141B800000000F"
    . "FD085C0750AB8FCFFFFFFE98E010000488D9560FFFFFF488B4"
    . "5B0488B4D10FFD0488945A0488D9550FFFFFF488B45B0488B4"
    . "D10FFD0488945988B452883C001C1E00289C04889C2488B45A"
    . "0B900000000FFD04889459048837D9000750AB8FBFFFFFFE93"
    . "6010000C78540FFFFFF0B000000C78544FFFFFF0D000000C78"
    . "548FFFFFF11000000C7854CFFFFFF13000000C745F40000000"
    . "0EB2C8B45F483E00389458C8B458C8B848540FFFFFF69D0830"
    . "000008B45F401C28B458C89948540FFFFFF8345F401837DF46"
    . "376CEC745F400000000EB698B45F483E00389458C8B458C8B8"
    . "48540FFFFFF69D0830000008B45F401C28B458C89948540FFF"
    . "FFF8B45F4488D148500000000488B45204801D08B108B458C8"
    . "B848540FFFFFF31D08945888B45F4488D148500000000488B4"
    . "5904801D08B5588C1CA0889108345F4018B45F43B4528728F8"
    . "B45F4488D148500000000488B45904801D0C700000000008B4"
    . "52883C001448D1485000000004C8B4530488B5590488B4D184"
    . "8C744242000000000488B45A84D89C14589D0FFD0488B55904"
    . "88B45984889D1FFD0B8000000004881C4F00000005DC390"
    hex:=A_PtrSize=8 ? x64:x32
    VarSetCapacity(WriteFile, len:=StrLen(hex)//2)
    Loop, % len
      NumPut("0x" SubStr(hex,2*A_Index-1,2),WriteFile,A_Index-1,"uchar")
    DllCall("VirtualProtect",Ptr,&WriteFile,Ptr,len,"uint",0x40,Ptr "*",0)
    h:=DllCall("GetModuleHandle", "Str","Kernel32", Ptr)
  }
  DllCall(&WriteFile,Ptr,h,Ptr,p2,Ptr,&str,"uint",size,"uint*",0)
  DllCall("CloseHandle", Ptr,p2)
  return, 1
}

)

; 支持命令行,拖动脚本到加密器的程序图标上也能加密
if 0>0
{
  file:=%True%
  IfExist, %file%
    Gosub, 加密
  ExitApp
}
Gui, +AlwaysOnTop +ToolWindow
Gui, Color, DDEEFF
Gui, Font, cRed s28
Gui, Add, Text,, AHK脚本拖动到本窗口即可加密`n`n
Gui, Show,, AHK源代码加密器 v2.0  -  By FeiYue
OnMessage(0x201, "LButton_Down")
return

LButton_Down() {
  ListLines, Off
  if (A_Gui=1 and A_GuiControl="")
    SendMessage, 0xA1, 2
}

GuiClose:
ExitApp

GuiDropFiles:
Gui, +OwnDialogs
Loop, parse, A_GuiEvent, `n
{
  file:=A_LoopField
  MsgBox, 4100, 提示, 这个文件要加密吗?`n`n%file%
  IfMsgBox, Yes
    Gosub, 加密
  MsgBox, 4096, 提示, 加密成功!
}
return

加密:
FileRead, s, %file%
s:="`n;请自行采用Ahk2Exe.exe压缩编译并设定图标`n"
  . "`n;如果将AutoHotkey.exe改名为abc.exe,"
  . "请修改Ahk=%A_ScriptDir%\abc.exe`n"
  . "`n#NoEnv"
  . "`n#NoTrayIcon"
  . "`n#SingleInstance off"
  . "`nSetBatchLines, -1"
  . "`nAhk=`n"
  . "`ns=`n"
  . RegExReplace(JiaMi(s),".{1,60}","s.=""$0""`n")
  . "`nExec(s, Ahk)"
  . "`nExitApp`n"
  . fs
f:=RegExReplace(file,"\.[^.]+$") . "-加密.ahk"
FileDelete, %f%
FileAppend, %s%, %f%
return

JiaMi(s) {
  static MyFunc
  Ptr:=A_PtrSize ? "UPtr":"UInt"
  if !MyFunc
  {
    x32:="5589E583EC20C745E40B000000C745E80D000000C745EC1"
    . "1000000C745F013000000C745FC00000000EB268B45FC83E00"
    . "38945F88B45F88B4485E469D0830000008B45FC01C28B45F88"
    . "95485E48345FC01837DFC6376D4C745FC00000000EB5C8B45F"
    . "C83E0038945F88B45F88B4485E469D0830000008B45FC01C28"
    . "B45F8895485E48B45FC8D1485000000008B450801D08B00894"
    . "5F48B45FC8D1485000000008B450801C28B45F4C1C00889C18"
    . "B45F88B4485E431C889028345FC018B450C3945FC729CB8000"
    . "00000C9C20800909090"
    x64:="554889E54883EC2048894D10895518C745E00B000000C74"
    . "5E40D000000C745E811000000C745EC13000000C745FC00000"
    . "000EB268B45FC83E0038945F88B45F88B4485E069D08300000"
    . "08B45FC01C28B45F8895485E08345FC01837DFC6376D4C745F"
    . "C00000000EB628B45FC83E0038945F88B45F88B4485E069D08"
    . "30000008B45FC01C28B45F8895485E08B45FC488D148500000"
    . "000488B45104801D08B008945F48B45FC488D1485000000004"
    . "88B45104801C28B45F4C1C00889C18B45F88B4485E031C8890"
    . "28345FC018B45183945FC7296B8000000004883C4205DC390"
    hex:=A_PtrSize=8 ? x64:x32
    VarSetCapacity(MyFunc, len:=StrLen(hex)//2)
    Loop, % len
      NumPut("0x" SubStr(hex,2*A_Index-1,2),MyFunc,A_Index-1,"uchar")
    DllCall("VirtualProtect", Ptr,&MyFunc, Ptr,len, "uint",0x40, Ptr "*",0)
  }
  add=
  (%
;-----------------------
ListLines, Off
#NoEnv
ScriptName:=%True%
Try Menu, Tray, Icon, %ScriptName%
Gui, SingleInstance_force:Show, Hide, [%ScriptName%]
Gui, SingleInstance_force:+Hwndmyid
DetectHiddenWindows, On
WinGet, list, List, [%ScriptName%] ahk_class AutoHotkeyGUI
Loop, % list {
  IfEqual, myid, % id:=list%A_Index%, Continue
  WinGet, pid, PID, ahk_id %id%
  WinClose, ahk_class AutoHotkey ahk_pid %pid%
  WinWaitClose, ahk_id %id%
}
DetectHiddenWindows, Off
;-----------------------
Reload() {
  static ScriptName:=%True%
  Try {
    if ScriptName=
      return
    else if InStr(ScriptName,".exe")
      Run, %ScriptName%
    else
      Run, %A_AhkPath% "%ScriptName%"
    ExitApp
  }
}
ListLines, On
;-----------------------
  )
  s:=add "`n" s "`nExitApp`n#SingleInstance off`n"
  s:=(A_IsUnicode ? chr(0xfeff) : chr(0xEF) chr(0xBB) chr(0xBF))
    . RegExReplace(s,"\R","`r`n") . "`t`t`t"
  size:=VarSetCapacity(s,-1)//4
  VarSetCapacity(str, size*11*(!!A_IsUnicode+1))
  DllCall(&MyFunc, Ptr,&s, "int",size)
  Loop, % size
    str.="u" . NumGet(s,(A_Index-1)*4,"uint")
  return, str
}

;======== 脚本结束 ========

;



RE: AHK源代码加密器 v1.0 - jk1999 - 2018-01-10 20 : 50

你好。我试用了下。加密之后再用Ahk2Exe编译后,文件夹下必须有AutoHotkey.exe才能运行。
然后加密文件的注释里有一句:如果不想采用AutoHotkey.exe请修改Ahk=
可我不知道改哪里好。
能否有个怎么改AHK的说明?


RE: AHK源代码加密器 v1.0 - feiyue - 2018-01-11 11 : 08

由于是调用AHK主程序(AutoHotkey.exe)动态运行,所以同目录下必须有AHK主程序。
如果将AutoHotkey.exe改名为abc.exe,请修改Ahk=%A_ScriptDir%\abc.exe


RE: AHK源代码加密器 v1.5 - xinyu3ru - 2018-01-14 14 : 54

就是想留个名,ahk8挂了之后也没有太在意,找的时候真的是转了一大圈才找到这里,还好好像所有数据都在


RE: AHK源代码加密器 v1.5 - xsm121 - 2018-03-06 17 : 32

不错!支持楼主!Ahk就是这点不好,有ahk2EXE,就有EXE2ahk。感觉没有可靠的方法加密。


RE: AHK源代码加密器 v2.0 - feiyue - 2018-05-14 22 : 51

更新到v2.0版,大大增强了防破解能力。欢迎大家试用。

有人说通过任务管理器的创建转储文件,再解析转储文件中的字符串,可以得到内存中的源代码,
我实验了一下,确实可以解析出一些零碎的代码,但是非常缺漏和不连贯,难以得到完整的源代码。