Page 1 of 4

AHK源代码加密器 v3.0

Posted: 06 Jan 2018, 17:33
by feiyue
在利用AHK解决实际问题时,有时想保护自己的劳动成果,不想直接给其他人看到。
但是当前的Ahk2Exe不能加密,源代码是明文附加在程序末尾的,起不到保护源代码的作用,
所以我制作了一个简单的AHK源代码加密器,可以防止小白,对高手没有什么作用。

Code: Select all

/*
;-------------------------------
  AHK Source Code Encryptor v3.0  By FeiYue

  1. This tool can encrypt the AHK script into a self decode script.

  2. Then you can use ahk2exe to compile the script into a program,
     combined with mpress.exe or upx.exe packers.
     Note: To compile the script, you must have a AutoHotkey.exe
     in the script directory.

  3. When you want to use the directory relative to the script(eg 001.jpg),
     Don't use A_ScriptDir, but use A_WorkingDir. It is recommended to backup
     the working directory to a variable at the beginning of the script,
     Then change the path to absolute path based on the variable (eg %dir%\001.jpg).

  4. When you want to Reload your own script, the built-in Reload command will fail,
     You can use Reload() function instead (it's added when encrypted).

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

#NoEnv
#SingleInstance force
ListLines, Off
SetBatchLines, -1
Version = 3.0

fs=
(` %

; You can compile and set icons by using Ahk2Exe.exe.
; If AutoHotkey.exe wants to change its name to abc.exe,
; please modify Ahk=%A_ScriptDir%\abc.exe.

#NoEnv
#NoTrayIcon
#SingleInstance off
SetBatchLines, -1
if RegExMatch(%True%, "i)kill:\K\w+", pid)
{
  Process, WaitClose, %pid%, 3
  Process, Close, %pid%
  ExitApp
}
Ahk=%A_ScriptDir%\AutoHotkey.exe
if (A_IsCompiled)
  FileInstall, AutoHotkey.exe, %Ahk%
Exec(s, Ahk)
ExitApp

Exec(str, Ahk="", args="") {
  static MyFunc, base
  Ptr:=A_PtrSize ? "UPtr" : "UInt"
  AStr:=A_IsUnicode ? "AStr" : "Str"
  Ahk:=Ahk ? Ahk : A_ScriptDir "\AutoHotkey.exe"
  Ahk:=InStr(Ahk,"\\.\pipe\") ? A_AhkPath : Ahk
  IfNotExist, %Ahk%
  {
    MsgBox, 4096, Error!, `nCan't Find:`n`n%Ahk%`n`t
    return
  }
  if !MyFunc
  {
    x32:="5557565381ECDC0100008BBC24F80100008BAC24FC01000"
    . "08B473C01F88038500F85270A0000807801450F851D0A00008"
    . "B4860BA8800000085C90F85E20900008B341001FE8B4E188B5"
    . "62085C90F84BD09000031C08D1C17EB118DB60000000083C00"
    . "139C10F84A50900008B1483813C174765745075E9817C17047"
    . "26F634175DF8D04470346240FB7008D048703461C8B1885DB0"
    . "F84760900008D842496000000BE6500000001FB6689B4249E0"
    . "00000893C24C784249600000057726974C784249A000000654"
    . "6696C89442404FFD383EC08894424388D8424AB000000893C2"
    . "4C78424AB000000476C6F62C78424AF000000616C416CC7842"
    . "4B30000006C6F630089442404FFD383EC0889C6B8656500006"
    . "6898424A80000008D8424A0000000893C24C78424A00000004"
    . "76C6F62C78424A4000000616C4672C68424AA0000000089442"
    . "404FFD383EC088944244CB873410000893C2466898424F5000"
    . "0008D8424E9000000C78424E900000043726561C78424ED000"
    . "00074655072C78424F10000006F636573C68424F7000000008"
    . "9442404FFD383EC08894424508D84242A010000893C24C7842"
    . "42A01000043726561C784242E01000074654E61C7842432010"
    . "0006D656450C784243601000069706541C684243A010000008"
    . "9442404FFD383EC08894424548D84243B010000893C24C7842"
    . "43B010000436F6E6EC784243F0100006563744EC7842443010"
    . "000616D6564C784244701000050697065C684244B010000008"
    . "9442404FFD383EC08894424448D8424B7000000893C24C7842"
    . "4B7000000436C6F73C78424BB0000006548616EC78424BF000"
    . "000646C650089442404FFD383EC088944243C8D8424C300000"
    . "0893C24C78424C300000043726561C78424C70000007465466"
    . "9C78424CB0000006C65410089442404FFD383EC088944245C8"
    . "D8424CF000000893C24C78424CF00000047657446C78424D30"
    . "00000696C6553C78424D7000000697A650089442404FFD383E"
    . "C0889442460B867410000893C24668984245C0100008D84244"
    . "C010000C784244C01000043726561C78424500100007465466"
    . "9C78424540100006C654D61C78424580100007070696EC6842"
    . "45E0100000089442404FFD383EC0889442464B865000000893"
    . "C2466898424E70000008D8424DB000000C78424DB0000004D6"
    . "17056C78424DF0000006965774FC78424E30000006646696C8"
    . "9442404FFD383EC08894424688D84241A010000893C24C7842"
    . "41A010000556E6D61C784241E01000070566965C7842422010"
    . "000774F6646C7842426010000696C650089442404FFD383EC0"
    . "88944246C8D842474010000893C24C78424740100005175657"
    . "2C784247801000079506572C784247C010000666F726DC7842"
    . "480010000616E6365C7842484010000436F756EC7842488010"
    . "0007465720089442404FFD383EC08894424588D84248400000"
    . "0893C24C78424840000006C737472C78424880000006361744"
    . "1C684248C0000000089442404FFD383EC08894424348D84248"
    . "D000000893C24C784248D0000006C737472C78424910000006"
    . "C656E41C68424950000000089442404FFD383EC0889C38DBC2"
    . "496000000C744241000000000C744240800000000896C2404C"
    . "7042400000000897C240CFF54243883EC1480BC24960000000"
    . "00F85AA0500008B8C24F0010000895C2440890C24FFD383EC0"
    . "43DE27F00000F8F930500008B8C2400020000B800800000C70"
    . "424000000008D3C8D0400000081FF00800000897C24480F43C"
    . "789442404FFD683EC0885C089C30F84900500008D442478C78"
    . "4245F0100005C5C2E5CC784246301000070697065C78424670"
    . "100005C41484BC784246B01000031323334C784246F0100003"
    . "5363738C684247301000000890424FF54245883EC048DB4245"
    . "F0100008B7C2478893424FF5424408D14068D4C06F883EC048"
    . "D76008DBC270000000089F883EA01C1EF0483E00F83C041880"
    . "239D175EC8B8424F0010000C60322C6430100891C248944240"
    . "4FF54243483EC08B9202200008D7C247166894C2475C744247"
    . "122202F66C644247700891C24897C2404FF54243483EC08897"
    . "42404891C24FF54243483EC08897C2404C644247300891C24F"
    . "F54243483EC08891C24FF54244083EC0489C78B8C24F401000"
    . "0890C24FF54244001C783EC0481FFFF7F00000F8E0E0400008"
    . "934248B7C2454C744241C00000000C744241800000000C7442"
    . "41400000000C744241000000000C744240CFF000000C744240"
    . "800000000C744240402000000FFD783EC2089342489442434C"
    . "744241C00000000C744241800000000C744241400000000C74"
    . "4241000000000C744240CFF000000C744240800000000C7442"
    . "40402000000FFD783EC2089C6837C2434FF0F84ED03000083F"
    . "8FF0F84E40300008D8C248C0100008D9424D001000089C8C60"
    . "00083C00139D075F68D8424F8000000C784248C01000044000"
    . "000894C2420C744241C00000000C7442418000000008944242"
    . "4C744241400000000C744241000000000C744240C00000000C"
    . "744240800000000895C2404C7042400000000FF54245083EC2"
    . "885C00F842C0300008B8424F80000008B7C243C89042489F8F"
    . "FD083EC048B8424FC00000089042489F8FFD083EC0431C08B9"
    . "4240002000085D274218B8C2400020000908D7426008B54850"
    . "089148383C00139C175F28B44244883E804C70403000000008"
    . "B8424F0010000C744241800000000C744241400000000C7442"
    . "41003000000C744240C00000000C744240803000000C744240"
    . "400000080890424FF54245C83EC1C83F8FF89C7894424400F8"
    . "458010000C744240400000000890424FF54246083EC0889C5C"
    . "744241400000000C744241000000000C744240C00000000C74"
    . "4240802000000C744240400000000893C24FF54246483EC188"
    . "5C0894424500F84F8000000C744241000000000C744240C000"
    . "00000C744240800000000C744240404000000890424FF54246"
    . "883EC1485C00F84B8000000C1ED02C78424080100000B00000"
    . "0C784240C0100000D00000085EDC784241001000011000000C"
    . "7842414010000130000007435B90B00000031D2EB108D74260"
    . "089D183E1038B8C8C0801000069C983000000030C9089D783C"
    . "20183E70339D5898CBC0801000075D88BBC240002000031C98"
    . "5FF74398BBC24000200008D74260089CA83E20369AC9408010"
    . "0008300000001CD89AC94080100008B148BD1C229EAD1C229E"
    . "A89148B83C10139CF75D2890424FF54246C83EC048B4424508"
    . "90424FF54243C83EC048B442440890424FF54243C83EC048B7"
    . "C2434C744240400000000893C24FF54244483EC088B6C24488"
    . "D8C2496000000893C24C744241000000000895C2404894C240"
    . "C896C2408FF54243883EC14893C248B7C243CFFD783EC04C74"
    . "4240400000000893424FF54244483EC088D942496000000C74"
    . "4241000000000896C2408895C24048934248954240CFF54243"
    . "883EC1489F8893424FFD083EC0489D88D542BFC8B8C2400020"
    . "00085C9740DC7000000000083C00439C275F3891C24FF54244"
    . "C83EC0431C081C4DC0100005B5E5F5DC2140089F68DBC27000"
    . "00000B8FEFFFFFF81C4DC0100005B5E5F5DC21400837864011"
    . "9D283E21083C278E90DF6FFFF8B8424F4010000891C2489442"
    . "404FF54243483EC08E9D8FBFFFF81C4DC010000B8FFFFFFFF5"
    . "B5E5F5DC21400B8FDFFFFFFEBAFB8FCFFFFFFEBA88B4424348"
    . "B7C243C89042489F8FFD083EC0489F8893424FFD083EC04891"
    . "C24FF54244CB8F9FFFFFF83EC04E978FFFFFFB8FBFFFFFFE96"
    . "EFFFFFF8B4424348B7C243C89042489F8FFD083EC0489F8893"
    . "424FFD083EC04891C24FF54244CB8FAFFFFFF83EC04E93EFFF"
    . "FFF909090909090909090"
    x64:="4157415641554154555756534881ECC8020000B8FFFFFFF"
    . "F8B9C24300300004889942418030000418B503C4D89C648898"
    . "C24100300004C89CD4C01C2803A500F85AB080000807A01450"
    . "F85A1080000448B5A60B8880000004585DB0F85AA080000448"
    . "B0C02B8FEFFFFFF4D01F1418B4918418B512085C90F8473080"
    . "00031C04D8D0416EB0C4883C00139C10F8673080000418B148"
    . "041813C164765745075E641817C1604726F634175DB418B512"
    . "4498D04460FB71410418B411C498D1496448B24024585E40F8"
    . "43908000048B8577269746546696CBA6500000048BE476C6F6"
    . "2616C416C48898424F0000000488D8424F00000004D01F4668"
    . "99424F80000004C89F148BF476C6F62616C46724889C248894"
    . "4245041FFD44889B42410010000488D9424100100004C89F14"
    . "889442458C78424180100006C6F630048BE437265617465507"
    . "241FFD4B9656500004889BC2400010000488D9424000100006"
    . "6898C240801000048BF4372656174654E614C89F14989C5C68"
    . "4240A0100000041FFD441B8734100004889B42460010000488"
    . "D94246001000066448984246C0100004C89F148BE6D6564506"
    . "97065414889442460C78424680100006F636573C684246E010"
    . "0000041FFD44889BC249001000048BF436F6E6E6563744E488"
    . "9B42498010000488D9424900100004C89F148BE616D6564506"
    . "970654889442468C68424A00100000041FFD44889BC24B0010"
    . "00048BF436C6F736548616E4889B424B8010000488D9424B00"
    . "100004C89F14889442470C68424C00100000041FFD44889BC2"
    . "42001000048BF4372656174654669488D9424200100004C89F"
    . "14889442478C7842428010000646C650041FFD44889BC24300"
    . "1000048BF47657446696C6553488D9424300100004C89F1488"
    . "9C6C78424380100006C65410041FFD44889BC244001000048B"
    . "F4372656174654669488D9424400100004C89F148898424800"
    . "00000C7842448010000697A650041FFD44889BC24D00100004"
    . "8BF6C654D617070696E41B9674100004889BC24D801000048B"
    . "F4D6170566965774F6644898C24E0010000488D9424D001000"
    . "04C89F14889842490000000C68424E20100000041FFD441BA6"
    . "50000004889BC245001000048BF556E6D61705669656644899"
    . "4245C010000488D9424500100004C89F14889842498000000C"
    . "78424580100006646696C41FFD44889BC247001000048BF774"
    . "F6646696C6500488D9424700100004889BC247801000048BF5"
    . "1756572795065724C89F148898424A000000041FFD44889BC2"
    . "41002000048BF666F726D616E6365488D9424100200004889B"
    . "C241802000048BF436F756E746572004C89F14889BC2420020"
    . "00048BF6C7374726361744148898424A800000041FFD44889B"
    . "C24D0000000488D9424D00000004C89F14989C7C68424D8000"
    . "0000041FFD448B96C7374726C656E41488D9424E0000000488"
    . "9C748898C24E0000000C68424E8000000004C89F141FFD4453"
    . "1C031C94989C448C7442420000000004C8B4C24504889EA488"
    . "B442458FFD080BC24F000000000B8FDFFFFFF0F858D0400004"
    . "88B8C241003000041FFD43DE27F00000F8FB70400008D049D0"
    . "4000000BA008000003D008000008984248C000000480F43D03"
    . "1C941FFD54885C04989C60F84C704000048B85C5C2E5C70697"
    . "0654C8DAC24F0010000488D8C24C000000048898424F001000"
    . "048B85C41484B31323334C7842400020000353637384889842"
    . "4F8010000C68424040200000041FFD7448BBC24C00000004C8"
    . "9E941FFD48D50FF448D40F74489F889D183EA0183E00F41C1E"
    . "F0483C0414139D088840CF001000075E24C8DBC24B00000004"
    . "88B9424100300004C89F141C6062241C6460100FFD7B820220"
    . "0004C89FA4C89F1C78424B000000022202F6666898424B4000"
    . "000C68424B600000000FFD74C89EA4C89F1FFD74C89FA4C89F"
    . "1C68424B200000000FFD74C89F141FFD44189C7488B8C24180"
    . "3000041FFD44101C74181FFFF7F00000F8E790300004531C04"
    . "1B9FF000000BA020000004C89E9488B7C247048C7442438000"
    . "00000C744243000000000C744242800000000C744242000000"
    . "000FFD74989C44531C048C744243800000000C744243000000"
    . "000C74424280000000041B9FF000000C744242000000000BA0"
    . "20000004C89E9FFD74983FCFF4989C70F84300300004883F8F"
    . "F0F8426030000488D8C2450020000488D51684889C80F1F440"
    . "000C600004883C0014839D075F4488D84243002000048894C2"
    . "4404531C94531C031C9C784245002000068000000488944244"
    . "848C7442438000000004C89F248C744243000000000C744242"
    . "800000000C744242000000000488B442468FFD085C00F84920"
    . "20000488B8C2430020000FFD6488B8C2438020000FFD631C08"
    . "5DB7417908B548500418914864883C00139C377F089D848C1E"
    . "00241C70406000000004531C948C744243000000000C744242"
    . "800000000C74424200300000041B803000000BA00000080488"
    . "B8C2410030000488B842480000000FFD04883F8FF4989C50F8"
    . "43A01000031D24889C1488B842490000000FFD04531C931D28"
    . "9C748C744242800000000C74424200000000041B8020000004"
    . "C89E9488B842498000000FFD04885C04889C50F84EF0000004"
    . "531C94531C04889C148C744242000000000BA04000000488B8"
    . "424A0000000FFD04885C00F84C0000000C1EF02C7842480010"
    . "0000B000000C78424840100000D00000085FFC784248801000"
    . "011000000C784248C01000013000000743D4989C1BA0B00000"
    . "04531C0EB0D4489C283E2038B94948001000069D2830000004"
    . "103114489C14183C00183E1034983C1044439C789948C80010"
    . "00075D031C985DB743D660F1F4400004889CA83E2034469849"
    . "480010000830000004101C84489849480010000418B148ED1C"
    . "24429C2D1C24429C24189148E4883C10139CB77C94889C1488"
    . "B8424A8000000FFD04889E9FFD64C89E9FFD6488B7C247831D"
    . "24C89E14889F8FFD0448BAC248C000000488B6C24584C89F24"
    . "C8B4C24504C89E148C7442420000000004589E84889E8FFD04"
    . "C89E1FFD631D24C89F94889F8FFD04C8B4C24504589E84C89F"
    . "24C89F948C7442420000000004889E8FFD04C89F9FFD685DB7"
    . "41A8D53FF4C89F0498D549604C700000000004883C0044839D"
    . "075F14C89F1488B442460FFD031C04881C4C80200005B5E5F5"
    . "D415C415D415E415FC3B8FEFFFFFFEBE5837A64014819C083E"
    . "0104883C078E943F7FFFF488B9424180300004C89F1FFD7E97"
    . "5FCFFFFB8FCFFFFFFEBB94C89E1FFD64C89F9FFD64C89F1488"
    . "B442460FFD0B8F9FFFFFFEB9E4C89E1FFD64C89F9FFD64C89F"
    . "1488B442460FFD0B8FAFFFFFFEB83B8FBFFFFFFE979FFFFFF9"
    . "09090909090909090"
    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)
    base:=DllCall("GetModuleHandle", "Str","Kernel32", Ptr)
    IfExist, %A_ScriptFullPath%
      EnvSet, My_ScriptFullPath, %A_ScriptFullPath%
  }
  GetArgs(args)
  s:=RegExReplace(str,"\s"), RegExReplace(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")
  DetectHiddenWindows, On
  file:=A_ScriptFullPath, pid:=DllCall("GetCurrentProcessId")
  IfNotExist, %file%
  {
    EnvGet, file, My_ScriptFullPath
    IfNotExist, %file%
      return
  }
  if RegExMatch(file, "i)\.(exe|com|scr|bat|cmd)\s*$")
    Run, "%file%" /f kill:%pid%,,, kid
  else
    Run, "%A_AhkPath%" /f "%file%" kill:%pid%,,, kid
  DllCall(&MyFunc,AStr,Ahk,AStr,args,Ptr,base,Ptr,&str,"int",size)
  WinWait, ahk_class AutoHotkey ahk_pid %kid%,, 1
  if (!ErrorLevel)
    WinClose,,, 1
  Process, Close, %kid%
}

GetArgs(ByRef args) {
  global
  Loop, %0%
    args.=" """ (%A_Index%) """"
}

)

if 0>0
{
  file:=%True%
  IfExist, %file%
    Gosub, Encrypt
  ExitApp
}
Gui, +AlwaysOnTop
Gui, Color, DDEEFF
Gui, Font, cRed s28
Gui, Add, Text,, Drag the AHK script here to Encrypt`n`n
Gui, Show,, AHK Source Code Encryptor v%Version%  -  By FeiYue
OnMessage(0x201, "LButton_Down")
LButton_Down() {
  SendMessage, 0xA1, 2
}
return

GuiClose:
ExitApp

GuiDropFiles:
Gui, +OwnDialogs
Loop, parse, A_GuiEvent, `n
{
  file:=A_LoopField
  MsgBox, 4100, Tip, Do you want to encrypt this file ?`n`n%file%
  IfMsgBox, Yes
  {
    Gosub, Encrypt
    MsgBox, 4096, Tip, Encryption is completed !
  }
}
return

Encrypt:
SplitPath, file,, dir
IfNotExist, % Ahk:=dir "\AutoHotkey.exe"
{
  MsgBox, 4096, Error!, `nCan't Find:`n`n%Ahk%`n`t
  Exit
}
FileRead, s, %file%
s:=RegExReplace(Encode(s,Ahk), ".{1,60}", "s.=""$0""`n")
s:=RegExReplace(fs,"i)Exec\([^\n]+\s+ExitApp","`ns=`n" s "`n$0")
f:=RegExReplace(file,"\.[^.]+$") . "-encoded.ahk"
FileDelete, %f%
FileAppend, %s%, %f%
s:=""
return

Encode(s, Ahk) {
  static MyFunc
  Ptr:=A_PtrSize ? "UPtr" : "UInt"
  if !MyFunc
  {
    x32:="5557565383EC108B7C24308B4C24248B5C24288B6C242CC"
    . "704240B000000C74424040D00000085FFC744240811000000C"
    . "744240C13000000742ABA0B00000031C0EB0889C283E2038B1"
    . "49469D2830000000354850089C683C00183E60339C78914B47"
    . "5DF31D285DB742D8DB60000000089D783E7036904BC8300000"
    . "08D34108B04918934BC01F0D1C801F0D1C889049183C20139D"
    . "375D983C41031C05B5E5F5DC39090"
    x64:="4883EC184585C9C704240B000000C74424040D000000C74"
    . "4240811000000C744240C130000007434B80B0000004531D2E"
    . "B094489D083E0038B048469C0830000004103004589D34183C"
    . "2014183E3034983C0044539D14289049C75D64531C085D2743"
    . "24D89C24183E2034269049483000000468D0C00428B0481468"
    . "90C944401C8D1C84401C8D1C8428904814983C0014439C277C"
    . "E31C04883C418C390909090"
    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
OnlyOne()
OnlyOne() {
  EnvGet, file, My_ScriptFullPath
  if RegExMatch(file, "i)\.(exe|com|scr|bat|cmd)\s*$")
    Menu, Tray, Icon, %file%
  SetWorkingDir, % RegExReplace(file, "\\[^\\]*$")
  pid:=DllCall("GetCurrentProcessId")
  DetectHiddenWindows, % (dhw:=A_DetectHiddenWindows) ? "On":"On"
  WinGet, myid, ID, ahk_pid %pid% ahk_class AutoHotkey
  WinSetTitle, ahk_id %myid%,, [%file%]
  WinGet, list, List, [%file%] ahk_class AutoHotkey
  Loop, % list {
    IfEqual, myid, % id:=list%A_Index%, Continue
    WinClose, ahk_id %id%
    WinWaitClose, ahk_id %id%
  }
  DetectHiddenWindows, %dhw%
}
Reload(args="") {
  global
  Loop, %0%
    args.=" """ (%A_Index%) """"
  local file
  EnvGet, file, My_ScriptFullPath
  if (file="")
    return
  if RegExMatch(file, "i)\.(exe|com|scr|bat|cmd)\s*$")
    Run, "%file%" /f %args%,, UseErrorLevel
  else
    Run, "%A_AhkPath%" /f "%file%" %args%,, UseErrorLevel
  ExitApp
}
ListLines, On
;-----------------------
  )
  FileGetSize, pSize, %Ahk%
  FileRead, pFile, *c %Ahk%
  s:="`n" add "`n" s "`n"
  ;  . togetcode(s)
  s:=(A_IsUnicode ? chr(0xfeff) : chr(0xEF) chr(0xBB) chr(0xBF))
    . RegExReplace(s,"\R","`r`n") . "`t`t`t"
  size:=VarSetCapacity(s,-1)//4
  DllCall(&MyFunc, Ptr,&s, "uint",size
    , Ptr,&pFile, "uint",pSize//4, "Cdecl")
  VarSetCapacity(str, size*11*(!!A_IsUnicode+1))
  Loop, % size
    str.="u" . NumGet(s,(A_Index-1)*4,"uint")
  return str
}

; If you need to read the script itself, you need it
togetcode(s) {
  StringReplace, s, s, `r,, 1
  StringReplace, s, s, #, #0, 1
  r:="`n"";``"
  Loop, Parse, r
    StringReplace, s, s, %A_LoopField%, #%A_Index%, 1
  s:="`ngetcode(k) {`nListLines, Off`n"
    . "IfNotEqual,k,[email protected],return`ns=`n"
    . RegExReplace(s,".{1,60}","s.=""$0""`n")
    . "r:=""``n"""";````""`n"
    . "Loop, Parse, r`n"
    . "  StringReplace, s, s, #%A_Index%, %A_LoopField%, 1`n"
    . "StringReplace, s, s, #0, #, 1`n"
    . "return s`n}`n"
  return s
}

;======== The End ========

;

/******** The C source code ********

#include <windows.h>

typedef HANDLE (WINAPI * _GetProcAddress)(
  HMODULE hModule,
  LPCSTR  lpProcName
);

typedef BOOL (WINAPI * _CreateProcessA)(
  LPCSTR                lpApplicationName,
  LPSTR                 lpCommandLine,
  LPSECURITY_ATTRIBUTES lpProcessAttributes,
  LPSECURITY_ATTRIBUTES lpThreadAttributes,
  BOOL                  bInheritHandles,
  DWORD                 dwCreationFlags,
  LPVOID                lpEnvironment,
  LPCSTR                lpCurrentDirectory,
  LPSTARTUPINFOA        lpStartupInfo,
  LPPROCESS_INFORMATION lpProcessInformation
);

typedef HGLOBAL (WINAPI * _GlobalAlloc)(
  UINT   uFlags,
  SIZE_T dwBytes
);

typedef HGLOBAL (WINAPI * _GlobalFree)(
  HGLOBAL hMem
);

typedef BOOL (WINAPI * _WriteFile)(
  HANDLE       hFile,
  LPCVOID      lpBuffer,
  DWORD        nNumberOfBytesToWrite,
  LPDWORD      lpNumberOfBytesWritten,
  LPOVERLAPPED lpOverlapped
);

typedef HANDLE (WINAPI * _CreateNamedPipeA)(
  LPCSTR                lpName,
  DWORD                 dwOpenMode,
  DWORD                 dwPipeMode,
  DWORD                 nMaxInstances,
  DWORD                 nOutBufferSize,
  DWORD                 nInBufferSize,
  DWORD                 nDefaultTimeOut,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes
);

typedef BOOL (WINAPI * _ConnectNamedPipe)(
  HANDLE       hNamedPipe,
  LPOVERLAPPED lpOverlapped
);

typedef BOOL (WINAPI * _CloseHandle)(
  HANDLE hObject
);

typedef HANDLE (WINAPI * _CreateFileA)(
  LPCSTR                lpFileName,
  DWORD                 dwDesiredAccess,
  DWORD                 dwShareMode,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  DWORD                 dwCreationDisposition,
  DWORD                 dwFlagsAndAttributes,
  HANDLE                hTemplateFile
);

typedef DWORD (WINAPI * _GetFileSize)(
  HANDLE  hFile,
  LPDWORD lpFileSizeHigh
);

typedef HANDLE (WINAPI * _CreateFileMappingA)(
  HANDLE                hFile,
  LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
  DWORD                 flProtect,
  DWORD                 dwMaximumSizeHigh,
  DWORD                 dwMaximumSizeLow,
  LPCSTR                lpName
);

typedef LPVOID (WINAPI * _MapViewOfFile)(
  HANDLE hFileMappingObject,
  DWORD  dwDesiredAccess,
  DWORD  dwFileOffsetHigh,
  DWORD  dwFileOffsetLow,
  SIZE_T dwNumberOfBytesToMap
);

typedef BOOL (WINAPI * _UnmapViewOfFile)(
  LPCVOID lpBaseAddress
);

typedef BOOL (WINAPI * _QueryPerformanceCounter)(
  LARGE_INTEGER *lpPerformanceCount
);

typedef LPSTR (WINAPI * _lstrcatA)(
  LPSTR  lpString1,
  LPCSTR lpString2
);

typedef int (WINAPI * _lstrlenA)(
  LPCSTR lpString
);


int WINAPI DecodeAndRun(
  LPCSTR ahk_path, LPCSTR args,
  HMODULE hMod, LPDWORD str, DWORD size )
{
  DWORD i, j, k, name_num;
  PUCHAR base, addr1, addr2;
  LPDWORD Addr_func, Addr_name, hMen;
  PUSHORT Addr_ord;
  HANDLE p1, p2;
  //-----------------------------
  base = (PUCHAR)hMod;
  addr1 = base+(*(LPDWORD)(base+0x3C));
  if (addr1[0] != 'P' || addr1[1] != 'E')
    return -1;
  if ((*(LPDWORD)(addr1+0x60))==0||(*(LPDWORD)(addr1+0x64))==0)
    k=0x88;
  else
    k=0x78;
  addr2 = base+(*(LPDWORD)(addr1+k));
  name_num  = *(LPDWORD)(addr2+0x18);
  Addr_func = (LPDWORD)(base+(*(LPDWORD)(addr2+0x1C)));
  Addr_name = (LPDWORD)(base+(*(LPDWORD)(addr2+0x20)));
  Addr_ord  = (PUSHORT)(base+(*(LPDWORD)(addr2+0x24)));
  char str0[] = "GetProcAddress"; k=0;
  for (i=0; i<name_num; i++)
  {
    j=Addr_name[i];
    if ( (*(LPDWORD)(base+j)) == (*(LPDWORD)(str0))
    && (*(LPDWORD)(base+j+4)) == (*(LPDWORD)(str0+4)) )
    {
      k=Addr_func[Addr_ord[i]];
      break;
    }
  }
  if (k==0)
    return -2;
  //-----------------------------
  _GetProcAddress GetProcAddress = (_GetProcAddress)(base+k);

  char str1[] = "WriteFile";
  _WriteFile WriteFile = (_WriteFile)GetProcAddress(hMod,str1);

  char str2[] = "GlobalAlloc";
  _GlobalAlloc GlobalAlloc = (_GlobalAlloc)GetProcAddress(hMod,str2);

  char str3[] = "GlobalFree";
  _GlobalFree GlobalFree = (_GlobalFree)GetProcAddress(hMod,str3);

  char str4[] = "CreateProcessA";
  _CreateProcessA CreateProcessA = (_CreateProcessA)GetProcAddress(hMod,str4);

  char str5[] = "CreateNamedPipeA";
  _CreateNamedPipeA CreateNamedPipeA = (_CreateNamedPipeA)GetProcAddress(hMod,str5);

  char str6[] = "ConnectNamedPipe";
  _ConnectNamedPipe ConnectNamedPipe = (_ConnectNamedPipe)GetProcAddress(hMod,str6);

  char str7[] = "CloseHandle";
  _CloseHandle CloseHandle = (_CloseHandle)GetProcAddress(hMod,str7);

  char str8[] = "CreateFileA";
  _CreateFileA CreateFileA = (_CreateFileA)GetProcAddress(hMod,str8);

  char str9[] = "GetFileSize";
  _GetFileSize GetFileSize = (_GetFileSize)GetProcAddress(hMod,str9);

  char str10[] = "CreateFileMappingA";
  _CreateFileMappingA CreateFileMappingA = (_CreateFileMappingA)GetProcAddress(hMod,str10);

  char str11[] = "MapViewOfFile";
  _MapViewOfFile MapViewOfFile = (_MapViewOfFile)GetProcAddress(hMod,str11);

  char str12[] = "UnmapViewOfFile";
  _UnmapViewOfFile UnmapViewOfFile = (_UnmapViewOfFile)GetProcAddress(hMod,str12);

  char str13[] = "QueryPerformanceCounter";
  _QueryPerformanceCounter QueryPerformanceCounter = (_QueryPerformanceCounter)GetProcAddress(hMod,str13);

  char str14[] = "lstrcatA";
  _lstrcatA lstrcatA = (_lstrcatA)GetProcAddress(hMod,str14);

  char str15[] = "lstrlenA";
  _lstrlenA lstrlenA = (_lstrlenA)GetProcAddress(hMod,str15);

  //-----------------------------
  WriteFile(0, str, 0, (LPDWORD)str1, 0);
  if (str1[0]!=0)
    return -3;
  if (lstrlenA(ahk_path)>32768-30)
    return -4;
  k=(size+1)*4; if (k<32768) k=32768;
  hMen=(LPDWORD)GlobalAlloc(0, k);
  if (hMen==0)
    return -5;
  //-----------------------------
  // "ahk_path" /f "pipe_name" args
  //-----------------------------
  char pipe_name[] = "\\\\.\\pipe\\AHK12345678";
  LARGE_INTEGER count;
  QueryPerformanceCounter(&count);
  k=count.LowPart; j=lstrlenA(pipe_name);
  for (i=1; i<=8; i++, k>>=4)
    pipe_name[j-i]=(k&0xF)+'A';
  //-----------------------------
  LPSTR cmdline=(LPSTR)hMen;
  cmdline[0]='\"'; cmdline[1]=0;
  lstrcatA(cmdline, ahk_path); char t1[]="\" /f \"";
  lstrcatA(cmdline, t1);
  lstrcatA(cmdline, pipe_name); t1[2]=0;
  lstrcatA(cmdline, t1);
  if (lstrlenA(cmdline)+lstrlenA(args)<32768)
    lstrcatA(cmdline, args);
  //-----------------------------
  p1=CreateNamedPipeA(pipe_name, 2, 0, 255, 0, 0, 0, 0);
  p2=CreateNamedPipeA(pipe_name, 2, 0, 255, 0, 0, 0, 0);
  if ((HANDLE)-1 == p1 || (HANDLE)-1 == p2)
  {
    CloseHandle(p1);
    CloseHandle(p2);
    GlobalFree(hMen);
    return -6;
  }
  STARTUPINFOA si;
  PROCESS_INFORMATION pi;
  k=sizeof(STARTUPINFOA); addr1=(PUCHAR)&si;
  for (i=0; i<k; i++)
    addr1[i]=0;
  si.cb=k;
  if (!CreateProcessA(0, cmdline, 0,0,0, 0,0,0, &si, &pi))
  {
    CloseHandle(p1);
    CloseHandle(p2);
    GlobalFree(hMen);
    return -7;
  }
  CloseHandle(pi.hProcess);
  CloseHandle(pi.hThread);
  for (i=0; i<size; i++)
    hMen[i]=str[i];
  hMen[i]=0;
  HANDLE hFile, hFile2;
  LPDWORD pFile;
  DWORD pSize;
  hFile=CreateFileA(ahk_path, 0x80000000, 3, 0, 3, 0, 0);
  if ((HANDLE)-1 != hFile)
  {
    pSize=GetFileSize(hFile, 0) / 4;
    if (hFile2=CreateFileMappingA(hFile, 0, 2, 0, 0, 0))
    {
      if (pFile=(LPDWORD)MapViewOfFile(hFile2, 4, 0, 0, 0))
      {
        //-----------------------------
        // Copy the contents of Decode()
        //-----------------------------

        // My private encryption algorithm is not published here.
        // You can write your own encryption algorithm. Such as:
        unsigned int hash=0;
        for (i=0; i<pSize; i++)
          hash = hash * 31 + pFile[i];
        for (i=0; i<size; i++)
          hMen[i] = (hMen[i] ^ (hash + i)) - 666;

        //-----------------------------
        UnmapViewOfFile(pFile);
      }
      CloseHandle(hFile2);
    }
    CloseHandle(hFile);
  }
  ConnectNamedPipe(p1, 0); k=(size+1)*4;
  WriteFile(p1, hMen, k, (LPDWORD)str1, 0);
  CloseHandle(p1);
  ConnectNamedPipe(p2, 0);
  WriteFile(p2, hMen, k, (LPDWORD)str1, 0);
  CloseHandle(p2);
  for (i=0; i<size; i++)
    hMen[i]=0;
  GlobalFree(hMen);
  return 0;
}

//******** Encode() ********

int Encode(unsigned int * hMen, unsigned int size
  , unsigned int * pFile, unsigned int pSize)
{
  // My private encryption algorithm is not published here.
  // You can write your own encryption algorithm. Such as:
  unsigned int i, hash=0;
  for (i=0; i<pSize; i++)
    hash = hash * 31 + pFile[i];
  for (i=0; i<size; i++)
    hMen[i] = (hMen[i] + 666) ^ (hash + i);
}

//******** Decode() ********

int Decode(unsigned int * hMen, unsigned int size
  , unsigned int * pFile, unsigned int pSize)
{
  // My private encryption algorithm is not published here.
  // You can write your own encryption algorithm. Such as:
  unsigned int i, hash=0;
  for (i=0; i<pSize; i++)
    hash = hash * 31 + pFile[i];
  for (i=0; i<size; i++)
    hMen[i] = (hMen[i] ^ (hash + i)) - 666;
}

*/


