Skip to content

Commit 764aae7

Browse files
committed
Refactoring.
1 parent 91b026d commit 764aae7

File tree

1 file changed

+69
-70
lines changed

1 file changed

+69
-70
lines changed

src/CLI.php

Lines changed: 69 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -140,41 +140,41 @@ public function __construct(\phpMussel\Core\Loader &$Loader, \phpMussel\Core\Sca
140140
elseif ($Command === 'url_sig') {
141141
echo "\n";
142142
$Clean = $this->Scanner->normalise(substr($Clean, strlen($Command) + 1));
143-
$URL = ['avoidme' => '', 'forthis' => ''];
143+
$URL = ['AvoidMe' => '', 'ForThis' => ''];
144144
if (
145145
!preg_match_all('/(data|file|https?|ftps?|sftp|ss[hl])\:\/\/(www\d{0,3}\.)?([\da-z.-]{1,512})/i', $Clean, $URL['domain']) ||
146146
!preg_match_all('/(data|file|https?|ftps?|sftp|ss[hl])\:\/\/(www\d{0,3}\.)?([\!\#\$\&-;\=\?\@-\[\]_a-z~]{1,4000})/i', $Clean, $URL['url'])
147147
) {
148148
echo $this->Loader->L10N->getString('invalid_url') . "\n";
149-
} else {
150-
echo 'DOMAIN:' . md5($URL['domain'][3][0]) . ':' . strlen($URL['domain'][3][0]) . ':' . $this->Loader->L10N->getString('cli_signature_placeholder') . "\n";
151-
$URL['forthis'] = md5($URL['url'][3][0]) . ':' . strlen($URL['url'][3][0]);
152-
$URL['avoidme'] .= ',' . $URL['forthis'] . ',';
153-
echo 'URL:' . $URL['forthis'] . ':' . $this->Loader->L10N->getString('cli_signature_placeholder') . "\n";
154-
if (preg_match('/[^\da-z.-]$/i', $URL['url'][3][0])) {
155-
$URL['x'] = preg_replace('/[^\da-z.-]+$/i', '', $URL['url'][3][0]);
156-
$URL['forthis'] = md5($URL['x']) . ':' . strlen($URL['x']);
157-
if (strpos($URL['avoidme'], $URL['forthis']) === false) {
158-
$URL['avoidme'] .= ',' . $URL['forthis'] . ',';
159-
echo 'URL:' . $URL['forthis'] . ':' . $this->Loader->L10N->getString('cli_signature_placeholder') . "\n";
160-
}
149+
continue;
150+
}
151+
echo 'DOMAIN:' . hash('md5', $URL['domain'][3][0]) . ':' . strlen($URL['domain'][3][0]) . ':' . $this->Loader->L10N->getString('cli_signature_placeholder') . "\n";
152+
$URL['ForThis'] = hash('md5', $URL['url'][3][0]) . ':' . strlen($URL['url'][3][0]);
153+
$URL['AvoidMe'] .= ',' . $URL['ForThis'] . ',';
154+
echo 'URL:' . $URL['ForThis'] . ':' . $this->Loader->L10N->getString('cli_signature_placeholder') . "\n";
155+
if (preg_match('/[^\da-z.-]$/i', $URL['url'][3][0])) {
156+
$URL['x'] = preg_replace('/[^\da-z.-]+$/i', '', $URL['url'][3][0]);
157+
$URL['ForThis'] = hash('md5', $URL['x']) . ':' . strlen($URL['x']);
158+
if (strpos($URL['AvoidMe'], $URL['ForThis']) === false) {
159+
$URL['AvoidMe'] .= ',' . $URL['ForThis'] . ',';
160+
echo 'URL:' . $URL['ForThis'] . ':' . $this->Loader->L10N->getString('cli_signature_placeholder') . "\n";
161+
}
162+
}
163+
if (strpos($URL['url'][3][0], '?') !== false) {
164+
$URL['x'] = $this->Loader->substrBeforeFirst($URL['url'][3][0], '?');
165+
$URL['ForThis'] = hash('md5', $URL['x']) . ':' . strlen($URL['x']);
166+
if (strpos($URL['AvoidMe'], $URL['ForThis']) === false) {
167+
$URL['AvoidMe'] .= ',' . $URL['ForThis'] . ',';
168+
echo 'URL:' . $URL['ForThis'] . ':' . $this->Loader->L10N->getString('cli_signature_placeholder') . "\n";
161169
}
162-
if (strpos($URL['url'][3][0], '?') !== false) {
163-
$URL['x'] = $this->Loader->substrBeforeFirst($URL['url'][3][0], '?');
164-
$URL['forthis'] = md5($URL['x']) . ':' . strlen($URL['x']);
165-
if (strpos($URL['avoidme'], $URL['forthis']) === false) {
166-
$URL['avoidme'] .= ',' . $URL['forthis'] . ',';
167-
echo 'URL:' . $URL['forthis'] . ':' . $this->Loader->L10N->getString('cli_signature_placeholder') . "\n";
168-
}
169-
$URL['x'] = $this->Loader->substrAfterFirst($URL['url'][3][0], '?');
170-
$URL['forthis'] = md5($URL['x']) . ':' . strlen($URL['x']);
171-
if (
172-
strpos($URL['avoidme'], $URL['forthis']) === false &&
173-
$URL['forthis'] != 'd41d8cd98f00b204e9800998ecf8427e:0'
174-
) {
175-
$URL['avoidme'] .= ',' . $URL['forthis'] . ',';
176-
echo 'QUERY:' . $URL['forthis'] . ':' . $this->Loader->L10N->getString('cli_signature_placeholder') . "\n";
177-
}
170+
$URL['x'] = $this->Loader->substrAfterFirst($URL['url'][3][0], '?');
171+
$URL['ForThis'] = hash('md5', $URL['x']) . ':' . strlen($URL['x']);
172+
if (
173+
strpos($URL['AvoidMe'], $URL['ForThis']) === false &&
174+
$URL['ForThis'] !== 'd41d8cd98f00b204e9800998ecf8427e:0'
175+
) {
176+
$URL['AvoidMe'] .= ',' . $URL['ForThis'] . ',';
177+
echo 'QUERY:' . $URL['ForThis'] . ':' . $this->Loader->L10N->getString('cli_signature_placeholder') . "\n";
178178
}
179179
}
180180
unset($URL);
@@ -299,58 +299,57 @@ private function peMeta(string $File): string
299299
if (substr($Data, 0, 2) !== 'MZ') {
300300
return $this->Loader->L10N->getString('cli_pe1') . "\n";
301301
}
302-
$PEArr = ['Len' => strlen($Data)];
303-
$PEArr['Offset'] = $this->Loader->unpackSafe('S', substr($Data, 60, 4));
304-
$PEArr['Offset'] = $PEArr['Offset'][1];
302+
$PELength = strlen($Data);
303+
$Offset = $this->Loader->unpackSafe('S', substr($Data, 60, 4));
304+
$Offset = $Offset[1];
305305
while (true) {
306-
$PEArr['DoScan'] = true;
307-
if ($PEArr['Offset'] < 1 || $PEArr['Offset'] > 16384 || $PEArr['Offset'] > $PEArr['Len']) {
308-
$PEArr['DoScan'] = false;
306+
$Valid = true;
307+
if ($Offset < 1 || $Offset > 16384 || $Offset > $PELength) {
308+
$Valid = false;
309309
break;
310310
}
311-
$PEArr['Magic'] = substr($Data, $PEArr['Offset'], 2);
312-
if ($PEArr['Magic'] !== 'PE') {
313-
$PEArr['DoScan'] = false;
311+
$Magic = substr($Data, $Offset, 2);
312+
if ($Magic !== 'PE') {
313+
$Valid = false;
314314
break;
315315
}
316-
$PEArr['Proc'] = $this->Loader->unpackSafe('S', substr($Data, $PEArr['Offset'] + 4, 2));
317-
$PEArr['Proc'] = $PEArr['Proc'][1];
318-
if ($PEArr['Proc'] != 0x14c && $PEArr['Proc'] != 0x8664) {
319-
$PEArr['DoScan'] = false;
316+
$Proc = $this->Loader->unpackSafe('S', substr($Data, $Offset + 4, 2));
317+
$Proc = $Proc[1];
318+
if ($Proc != 0x14c && $Proc != 0x8664) {
319+
$Valid = false;
320320
break;
321321
}
322-
$PEArr['NumOfSections'] = $this->Loader->unpackSafe('S', substr($Data, $PEArr['Offset'] + 6, 2));
323-
$PEArr['NumOfSections'] = $PEArr['NumOfSections'][1];
324-
if ($PEArr['NumOfSections'] < 1 || $PEArr['NumOfSections'] > 40) {
325-
$PEArr['DoScan'] = false;
322+
$NumberOfSections = $this->Loader->unpackSafe('S', substr($Data, $Offset + 6, 2));
323+
$NumberOfSections = $NumberOfSections[1];
324+
if ($NumberOfSections < 1 || $NumberOfSections > 40) {
325+
$Valid = false;
326326
}
327327
break;
328328
}
329-
if (!$PEArr['DoScan']) {
329+
if (!$Valid) {
330330
return $this->Loader->L10N->getString('cli_pe1') . "\n";
331331
}
332-
$PEArr['OptHdrSize'] = $this->Loader->unpackSafe('S', substr($Data, $PEArr['Offset'] + 20, 2));
333-
$PEArr['OptHdrSize'] = $PEArr['OptHdrSize'][1];
332+
$OptHdrSize = $this->Loader->unpackSafe('S', substr($Data, $Offset + 20, 2));
333+
$OptHdrSize = $OptHdrSize[1];
334334
$Returnable .= $this->Loader->L10N->getString('cli_pe2') . "\n";
335-
for ($PEArr['k'] = 0; $PEArr['k'] < $PEArr['NumOfSections']; $PEArr['k']++) {
336-
$PEArr['SectionHead'] = substr($Data, $PEArr['Offset'] + 24 + $PEArr['OptHdrSize'] + ($PEArr['k'] * 40), $PEArr['NumOfSections'] * 40);
337-
$PEArr['SectionName'] = str_ireplace("\x00", '', substr($PEArr['SectionHead'], 0, 8));
338-
$PEArr['VirtualSize'] = $this->Loader->unpackSafe('S', substr($PEArr['SectionHead'], 8, 4));
339-
$PEArr['VirtualSize'] = $PEArr['VirtualSize'][1];
340-
$PEArr['VirtualAddress'] = $this->Loader->unpackSafe('S', substr($PEArr['SectionHead'], 12, 4));
341-
$PEArr['VirtualAddress'] = $PEArr['VirtualAddress'][1];
342-
$PEArr['SizeOfRawData'] = $this->Loader->unpackSafe('S', substr($PEArr['SectionHead'], 16, 4));
343-
$PEArr['SizeOfRawData'] = $PEArr['SizeOfRawData'][1];
344-
$PEArr['PointerToRawData'] = $this->Loader->unpackSafe('S', substr($PEArr['SectionHead'], 20, 4));
345-
$PEArr['PointerToRawData'] = $PEArr['PointerToRawData'][1];
346-
$PEArr['SectionData'] = substr($Data, $PEArr['PointerToRawData'], $PEArr['SizeOfRawData']);
347-
$PEArr['SHA256'] = hash('sha256', $PEArr['SectionData']);
348-
$Returnable .= $PEArr['SizeOfRawData'] . ':' . $PEArr['SHA256'] . ':' . $PEArr['SectionName'] . "\n";
335+
for ($PECaret = 0; $PECaret < $NumberOfSections; $PECaret++) {
336+
$SectionHead = substr($Data, $Offset + 24 + $OptHdrSize + ($PECaret * 40), $NumberOfSections * 40);
337+
$SectionName = str_ireplace("\x00", '', substr($SectionHead, 0, 8));
338+
$VirtualSize = $this->Loader->unpackSafe('S', substr($SectionHead, 8, 4));
339+
$VirtualSize = $VirtualSize[1];
340+
$VirtualAddress = $this->Loader->unpackSafe('S', substr($SectionHead, 12, 4));
341+
$VirtualAddress = $VirtualAddress[1];
342+
$SizeOfRawData = $this->Loader->unpackSafe('S', substr($SectionHead, 16, 4));
343+
$SizeOfRawData = $SizeOfRawData[1];
344+
$PointerToRawData = $this->Loader->unpackSafe('S', substr($SectionHead, 20, 4));
345+
$PointerToRawData = $PointerToRawData[1];
346+
$SectionData = substr($Data, $PointerToRawData, $SizeOfRawData);
347+
$SHA256 = hash('sha256', $SectionData);
348+
$Returnable .= $SizeOfRawData . ':' . $SHA256 . ':' . $SectionName . "\n";
349349
}
350350
$Returnable .= "\n";
351351
if (strpos($Data, "V\x00a\x00r\x00F\x00i\x00l\x00e\x00I\x00n\x00f\x00o\x00\x00\x00\x00\x00\x24") !== false) {
352-
$PEArr['Parts'] = $this->Loader->substrAfterLast($Data, "V\x00a\x00r\x00F\x00i\x00l\x00e\x00I\x00n\x00f\x00o\x00\x00\x00\x00\x00\x24");
353-
$PEArr['FINFO'] = [];
352+
$PEParts = $this->Loader->substrAfterLast($Data, "V\x00a\x00r\x00F\x00i\x00l\x00e\x00I\x00n\x00f\x00o\x00\x00\x00\x00\x00\x24");
354353
foreach ([
355354
["F\x00i\x00l\x00e\x00D\x00e\x00s\x00c\x00r\x00i\x00p\x00t\x00i\x00o\x00n\x00\x00\x00", 'PEFileDescription'],
356355
["F\x00i\x00l\x00e\x00V\x00e\x00r\x00s\x00i\x00o\x00n\x00\x00\x00", 'PEFileVersion'],
@@ -360,17 +359,17 @@ private function peMeta(string $File): string
360359
["O\x00r\x00i\x00g\x00i\x00n\x00a\x00l\x00F\x00i\x00l\x00e\x00n\x00a\x00m\x00e\x00\x00\x00", 'PEOriginalFilename'],
361360
["C\x00o\x00m\x00p\x00a\x00n\x00y\x00N\x00a\x00m\x00e\x00\x00\x00", 'PECompanyName'],
362361
] as $PEVars) {
363-
if (strpos($PEArr['Parts'], $PEVars[0]) !== false && (
364-
$PEArr['ThisData'] = trim(str_ireplace("\x00", '', $this->Loader->substrBeforeFirst(
365-
$this->Loader->substrAfterLast($PEArr['Parts'], $PEVars[0]),
362+
if (strpos($PEParts, $PEVars[0]) !== false && (
363+
$ThisPEData = trim(str_ireplace("\x00", '', $this->Loader->substrBeforeFirst(
364+
$this->Loader->substrAfterLast($PEParts, $PEVars[0]),
366365
"\x00\x00\x00"
367366
)))
368367
)) {
369368
$Returnable .= sprintf(
370369
"\$%s:%s:%d:%s\n",
371370
$PEVars[1],
372-
hash('sha256', $PEArr['ThisData']),
373-
strlen($PEArr['ThisData']),
371+
hash('sha256', $ThisPEData),
372+
strlen($ThisPEData),
374373
$this->Loader->L10N->getString('cli_signature_placeholder')
375374
);
376375
}

0 commit comments

Comments
 (0)