AHK源代码加密器 v3.4

许多实用脚本和封装函数, 可以让您编写脚本更加便捷高效

Moderators: tmplinshi, arcticir

Post Reply
feiyue
Posts: 381
Joined: 08 Aug 2014, 04:08

AHK源代码加密器 v3.4

Post by feiyue » 06 Jan 2018, 17:33

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

Code: Select all

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

  (required AHK v1.1.34+)

  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.
     Don't use Unicode 32/64-bit.bin, but use AHK v1.1.34+.exe

  3. When you want to use the directory relative to the script(eg 001.jpg),
     Don't use A_ScriptDir, A_ScriptFullPath, but use A_ScriptDir2, A_ScriptFullPath2.
     It is recommended to change the path to absolute path %A_ScriptDir2%\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.4

fs=
(` %

; You can compile and set icons by using Ahk2Exe.exe

#NoEnv
#NoTrayIcon
#SingleInstance off
SetBatchLines, -1
ScriptGuard1()
ScriptGuard1()  ; By TAC109
{
  For i,ahk in ["#1", ">AUTOHOTKEY SCRIPT<"]
  if (rc:=DllCall("FindResource", "Ptr",0, "Str",ahk, "Ptr",10, "Ptr"))
  && (sz:=DllCall("SizeofResource", "Ptr",0, "Ptr",rc, "Uint"))
  && (pt:=DllCall("LoadResource", "Ptr",0, "Ptr",rc, "Ptr"))
  && (pt:=DllCall("LockResource", "Ptr",pt, "Ptr"))
  && (DllCall("VirtualProtect", "Ptr",pt, "Ptr",sz, "UInt",0x40, "UInt*",rc))
  && (InStr(StrGet(pt,20,"CP0"), "<COMPILER"))
    DllCall("RtlZeroMemory", "Ptr",pt, "UInt",sz)
}

if !(A_IsAdmin or RegExMatch(DllCall("GetCommandLine","str"), " /restart(?!\S)"))
{
  Try
  {
    if (A_IsCompiled)
      Run *RunAs "%A_ScriptFullPath%" /restart
    else if FileExist(A_ScriptFullPath)
      Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
  }
  ExitApp
}

s=
Exec(s)
ExitApp

Exec(str, args:="") {
  static MyFunc, base
  SetBatchLines, -1
  if (!MyFunc)
  {
    x32:="5557565381ecfc0100008b9c24100200008bac24180200008b433c01d8803850"
    . "0f85780a0000807801450f856e0a00008b7878c744247c0000000001df89f88b"
    . "4f188b7f1c8b50208b40248944243431c085c98d34137513e9030a00008d7600"
    . "83c00139c10f84f50900008b1486813c134765745075e9817c1304726f634175"
    . "df8b7424348d04430fb704308d04838b343885f68974247c0f84c20900008d84"
    . "249f000000bf6500000001de6689bc24a7000000c784249f00000057726974c7"
    . "8424a30000006546696c89442404891c24ffd683ec08894424408d8424b40000"
    . "00c78424b4000000476c6f62c78424b8000000616c416cc78424bc0000006c6f"
    . "630089442404891c24ffd683ec0889c7b86565000066898424b10000008d8424"
    . "a9000000c78424a9000000476c6f62c78424ad000000616c4672c68424b30000"
    . "000089442404891c24ffd683ec0889442454b873410000c78424e60000004372"
    . "656166898424f20000008d8424e6000000c78424ea00000074655072c78424ee"
    . "0000006f636573c68424f40000000089442404891c24ffd683ec08894424588d"
    . "842437010000c784243701000043726561c784243b01000074654e61c784243f"
    . "0100006d656450c784244301000069706541c68424470100000089442404891c"
    . "24ffd683ec08894424488d842448010000c7842448010000436f6e6ec784244c"
    . "0100006563744ec7842450010000616d6564c784245401000050697065c68424"
    . "580100000089442404891c24ffd683ec088944245c8d8424c0000000c78424c0"
    . "000000436c6f73c78424c40000006548616ec78424c8000000646c6500894424"
    . "04891c24ffd683ec08894424348d842494010000c784249401000051756572c7"
    . "84249801000079506572c784249c010000666f726dc78424a0010000616e6365"
    . "c78424a4010000436f756ec78424a80100007465720089442404891c24ffd683"
    . "ec08894424608d84248d000000c784248d0000006c737472c784249100000063"
    . "617441c68424950000000089442404891c24ffd683ec08894424388d84249600"
    . "0000c78424960000006c737472c784249a0000006c656e41c684249e00000000"
    . "89442404891c24ffd683ec088944243cb865410000c78424590100004765744d"
    . "66898424690100008d842459010000c784245d0100006f64756cc78424610100"
    . "006546696cc7842465010000654e616dc684246b0100000089442404891c24ff"
    . "d683ec08894424448d8424cc000000c78424cc00000043726561c78424d00000"
    . "0074654669c78424d40000006c65410089442404891c24ffd683ec0889442450"
    . "b867410000c784246c01000043726561668984247c0100008d84246c010000c7"
    . "84247001000074654669c78424740100006c654d61c78424780100007070696e"
    . "c684247e0100000089442404891c24ffd683ec0889442464b865000000c78424"
    . "d80000004d61705666898424e40000008d8424d8000000c78424dc0000006965"
    . "774fc78424e00000006646696c891c2489442404ffd683ec08894424688d8424"
    . "27010000c7842427010000556e6d61c784242b01000070566965c784242f0100"
    . "00774f6646c7842433010000696c650089442404891c24ffd683ec088944246c"
    . "8b84241c0200008d0485040000003dff7f00008944244c0f86b5050000894424"
    . "7c89442404c7042400000000ffd783ec0885c089c60f8494060000c744240800"
    . "04000089442404891c24ff54244483ec0cc744247c00000000c7442418000000"
    . "00c744241400000000c744241003000000c744240c00000000c7442408030000"
    . "00c744240400000080893424ff54245083ec1c83f8ff89c70f84c0000000c744"
    . "241400000000c744241000000000c744240c00000000c744240802000000c744"
    . "240400000000890424ff54246483ec1885c089442450747cc744241000000000"
    . "c744240c00000000c744240800000000c744240404000000890424ff54246883"
    . "ec1485c074408b4c2440c744247c0100000031d28944246429d98b5c244001c1"
    . "8b04133904117408c744247c0000000083c20483fa3475e88b442464890424ff"
    . "54246c83ec048b442450890424ff54243483ec04893c24ff54243483ec048b5c"
    . "247c85db0f84110500008d44247cc744241000000000c744240800000000896c"
    . "2404c70424000000008944240cff54244083ec148b44247c85c0894424500f85"
    . "170500008d842480000000c784247f0100005c5c2e5cc7842483010000706970"
    . "65c78424870100005c41484bc784248b01000031323334c784248f0100003536"
    . "3738c684249301000000890424ff54246083ec048b8424800000008d9c247f01"
    . "0000891c248944247cff54243c83ec048d14038d7c03f88b4c247c908d742600"
    . "89c883ea01c1e90483e00f83c041880239d775ec8d4601894c247cc60622c744"
    . "240800040000c704240000000089442404ff54244483ec0cb9202200008dbc24"
    . "f500000066898c2401010000c78424f500000022202f73c78424f90000006372"
    . "6970c78424fd00000074202f66897c2404c684240301000000893424ff542438"
    . "83ec08895c2404893424ff54243883ec08897c2404c68424f700000000893424"
    . "ff54243883ec08893424ff54243c83ec0489c78b8c2414020000890c24ff5424"
    . "3c01c783ec0481ffff7f00000f8ef2020000891c24c744241c00000000c74424"
    . "1800000000c744241400000000c744241000000000c744240cff000000c74424"
    . "0800000000c744240402000000ff54244883ec2089c7891c24c744241c000000"
    . "00c744241800000000c744241400000000c744241000000000c744240cff0000"
    . "00c744240800000000c744240402000000ff54244883ec2083ffff89c30f8456"
    . "03000083f8ff0f844d0300008d8c24ac0100008d9424f0010000c744247c4400"
    . "000089c8c6000083c00139d075f68d842404010000c78424ac01000044000000"
    . "894c2420c744241c00000000c74424180000000089442424c744241400000000"
    . "c744241000000000c744240c00000000c74424080000000089742404c7042400"
    . "000000ff54245883ec2885c00f847d0200008b842404010000890424ff542434"
    . "83ec048b842408010000890424ff54243483ec048b94241c02000085d2742a8b"
    . "8c241c02000031c0908db426000000008b54850089148683c00139c175f28b44"
    . "244c83e804894424508b442450c78424140100006f000000ba6f000000c78424"
    . "1801000071000000c784241c01000075000000c784242001000077000000c704"
    . "060000000031c0eb138db4260000000089c283e2038b94941401000069d28300"
    . "000089c183e10301c283c00183f86489948c1401000075d88b84241c02000085"
    . "c00f84290100008b8c241c02000031d289d083e00369ac841401000083000000"
    . "01d589ac84140100008b0496d1c029e8d1c029e88944247c89049683c20139d1"
    . "75ce8b6c245c893c24c74424040000000089e8ffd083ec08893c248b7c243489"
    . "f8ffd083ec0489e8c744240400000000891c24ffd083ec088b6c244c8d44247c"
    . "c74424100000000089742404891c248944240c896c2408ff54244083ec1489f8"
    . "891c24ffd08d542efc83ec0489f06690c7000000000083c00439c275f3893424"
    . "ff54245483ec0431c081c4fc0100005b5e5f5dc210008d76008dbc2700000000"
    . "b8feffffff81c4fc0100005b5e5f5dc21000c744247c00800000b800800000e9"
    . "3dfaffff8b84241402000089342489442404ff54243883ec08e9f4fcffff81c4"
    . "fc010000b8ffffffff5b5e5f5dc210008b6c245c893c24c74424040000000089"
    . "e8ffd083ec08893c248b7c243489f8ffd083ec0489e8c744240400000000891c"
    . "24ffd083ec088d44247cc74424100000000089742404891c248944240c8b4424"
    . "4c89442408ff54244083ec1489f8891c24ffd083ec04e922ffffff893424ff54"
    . "2454b8fcffffff83ec04e936ffffff893c248b7c243489f8ffd083ec0489f889"
    . "1c24ffd083ec04893424ff542454b8f9ffffff83ec04e90affffff893424ff54"
    . "2454b8fbffffff83ec04e9f6feffffb8fdffffffe9ecfeffff893c248b7c2434"
    . "89f8ffd083ec0489f8891c24ffd083ec04893424ff542454b8faffffff83ec04"
    . "e9c0feffff9090909090909090909090"
    x64:="4157415641554154555756534881ece8020000b8ffffffff4889942438030000"
    . "8b513c4889cd4c89c64489cb4801ca803a500f8523090000807a01450f851909"
    . "00008b8288000000c78424bc000000000000004801c8448b40188b5020448b50"
    . "1c448b582431c04585c04c8d0c117512e9fa0800004883c0014139c00f86ed08"
    . "0000418b0c81817c0d00476574504889ca75e283c204817c1500726f634175d5"
    . "488d444500420fb70418488d448500468b2c104585ed4489ac24bc0000000f84"
    . "ab08000041b86500000048b8577269746546696c4901ed6644898424f8000000"
    . "488d9424f00000004889e948898424f000000041ffd5488944245848b8476c6f"
    . "62616c416c488d9424100100004889e94889842410010000c78424180100006c"
    . "6f630041ffd541b9656500004989c748b8476c6f62616c46726644898c240801"
    . "0000488d9424000100004889e94889842400010000c684240a0100000041ffd5"
    . "41ba73410000488944246848b8437265617465507266448994245c010000488d"
    . "9424500100004889e94889842450010000c78424580100006f636573c684245e"
    . "0100000041ffd5488944247048b84372656174654e61488d9424900100004889"
    . "84249001000048b86d656450697065414889e94889842498010000c68424a001"
    . "00000041ffd5488944247848b8436f6e6e6563744e488d9424b0010000488984"
    . "24b001000048b8616d6564506970654889e948898424b8010000c68424c00100"
    . "000041ffd5488984248800000048b8436c6f736548616e488d94242001000048"
    . "89e94889842420010000c7842428010000646c650041ffd54889c748b8517565"
    . "7279506572488d942430020000488984243002000048b8666f726d616e636548"
    . "89e9488984243802000048b8436f756e74657200488984244002000041ffd548"
    . "8984248000000048b86c73747263617441488d9424d00000004889e948898424"
    . "d0000000c68424d80000000041ffd54989c448b86c7374726c656e41488d9424"
    . "e00000004889e948898424e0000000c68424e80000000041ffd5488944245048"
    . "b84765744d6f64756c41bb6541000048898424d001000048b86546696c654e61"
    . "6d6644899c24e0010000488d9424d00100004889e948898424d8010000c68424"
    . "e20100000041ffd5488944246048b84372656174654669488d94243001000048"
    . "89e94889842430010000c78424380100006c65410041ffd54989c648b8437265"
    . "6174654669488d9424f001000048898424f001000048b86c654d617070696e48"
    . "89e948898424f8010000b867410000c684240202000000668984240002000041"
    . "ffd5488984249800000048b84d6170566965774f488d94244001000048898424"
    . "40010000b8650000004889e9c78424480100006646696c668984244c01000041"
    . "ffd548898424a000000048b8556e6d6170566965488d94247001000048898424"
    . "7001000048b8774f6646696c65004889e9488984247801000041ffd548898424"
    . "a80000008d049d040000003dff7f0000898424940000000f8619050000898424"
    . "bc00000089c231c941ffd74885c04989c50f84b70500004889c241b800040000"
    . "4889e9488b442460ffd04531c9c78424bc0000000000000048c7442430000000"
    . "00c744242800000000c74424200300000041b803000000ba000000804c89e941"
    . "ffd64883f8ff4989c60f84ad0000004531c931d24889c148c744242800000000"
    . "c74424200000000041b802000000488b842498000000ffd04885c04989c77477"
    . "4531c94531c04889c148c744242000000000ba04000000488b8424a0000000ff"
    . "d04885c0744c4c8b442458c78424bc000000010000004d89c34929eb4489da41"
    . "83c33489d1458b1044391408740bc78424bc0000000000000083c2044983c004"
    . "4139d375de4889c1488b8424a8000000ffd04c89f9ffd74c89f1ffd78b8c24bc"
    . "00000085c90f845d0400004c8db424bc0000004531c04889f231c948c7442420"
    . "00000000488b4424584d89f1ffd08b9424bc00000085d20f855d04000048b85c"
    . "5c2e5c70697065488d8c24c0000000c784242002000035363738488984241002"
    . "000048b85c41484b31323334c6842424020000004889842418020000488b8424"
    . "80000000488dac2410020000ffd08b8424c00000004889e9898424bc00000048"
    . "8b442450ffd08b8c24bc0000008d50ff448d48f789c84189d083ea0183e00fc1"
    . "e90483c0414439ca428884041002000075e241b800040000898c24bc0000004c"
    . "8dbc246001000031c9498d550141c6450022488b442460ffd048b822202f7363"
    . "7269704c89fa4c89e94889842460010000b820220000c784246801000074202f"
    . "66668984246c010000c684246e0100000041ffd44889ea4c89e941ffd44c89fa"
    . "4c89e9c68424620100000041ffd44c89e9488b442450ffd04189c7488b8c2438"
    . "030000488b442450ffd04101c74181ffff7f00000f8e910200004531c04889e9"
    . "41b9ff000000ba020000004c8b7c247848c744243800000000c7442430000000"
    . "00c744242800000000c74424200000000041ffd74989c44531c04889e948c744"
    . "243800000000c74424300000000041b9ff000000c744242800000000c7442420"
    . "00000000ba0200000041ffd74983fcff4889c50f84bf0200004883f8ff0f84b5"
    . "020000488d8c2470020000c78424bc00000068000000488d51684889c80f1f00"
    . "c600004883c0014839d075f4488d84245002000048894c24404531c94531c031"
    . "c9c784247002000068000000488944244848c7442438000000004c89ea48c744"
    . "243000000000c744242800000000c744242000000000488b442470ffd085c00f"
    . "84f7010000488b8c2450020000ffd7488b8c2458020000ffd731c085db741790"
    . "8b148641895485004883c00139c377f089d848c1e00241c744050000000000c7"
    . "8424800100006f000000ba6f000000c784248401000071000000c78424880100"
    . "007500000031c0c784248c01000077000000eb0c89c283e2038b949480010000"
    . "69d28300000089c183e10301c283c00183f86489948c8001000075d831d285db"
    . "0f84f8000000662e0f1f8400000000004889d083e003698c8480010000830000"
    . "0001d1898c8480010000418b449500d1c029c8d1c029c8898424bc0000004189"
    . "4495004883c20139d377c5488bb4248800000031d24c89e14889f0ffd04c89e1"
    . "ffd731d24889e94889f0ffd04c89ea4d89f1448b8424940000004889e948c744"
    . "242000000000488b442458ffd04889e9ffd78d53ff4c89e8498d5495040f1f00"
    . "c700000000004883c0044839c275f14c89e9488b442468ffd031c04881c4e802"
    . "00005b5e5f5d415c415d415e415fc3b8feffffffebe5c78424bc000000008000"
    . "00ba00800000e9dbfaffff488b9424380300004c89e941ffd4e95cfdffff488b"
    . "b424880000004c89e14889f0ffd04c89e1ffd731d24889e94889f0ffd04889e9"
    . "48c7442420000000004d89f1448b8424940000004c89ea488b442458ffd04889"
    . "e9ffd7e967ffffff4c89e9488b442468ffd0b8fcffffffe95fffffff4c89e1ff"
    . "d74889e9ffd74c89e9488b442468ffd0b8f9ffffffe941ffffff4c89e9488b44"
    . "2468ffd0b8fbffffffe92dffffffb8fdffffffe923ffffff4c89e1ffd74889e9"
    . "ffd74c89e9488b442468ffd0b8faffffffe905ffffff90909090909090909090"
    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")
    if FileExist(A_ScriptFullPath)
      EnvSet, My_ScriptFullPath, %A_ScriptFullPath%
  }
  For k,v in A_Args
  {
    args.=" """ v """"
    if InStr(v, "\\.\pipe\")
    {
      MsgBox, 4096,, Ahk2Exe Don't Use AutoHotkey.bin but Use AutoHotkey.exe
      ExitApp
    }
  }
  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")
  f:=RegisterCallback("Thread","F")
  hThread:=DllCall("CreateThread", "Ptr",0, "Ptr",0, "Ptr",f, "Ptr",0, "uint",0, "Ptr",0)
  r:=DllCall(&MyFunc, "Ptr",base, "AStr",args, "Ptr",&str, "int",size)
  if (hThread)
  {
    DllCall("TerminateThread", "Ptr",hThread, "Ptr",0)
    DllCall("CloseHandle", "Ptr",hThread)
  }
  return r
}

Thread(p)
{
  SetBatchLines, -1
  DllCall("Sleep", "int",1000)
  ExitApp
}

)

if A_Args.Length()
{
  Encrypt(A_Args[1])
  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
  {
    Encrypt(file)
    MsgBox, 4096, Tip, Encryption is completed !
  }
}
return

Encrypt(file)
{
  global fs
  if !FileExist(file)
    return
  FileRead, s, %file%
  s:=RegExReplace(Encode(s), ".{1,60}", "s.=""$0""`n")
  s:=RegExReplace(fs, "i)Exec\([^\n]+\s+ExitApp", s "`n$0")
  f:=RegExReplace(file,"\.[^.\\]+$") . "-encoded.ahk"
  FileDelete, %f%
  FileAppend, %s%, %f%
}