Re: AHK源代码加密器 v1.5

Posted: 20 Jan 2018, 06:50
by oldbrother
xiexie!!!!

Re: AHK源代码加密器 v1.5

Posted: 06 Mar 2018, 12:38
by guest3456
nice

can you give an explanation in english? maybe you could post this in the "Scripts And Functions" forum

and can you provide the c++ source code for the machine codes?

Re: AHK源代码加密器 v1.5

Posted: 06 Mar 2018, 17:37
by garry
謝謝

Re: AHK源代码加密器 v1.5

Posted: 07 Mar 2018, 11:18
by feiyue
@guest3456, I've changed the script to English, but I don't want to repeat it to the "Scripts And Functions" forum.
If I provide the C source code for the machine code, the Encryptor is cracked. :beer:

Re: AHK源代码加密器 v1.5

Posted: 07 Mar 2018, 12:17
by SpeedMaster
Hello,

I tried the compiled exe script but a get this error. :cry:

Code: Select all

---------------------------
Error!
---------------------------


Can't Find: C:\Users\ZZ\AutoHotkey.exe ! 


---------------------------
OK   
---------------------------

Re: AHK源代码加密器 v1.5

Posted: 07 Mar 2018, 12:33
by feiyue
@SpeedMaster,
Because it is calling the AHK master program to run dynamically,
There must be a AHK master program in the same directory.

