-
Notifications
You must be signed in to change notification settings - Fork 72
/
Decompiler.ahk
247 lines (216 loc) · 7.83 KB
/
Decompiler.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#NoEnv
SetBatchLines, -1
Gui, Font, s16, Arial
Gui, Add, Edit, x10 y10 w450 h30 vPath gUpdate
Gui, Add, Button, x460 y10 w30 h30 gSelectFile, ...
Gui, Font, s12, Courier New
Gui, Add, Edit, x10 y50 w480 h340 vCode ReadOnly -Wrap, <Output>
Gui, Show, w496 h398, AutoHotkey Decompiler
If 0 = 1
GuiControl,, Path, %1%
Return
GuiClose:
ExitApp
Update:
SetTimer, Decompile, -800
Return
GuiDropFiles:
Loop, Parse, A_GuiEvent, `n
{
GuiControl,, Path, %A_LoopField%
Break
}
Return
SelectFile:
FileSelectFile, Path, 3,, Select an AutoHotkey executable to decompile, *.exe
If !ErrorLevel
GuiControl,, Path, %Path%
Return
Decompile:
GuiControlGet, Path,, Path
Attributes := FileExist(Path)
If !Attributes
{
GuiControl,, Code, <Error>`nInvalid path: "%Path%".
Return
}
If InStr(Attributes,"D")
{
GuiControl,, Code, <Error>`nInvalid file: %Path%.
Return
}
GuiControl,, Code, <Decompiling>
try Code := Decompile(Path)
catch e
{
GuiControl,, Code, % "<Error>`n" . e.Message . "`nFunction: " . e.What . " (Line " . e.Line . ")"
Return
}
GuiControl,, Code, %Code%
Return
Decompile(Path)
{
ExtractDir := A_ScriptDir . "\ExtractFiles"
PatchedPath := ExtractDir . "\Patched.exe"
Payload := ExtractDir . "\winmm.dll"
;set up temporary directory
try FileCreateDir, %ExtractDir%
catch e
throw Exception("Could not create temporary extraction directory: """ . ExtractDir . """.")
;make a copy of the executable to work with
try FileCopy, %Path%, %PatchedPath%, 1
catch e
{
try FileRemoveDir, %ExtractDir%, 1 ;clean up temporary directory
throw Exception("Invalid executable path: """ . Path . """.")
}
;read the executable contents
Executable := FileOpen(PatchedPath,"rw")
If !Executable
{
try FileRemoveDir, %ExtractDir%, 1 ;clean up temporary directory
throw Exception("Could not access executable: """ . PatchedPath . """.")
}
Length := Executable.RawRead(Buffer,Executable.Length)
If Length < 64 ;minimum executable size
{
try FileRemoveDir, %ExtractDir%, 1 ;clean up temporary directory
throw Exception("Could not read executable: """ . PatchedPath . """.")
}
try SectionLength := GetSectionOffset(Buffer,".rsrc",SectionDataOffset) ;search for the resources section
catch e
{
try FileRemoveDir, %ExtractDir%, 1 ;clean up temporary directory
throw e
}
PatchedNames := [">UHK WITH ICON<",">UUTOHOTKEY SCRIPT<"]
Success := False
For Index, Name In [">AHK WITH ICON<",">AUTOHOTKEY SCRIPT<"]
{
;obtain resource name in UTF-16
Size := StrLen(Name)
VarSetCapacity(uName,Size << 1)
StrPut(Name,&uName,Size,"UTF-16")
;patch resource name if found
Offset := SearchBuffer(&Buffer + SectionDataOffset,SectionLength,uName,Size)
If Offset = -1
Continue
Offset += SectionDataOffset ;obtain absolute offset
Success := True
;obtain new resource name in UTF-16
NewName := PatchedNames[Index]
Size := StrLen(NewName)
VarSetCapacity(uName,Size << 1)
StrPut(NewName,&uName,Size,"UTF-16")
;patch the resource name to prevent access by executable
If !Executable.Seek(Offset)
{
try FileRemoveDir, %ExtractDir%, 1 ;clean up temporary directory
throw Exception("Could not seek to resource position: " . Offset . ".")
}
If !Executable.RawWrite(uName,Size << 1)
{
try FileRemoveDir, %ExtractDir%, 1 ;clean up temporary directory
throw Exception("Could not patch resource: """ . NewName . """.")
}
}
If !Success ;resources not found
{
try FileRemoveDir, %ExtractDir%, 1 ;clean up temporary directory
throw Exception("Invalid executable script resources: """ . PatchedPath . """.")
}
Executable.Close()
;determine the correct payload to use
BinaryType := 0
If !DllCall("GetBinaryType" . (A_IsUnicode ? "W" : "A"),"Str",PatchedPath,"UInt*",BinaryType)
throw Exception("Could not determine executable type: """ . PatchedPath . """.")
If BinaryType = 0 ;SCS_32BIT_BINARY
PayloadPath := A_ScriptDir . "\payload.dll"
Else If BinaryType = 6 ;SCS_64BIT_BINARY
PayloadPath := A_ScriptDir . "\payload64.dll"
Else
{
try FileRemoveDir, %ExtractDir%, 1 ;clean up temporary directory
throw Exception("Invalid executable type: """ . BinaryType . """.")
}
;deploy payload
try FileCopy, %PayloadPath%, %Payload%, 1
catch e
{
try FileRemoveDir, %ExtractDir%, 1 ;clean up temporary directory
throw Exception("Could not deploy payload: """ . PayloadPath . """.")
}
;run the executable
try RunWait, %PatchedPath%, %ExtractDir%
catch e
{
try FileRemoveDir, %ExtractDir%, 1 ;clean up temporary directory
throw Exception("Could not run executable: """ . PatchedPath . """.")
}
;obtain script extracted by payload
SplitPath, PatchedPath,, OutDir,, OutNameNoExt
ScriptPath := OutDir . "\" . OutNameNoExt . "-uncompiled.ahk"
try FileRead, Code, %ScriptPath%
catch e
{
try FileRemoveDir, %ExtractDir%, 1 ;clean up temporary directory
throw Exception("Could read script code: """ . ScriptPath . """.")
}
try FileRemoveDir, %ExtractDir%, 1 ;clean up temporary directory
Return, Code
}
GetSectionOffset(ByRef Buffer,Name,ByRef DataOffset)
{
If NumGet(Buffer,0,"UShort") != 0x5A4D ;MS-DOS executable marker
throw Exception("Invalid MS-DOS signature.")
Offset := NumGet(Buffer,60,"UInt") ;read the "e_lfanew" field of the MS-DOS header for the file address of the PE header
If NumGet(Buffer,Offset,"UInt") != 0x4550 ;IMAGE_NT_SIGNATURE
throw Exception("Invalid NT signature.")
Offset += 4 ;move past the file signature
SectionsCount := NumGet(Buffer,Offset + 2,"UShort") ;read the "NumberOfSections" field of the IMAGE_FILE_HEADER structure for the number of sections present
OptionalHeaderSize := NumGet(Buffer,Offset + 16,"UShort") ;read the "SizeOfOptionalHeader" field of the IMAGE_FILE_HEADER structure for the size of the optional header
Offset += 20 ;move past the IMAGE_FILE_HEADER structure
Marker := NumGet(Buffer,Offset,"UShort") ;IMAGE_OPTIONAL_HEADER marker
If (Marker != 0x10B && Marker != 0x20B) ;IMAGE_NT_OPTIONAL_HDR32_MAGIC, IMAGE_NT_OPTIONAL_HDR64_MAGIC
throw Exception("Invalid PE signature.")
Offset += OptionalHeaderSize ;move past the IMAGE_OPTIONAL_HEADER structure
Loop, %SectionsCount% ;loop through each section header
{
If StrGet(&Buffer + Offset,8,"UTF-8") = Name ;found header
{
DataLength := NumGet(Buffer,Offset + 16,"UInt")
DataOffset := NumGet(Buffer,Offset + 20,"UInt")
Return, DataLength
}
Offset += 40 ;move past the IMAGE_SECTION_HEADER structure
}
throw Exception("Could not find PE file section: """ . Name . """.")
}
SearchBuffer(pBuffer,BufferSize,ByRef Search,SearchSize) ;Boyer-Moore-Horspool algorithm
{
;preprocess search input
ShiftTable := []
Loop, 255
ShiftTable[A_Index] := SearchSize
Loop, % SearchSize - 1
ShiftTable[NumGet(Search,A_Index - 1,"UChar")] := SearchSize - A_Index
;search buffer
Offset := 0
While, BufferSize >= SearchSize
{
;scan from the end of the search input
Position := SearchSize - 1
While, NumGet(pBuffer + Offset,Position,"UChar") = NumGet(Search,Position,"UChar")
{
If Position = 0
Return, Offset
Position --
}
;shift buffer if not found
Value := ShiftTable[NumGet(pBuffer + Offset,SearchSize - 1,"UChar")]
Offset += Value
BufferSize -= Value
}
;search input not found
Return, -1
}