Encode(s) {
  static MyFunc
  if (!MyFunc)
  {
    x32:=""
    . "5756BA6F0000005331C083EC108B5C24208B742424C704246F000000C7442404"
    . "71000000C744240875000000C744240C77000000EB128D76008DBC2700000000"
    . "89C283E2038B149469D28300000089C183E10301C283C00183F86489148C75E0"
    . "31D285F674318D76008DBC270000000089D783E7036904BC830000008D0C108B"
    . "0493890CBC01C8D1C801C8D1C889049383C20139D675D983C41031C05B5E5FC3"
    x64:=""
    . "4883EC1841B86F00000031C0C704246F000000C744240471000000C744240875"
    . "000000C744240C77000000EB0E0F1F004189C04183E003468B04844569C08300"
    . "00004189C14183E1034101C083C00183F8644689048C75D84531C085D2743390"
    . "4D89C24183E2034269049483000000468D0C00428B048146890C944401C8D1C8"
    . "4401C8D1C8428904814983C0014439C277CE31C04883C418C390909090909090"
    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
ScriptGuard2()
ScriptGuard2()  ; By TAC109
{
  For i,ahk in ["#1", ">AUTOHOTKEY SCRIPT<"]
  if (rc:=DllCall("FindResource", "Ptr",0, "Str",ahk, "Ptr",10, "Ptr"))
  && (sz:=DllCall("SizeofResource", "Ptr",0, "Ptr",rc, "Uint"))
  && (pt:=DllCall("LoadResource", "Ptr",0, "Ptr",rc, "Ptr"))
  && (pt:=DllCall("LockResource", "Ptr",pt, "Ptr"))
  && (DllCall("VirtualProtect", "Ptr",pt, "Ptr",sz, "UInt",0x40, "UInt*",rc))
  && (InStr(StrGet(pt,20,"CP0"), "<COMPILER"))
    DllCall("RtlZeroMemory", "Ptr",pt, "UInt",sz)
}
global A_ScriptDir2, A_ScriptFullPath2
EnvGet, file, My_ScriptFullPath
A_ScriptFullPath2:=file
A_ScriptDir2:=RegExReplace(file, "\\[^\\]*$")
SetWorkingDir, % A_ScriptDir2
OnlyOne(file)
OnlyOne(flag="") {
  DetectHiddenWindows, % (dhw:=A_DetectHiddenWindows) ? "On":"On"
  hash:=0
  Loop, Parse, flag
    hash:=(hash*31+Ord(A_LoopField))&0xFFFFFFFF
  Name:="Ahk_OnlyOne_" hash
  While Mutex:=DllCall("OpenMutex", "int",0x100000, "int",0, "str",Name)
  {
    DllCall("CloseHandle", "Ptr",Mutex)
    WinGet, list, List, <<%flag%>> ahk_class AutoHotkey
    Loop % list
    IfWinExist, % "ahk_id " . list%A_Index%
    {
      WinGet, pid, PID
      WinClose,,, 3
      IfWinExist
      {
        Process, Close, %pid%
        Process, WaitClose, %pid%, 3
      }
    }
  }
  DllCall("CreateMutex", "Ptr",0, "int",0, "str",Name)
  IfEqual, A_LastError, 0xB7, ExitApp
  WinSetTitle, ahk_id %A_ScriptHwnd%,, <<%flag%>>
  DetectHiddenWindows, %dhw%
}
Reload(args="") {
  Loop % A_Args.Length()
    args.=" """ A_Args[A_Index] """"
  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%" /script /f "%file%" %args%,, UseErrorLevel
  ExitApp
}
ListLines, On
;-----------------------
  )
  s:="`n" 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
  DllCall(&MyFunc, "Ptr",&s, "uint",size, "Cdecl")
  VarSetCapacity(str, size*11*(!!A_IsUnicode+1))
  Loop, % size
    str.="u" . NumGet(s, (A_Index-1)*4, "uint")
  return str
}


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

;

getfs() {
  fs=
(` %

#include <windows.h>

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

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

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

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

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 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 BOOL (WINAPI * _QueryPerformanceCounter)(
  LARGE_INTEGER *lpPerformanceCount );

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

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

typedef DWORD (WINAPI * _GetModuleFileNameA)(
  HMODULE hModule,
  LPSTR lpFilename,
  DWORD nSize );

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

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 );

//-----------------------------

int WINAPI DecodeAndRun(HMODULE hMod, LPCSTR args, LPDWORD str, DWORD size)
{
  DWORD i, j, k, name_num;
  PUCHAR base, addr;
  LPDWORD Addr_func, Addr_name, hMen;
  PUSHORT Addr_ord;
  HANDLE p1, p2;
  //-----------------------------
  base = (PUCHAR)hMod;
  addr = base+(*(LPDWORD)(base+0x3C));
  if (addr[0] != 'P' || addr[1] != 'E')
    return -1;
  k = (sizeof(HANDLE)==8 ? 0x88 : 0x78);
  addr = base+(*(LPDWORD)(addr+k));
  name_num  = *(LPDWORD)(addr+0x18);
  Addr_func = (LPDWORD)(base+(*(LPDWORD)(addr+0x1C)));
  Addr_name = (LPDWORD)(base+(*(LPDWORD)(addr+0x20)));
  Addr_ord  = (PUSHORT)(base+(*(LPDWORD)(addr+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[] = "QueryPerformanceCounter";
  _QueryPerformanceCounter QueryPerformanceCounter = (_QueryPerformanceCounter)GetProcAddress(hMod,str8);

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

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

  char str11[] = "GetModuleFileNameA";
  _GetModuleFileNameA GetModuleFileNameA = (_GetModuleFileNameA)GetProcAddress(hMod,str11);

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

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

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

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

  //-----------------------------
  k=(size+1)*4; if (k<32768) k=32768;
  hMen=(LPDWORD)GlobalAlloc(0, k);
  if (hMen==0)
    return -3;
  HANDLE hFile, hFile2;
  PUCHAR pFile;
  LPSTR dllpath=(LPSTR)hMen;
  GetModuleFileNameA(hMod, dllpath, 1024);
  addr=(PUCHAR)WriteFile; j=addr-base; k=0;
  hFile=CreateFileA(dllpath, 0x80000000, 3, 0, 3, 0, 0);
  if ((HANDLE)-1 != hFile)
  {
    if (hFile2=CreateFileMappingA(hFile, 0, 2, 0, 0, 0))
    {
      if (pFile=(PUCHAR)MapViewOfFile(hFile2, 4, 0, 0, 0))
      {
        for (k=1, i=0; i<50; i+=4)
        if ((*(LPDWORD)(pFile+(j+i))) != (*(LPDWORD)(addr+i)))
          k=0;
        UnmapViewOfFile(pFile);
      }
      CloseHandle(hFile2);
    }
    CloseHandle(hFile);
  }
  if (k==0)
  {
    GlobalFree(hMen);
    return -4;
  }
  WriteFile(0, str, 0, &k, 0);
  if (k!=0)
  {
    GlobalFree(hMen);
    return -5;
  }
  //-----------------------------
  // "ahk_path" /script /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]='\"';
  GetModuleFileNameA(0, cmdline+1, 1024);
  char t1[]="\" /script /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); addr=(PUCHAR)&si;
  for (i=0; i<k; i++)
    addr[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;
  //-----------------------------
  // 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<100; i++)
    hash = hash * 31 + i;
  for (i=0; i<size; i++)
    hMen[i] = (hMen[i] ^ (hash + i)) - 666;
  //-----------------------------
  ConnectNamedPipe(p1, 0);
  CloseHandle(p1);
  ConnectNamedPipe(p2, 0);
  WriteFile(p2, hMen, (size+1)*4, &k, 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)
{
  // 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<100; i++)
    hash = hash * 31 + i;
  for (i=0; i<size; i++)
    hMen[i] = (hMen[i] + 666) ^ (hash + i);
}

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

int Decode(unsigned int * hMen, unsigned int size)
{
  // 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<100; i++)
    hash = hash * 31 + i;
  for (i=0; i<size; i++)
    hMen[i] = (hMen[i] ^ (hash + i)) - 666;
}

)
  return fs
}

Last edited by feiyue on 27 Oct 2024, 20:23, edited 82 times in total.

User avatar
oldbrother
Posts: 279
Joined: 23 Oct 2013, 05:08

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

Post by oldbrother » 20 Jan 2018, 06:50

xiexie!!!!

guest3456
Posts: 3477
Joined: 09 Oct 2013, 10:31

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

Post by guest3456 » 06 Mar 2018, 12:38

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?


garry
Posts: 3852
Joined: 22 Dec 2013, 12:50

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

Post by garry » 06 Mar 2018, 17:37

謝謝
Last edited by garry on 07 Mar 2018, 17:07, edited 1 time in total.

feiyue
Posts: 381
Joined: 08 Aug 2014, 04:08

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

Post by feiyue » 07 Mar 2018, 11:18

@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:

User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

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

Post by SpeedMaster » 07 Mar 2018, 12:17

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   
---------------------------

feiyue
Posts: 381
Joined: 08 Aug 2014, 04:08

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

Post by feiyue » 07 Mar 2018, 12:33

@SpeedMaster,
Because it is calling the AHK master program to run dynamically,
There must be a AHK master program in the same directory.

User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

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

Post by SpeedMaster » 07 Mar 2018, 13:00

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 54277 times

feiyue
Posts: 381
Joined: 08 Aug 2014, 04:08

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

Post by feiyue » 07 Mar 2018, 13:27

AHK master program is AutoHotkey.exe.
If AutoHotkey.exe is renamed abc.exe, Please modify Ahk=%A_ScriptDir%\abc.exe

User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

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

Post by SpeedMaster » 07 Mar 2018, 14:03

I got it now :idea: :lolno:
Thanks :thumbup:
ahk.png
ahk.png (7.06 KiB) Viewed 54257 times

guest3456
Posts: 3477
Joined: 09 Oct 2013, 10:31

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

Post by guest3456 » 08 Mar 2018, 21:08

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?


feiyue
Posts: 381
Joined: 08 Aug 2014, 04:08

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

Post by feiyue » 08 Mar 2018, 22:40

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:

User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

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

Post by Cuadrix » 08 Apr 2018, 07:24

Very nice!

feiyue
Posts: 381
Joined: 08 Aug 2014, 04:08

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

Post by feiyue » 21 Apr 2018, 15:51

Updated to the V1.7 version, made some improvements to avoid being easily cracked.
Thank you very much for Helgef ! :bravo:

brutus_skywalker
Posts: 175
Joined: 24 Dec 2016, 13:16
Location: Antarctica

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

Post by brutus_skywalker » 24 Apr 2018, 23:41

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.
Outsourcing Clicks & Presses Since 2004.

feiyue
Posts: 381
Joined: 08 Aug 2014, 04:08

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

Post by feiyue » 25 Apr 2018, 12:39

@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

feiyue
Posts: 381
Joined: 08 Aug 2014, 04:08

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

Post by feiyue » 29 Apr 2018, 02:53

Updated to the V2.0 version, Enhance the difficulty of cracking. :superhappy:

AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

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

Post by AHKStudent » 05 Jun 2018, 12:57

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?

alware
Posts: 8
Joined: 01 Jun 2018, 02:25

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

Post by alware » 06 Jun 2018, 04:36

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??

feiyue
Posts: 381
Joined: 08 Aug 2014, 04:08

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

Post by feiyue » 07 Jun 2018, 01:25

@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.
Last edited by feiyue on 07 Jun 2018, 18:35, edited 1 time in total.

Post Reply

Return to “脚本函数”