-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimpexp.txt
250 lines (215 loc) · 8.83 KB
/
impexp.txt
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
248
249
250
#This project is mainly for simplicity and to have quick access to the imphash, exphash was made for fun...
#If you are reading this, you have passed the static analysis check.
#Achievement Unlocked: Read the code before you execute the code.
#Experience: +50 points
#Level: Expert
function Get-ImpHash {
param (
[Parameter(Mandatory=$true, Position=0)]
[string]$FilePath,
[string]$Algorithm = "md5",
[string]$Delimiter = ", ",
[switch]$ShowTable
)
if ($FilePath -eq "-h" -or $FilePath -eq "--help" -or $FilePath -eq "imphash--help") {
Show-Help "Get-ImpHash"
return
}
if ($FilePath -eq "*") {
Get-ChildItem | ForEach-Object {
Get-ImpHash -FilePath $_.FullName -Algorithm $Algorithm -Delimiter $Delimiter -ShowTable:$ShowTable
}
return
}
if (-not (Test-Path $FilePath)) {
Write-Host "File not found: $FilePath"
return
}
$pythonScript = @"
import sys
import pefile
import hashlib
def calculate_hash(file_path, algorithm, delimiter, show_table):
try:
pe = pefile.PE(file_path)
impstrs = []
exts = ["ocx", "sys", "dll"]
if not hasattr(pe, "DIRECTORY_ENTRY_IMPORT"):
return ""
for entry in pe.DIRECTORY_ENTRY_IMPORT:
if isinstance(entry.dll, bytes):
libname = entry.dll.decode().lower()
else:
libname = entry.dll.lower()
parts = libname.rsplit(".", 1)
if len(parts) > 1 and parts[1] in exts:
libname = parts[0]
entry_dll_lower = entry.dll.lower()
for imp in entry.imports:
funcname = None
if not imp.name:
funcname = ordlookup.ordLookup(
entry_dll_lower, imp.ordinal, make_name=True
)
if not funcname:
raise PEFormatError(
f"Unable to look up ordinal {entry.dll}:{imp.ordinal:04x}"
)
else:
funcname = imp.name
if not funcname:
continue
if isinstance(funcname, bytes):
funcname = funcname.decode()
impstrs.append("%s.%s" % (libname.lower(), funcname.lower()))
impstrs_str = ",".join(impstrs) # Join import strings with commas
impstrs_display = delimiter.join(impstrs) if show_table else impstrs_str # Join with custom delimiter for display
if algorithm.lower() == "md5":
hash_val = hashlib.md5(impstrs_str.encode()).hexdigest()
elif algorithm.lower() == "sha1":
hash_val = hashlib.sha1(impstrs_str.encode()).hexdigest()
elif algorithm.lower() == "sha256":
hash_val = hashlib.sha256(impstrs_str.encode()).hexdigest()
elif algorithm.lower() == "sha512":
hash_val = hashlib.sha512(impstrs_str.encode()).hexdigest()
else:
return "Invalid algorithm specified."
return hash_val, impstrs_display
except pefile.PEFormatError:
return "Not a valid PE file", ""
except Exception as e:
return "Error: " + str(e), ""
file_path = sys.argv[1]
algorithm = sys.argv[2]
delimiter = sys.argv[3]
show_table = sys.argv[4].lower() == "true"
hash_val, impstrs_str = calculate_hash(file_path, algorithm, delimiter, show_table)
print(hash_val)
print(impstrs_str)
"@
$pythonScriptPath = Join-Path -Path $env:TEMP -ChildPath "pe_hash_script.py"
$pythonScript | Out-File -FilePath $pythonScriptPath -Encoding utf8
$pythonCommand = "python $pythonScriptPath ""$FilePath"" ""$Algorithm"" ""$Delimiter"" ""$ShowTable"""
$result = Invoke-Expression -Command $pythonCommand
$output = $result -split "`n"
$hash = $output[0]
$impstrs = $output[1]
if ($hash -like "Error:*") {
Write-Host "Error: $hash"
} else {
if ($ShowTable) {
Write-Host "IAT Table:`n`"$impstrs`""
}
Write-Host "$Algorithm`t$hash`t$FilePath"
}
}
function Get-ExpHash {
param (
[Parameter(Mandatory=$true, Position=0)]
[string]$FilePath,
[string]$Algorithm = "sha256",
[string]$Delimiter = ", ",
[switch]$ShowTable
)
if ($FilePath -eq "-h" -or $FilePath -eq "--help" -or $FilePath -eq "exphash--help") {
Show-Help "Get-ExpHash"
return
}
if ($FilePath -eq "*") {
Get-ChildItem | ForEach-Object {
Get-ExpHash -FilePath $_.FullName -Algorithm $Algorithm -Delimiter $Delimiter -ShowTable:$ShowTable
}
return
}
if (-not (Test-Path $FilePath)) {
Write-Host "File not found: $FilePath"
return
}
$pythonScript = @"
import sys
import pefile
import hashlib
def calculate_hash(file_path, algorithm, delimiter, show_table):
try:
pe = pefile.PE(file_path)
expstrs = []
exts = ["ocx", "sys", "dll"]
if not hasattr(pe, "DIRECTORY_ENTRY_EXPORT"):
return ""
for exp in pe.DIRECTORY_ENTRY_EXPORT.symbols:
if isinstance(exp.name, bytes):
expname = exp.name.decode().lower()
else:
expname = exp.name.lower()
if isinstance(expname, bytes):
expname = expname.decode()
expstrs.append(expname)
expstrs_str = ",".join(expstrs) # Join export strings with commas
expstrs_display = delimiter.join(expstrs) if show_table else expstrs_str # Join with custom delimiter for display
if algorithm.lower() == "md5":
hash_val = hashlib.md5(expstrs_str.encode()).hexdigest()
elif algorithm.lower() == "sha1":
hash_val = hashlib.sha1(expstrs_str.encode()).hexdigest()
elif algorithm.lower() == "sha256":
hash_val = hashlib.sha256(expstrs_str.encode()).hexdigest()
elif algorithm.lower() == "sha512":
hash_val = hashlib.sha512(expstrs_str.encode()).hexdigest()
else:
return "Invalid algorithm specified."
return hash_val, expstrs_display
except pefile.PEFormatError:
return "Not a valid PE file", ""
except Exception as e:
return "Error: " + str(e), ""
file_path = sys.argv[1]
algorithm = sys.argv[2]
delimiter = sys.argv[3]
show_table = sys.argv[4].lower() == "true"
hash_val, expstrs_str = calculate_hash(file_path, algorithm, delimiter, show_table)
print(hash_val)
print(expstrs_str)
"@
$pythonScriptPath = Join-Path -Path $env:TEMP -ChildPath "pe_hash_script.py"
$pythonScript | Out-File -FilePath $pythonScriptPath -Encoding utf8
$pythonCommand = "python $pythonScriptPath ""$FilePath"" ""$Algorithm"" ""$Delimiter"" ""$ShowTable"""
$result = Invoke-Expression -Command $pythonCommand
$output = $result -split "`n"
$hash = $output[0]
$expstrs = $output[1]
if ($hash -like "Error:*") {
Write-Host "Error: $hash"
} else {
if ($ShowTable) {
Write-Host "EAT Table:`n`"$expstrs`""
}
Write-Host "$Algorithm`t$hash`t$FilePath"
}
}
function Show-Help {
param (
[string]$Command
)
if ($Command -eq "Get-ImpHash") {
Write-Host ""
Write-Host "Usage: Get-ImpHash <FilePath> [-Algorithm <Algorithm>] [-Delimiter <Delimiter>] [-ShowTable]"
Write-Host " Get-ImpHash * -Algorithm sha512 -Delimiter ' | ' -ShowTable"
Write-Host ""
Write-Host " --help -> Get the help manual"
Write-Host " -Algorithm -> Specify the algorithm (MD5, SHA1, SHA256, SHA512). Default: MD5"
Write-Host " -Delimiter -> Specify the delimiter for the IAT table entries. Default: ', '"
Write-Host " * -> Get the hash of all files in the present directory."
Write-Host " -ShowTable -> Print the IAT Table."
Write-Host ""
} elseif ($Command -eq "Get-ExpHash") {
Write-Host ""
Write-Host "Usage: Get-ExpHash <FilePath> [-Algorithm <Algorithm>] [-Delimiter <Delimiter>] [-ShowTable]"
Write-Host " Get-ExpHash * -Algorithm sha512 -Delimiter ' | ' -ShowTable"
Write-Host ""
Write-Host " --help -> Get the help manual"
Write-Host " -Algorithm -> Specify the algorithm (MD5, SHA1, SHA256, SHA512). Default: SHA256"
Write-Host " -Delimiter -> Specify the delimiter for the EAT table entries. Default: ', '"
Write-Host " * -> Get the hash of all files in the present directory."
Write-Host " -ShowTable -> Print the EAT Table."
Write-Host ""
}
}