-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog4hunter.php
242 lines (183 loc) · 6.04 KB
/
log4hunter.php
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
<?php
/**
* Return fake Java headers from a Jboss Wildfly.
*/
function fakeHttpJavaHeaders(){
header("Server: WildFly/8",true);
header("X-Powered-By: Undertow/1",true);
}
/**
* Return the storage path.
*/
function getStoragePath($filename){
$path = "./data/" . date('Y-m-d') . "/". $_SERVER['REMOTE_ADDR']. "/";
if (!file_exists($path)) {
// create directory
mkdir($path, 0777, true);
}
return $path . $filename;
}
/**
* Save data in file.
*/
function saveFile($filename, $data){
file_put_contents(getStoragePath($filename), $data);
}
/**
* Decompile Java Class file
*/
function decompile($classFileName, $javaFileName){
$classFilePath = getStoragePath($classFileName);
$javaFilePath = getStoragePath($javaFileName);
$command = "java -jar ./lib/cfr-0.152.jar $classFilePath > $javaFilePath";
echo $command;
exec($command);
}
/**
* A Simple logger.
*/
function logAttack($log_msg, $isEnd = false) {
$log_file_path = getStoragePath("attack.log");
// write the content
$data = "> " . $log_msg . "\n";
file_put_contents($log_file_path, $data, FILE_APPEND);
if($isEnd){
file_put_contents($log_file_path,"---------------------------------------------------\n\n",FILE_APPEND);
}
}
/**
* Download a remote data.
*/
function downloadData($url, $logResponse = true){
// Create curl resource.
$ch = curl_init();
// Set tyarget url.
curl_setopt($ch, CURLOPT_URL, $url);
// Return the transfer as a string.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// We wait maximum 20 sec.
curl_setopt($ch,CURLOPT_TIMEOUT,20);
// Download the infected payload from the hacker server.
$data = curl_exec($ch);
// Get Status Code.
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// We save it for analysis.
if ($logResponse){
logAttack("PAYLOAD:$url ($http_code)\n" . $data);
}
// close curl resource to free up system resources.
curl_close($ch);
return $data;
}
/**
* Analyze the LDAP URL in the JNDI Request.
*/
function analyzeLdapUrl($payload){
// Check for Basic64 Command.
$pointer = strpos($payload, "//");
// The JNDI contains a Basic64 command.
if ($pointer == FALSE){
logAttack("No LDAP URL found !");
return false;
}
// Generate an unobfruscated LDAP URL
$ldapurl = "ldap://" . substr($payload, $pointer + 2, -1);
// Capture the remote payload
$data = downloadData($ldapurl);
analyzeLdapEntry($data);
}
/**
* Analyze the LDAP Entry.
*/
function analyzeLdapEntry($entry){
// We extract the LDAP Entry
$lines = explode("\n", $entry);
// Remove the first DN line
array_shift($lines);
// Remove empty lines
$lines = array_filter($lines);
$ldapEntry = [];
// For each key / value lines
foreach($lines as $line){
$line = trim($line);
list($key,$value) = explode(": ", $line);
$ldapEntry[$key] = $value;
}
$codeFound = false;
// The java code is serialized into the LDAP Entry
if (array_key_exists("javaSerializedData",$ldapEntry)){
// TODO : Check the safetyness of javaFactory value
$classFileName = $ldapEntry['javaFactory'].".class";
$javaFileName = $ldapEntry['javaFactory'].".java";
logAttack("javaSerializedData Found : ". $classFileName);
$data = $ldapEntry['javaSerializedData'];
$codeFound = true;
}
// If we find a remote Java code base we download the class content
else if (array_key_exists("javaCodeBase",$ldapEntry) && array_key_exists("javaFactory",$ldapEntry)){
$classFileName = $ldapEntry['javaFactory'].".class";
$javaFileName = $ldapEntry['javaFactory'].".java";
$data = downloadData($ldapEntry['javaCodeBase']."/". $classFileName, false);
$codeFound = true;
}
// If a Java code is found, we save it and decompile it !
if ($codeFound){
logAttack("javaCodeBase Found : " . $classFileName);
saveFile( $classFileName, $data);
decompile($classFileName,$javaFileName);
}
}
/**
* Analyze the Base64 shell command.
*/
function analyzeBase64Command($payload){
// Check for Basic64 Command.
$pointer = strpos($payload, "Base64");
// The JNDI contains a Basic64 command.
if ($pointer == FALSE){
logAttack("No base64 shell code found !");
return false;
}
// We extract the base64 encoded command.
$basic64 = substr($payload, $pointer + 7, -1);
// We decode the shell command and log it.
$command = base64_decode($basic64);
// We split the shell command into unique instructions for analysis.
$command = str_replace(array("(",")"), "", $command);
$command = str_replace(array("||"), "|", $command);
$instructions = explode('|',$command);
// We check each instruction to detect if it's a curl or wget command.
foreach($instructions as $instruction){
// If it's a curl or wget command.
if (strpos($instruction,"curl") !== FALSE || strpos($instruction,"wget") !== FALSE){
// We extract all args of the shell instruction.
$args = explode(" ", $instruction);
// The URL is the last arg of the string.
$url = end($args);
// We log the remote payload.
downloadData($url);
}
}
}
/**
* Analyze the request and return true if an attack is detected.
*/
function analyzeRequest($data){
$isAttack = false;
// We try to find a dollar in the different values that can be a proof of log4shell attack.
foreach ( $data as $key => $value) {
// We found a dollar in a HTTP header or Request.
if (strpos($value, '$') !== FALSE) {
$isAttack = true;
logAttack($key . ":" . print_r($value,true));
// Analyze the LDAP URL Content
analyzeLdapUrl($value);
// Analyze the Shell command
analyzeBase64Command($value);
}
}
if ($isAttack){
logAttack("Finished !",true);
}
return $isAttack;
}