Re: AHK源代码加密器 v1.5

Posted: 07 Mar 2018, 13:00
by SpeedMaster
feiyue wrote:@SpeedMaster,
There must be a AHK master program in the same directory.
I do not see any AHK master program in the folder :?
03_07_18 @6_54_57.PNG
03_07_18 @6_54_57.PNG (9.91 KiB) Viewed 15215 times

Re: AHK源代码加密器 v1.5

Posted: 07 Mar 2018, 13:27
by feiyue
AHK master program is AutoHotkey.exe.
If AutoHotkey.exe is renamed abc.exe, Please modify Ahk=%A_ScriptDir%\abc.exe

Re: AHK源代码加密器 v1.5

Posted: 07 Mar 2018, 14:03
by SpeedMaster
I got it now :idea: :lolno:
Thanks :thumbup:
ahk.png
ahk.png (7.06 KiB) Viewed 15195 times

Re: AHK源代码加密器 v1.6

Posted: 08 Mar 2018, 21:08
by guest3456
can you explain what the new 'add' code does, with the Gui and getting the process Pid, and the Reload() func that is never called?

Re: AHK源代码加密器 v1.6

Posted: 08 Mar 2018, 22:40
by feiyue
Because the decoded code is not saved to the disk, it is sent directly to the AutoHotkey.exe to execute.
No source files lead to:
1. Only one is allowed to run at the same time (#SingleInstance force), is invalid.
Solution: Use a hidden GUI as the unique tag to prevent conflicts.
2, Reload is invalid. Solution: use the Reload() function instead. :beer:

Re: AHK源代码加密器 v1.6

Posted: 08 Apr 2018, 07:24
by Cuadrix
Very nice!

Re: AHK源代码加密器 v1.7

Posted: 21 Apr 2018, 15:51
by feiyue
Updated to the V1.7 version, made some improvements to avoid being easily cracked.
Thank you very much for Helgef ! :bravo:

Re: AHK源代码加密器 v1.7

Posted: 24 Apr 2018, 23:41
by brutus_skywalker
ROCKS!

Still Rocks, but script can be recovered by doing a memory dump of autohotkey.exe and using sysinternals 'strings' utility, to extract the code.

Simpler still uses processhacker to view the properties of the process running encoded script and just view strings in the memory tab of the properties.

Re: AHK源代码加密器 v1.7

Posted: 25 Apr 2018, 12:39
by feiyue
@brutus_skywalker, Thank you very much for the two ways to view memory text, and I learned new knowledge. :thumbup:

This method can extract fragmented source code text, but can not extract complete code.

It's really lucky. :D

Re: AHK源代码加密器 v2.0

Posted: 29 Apr 2018, 02:53
by feiyue
Updated to the V2.0 version, Enhance the difficulty of cracking. :superhappy:

Re: AHK源代码加密器 v2.0

Posted: 05 Jun 2018, 12:57
by AHKStudent
Hi

To share my app users need to download both files? Or can I just compile my main exe and have the autohotkey.exe within it (like through file install?)

What is the right way to do this?

Re: AHK源代码加密器 v2.0

Posted: 06 Jun 2018, 04:36
by alware
i am using gdi+ and gdip imagesearch but it seems encoded script won't recognize the image files that are in the same folder..
any help??

Re: AHK源代码加密器 v2.0

Posted: 07 Jun 2018, 01:25
by feiyue
@AHKStudent, I fixed this problem, and now it can package the AHK main program into a separate program.

@alware, Because script runs through named pipes, A_ScriptDir is useless.
You should use dir:=A_WorkingDir to get the used directory of the compiled program.