|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Aszone\Vulnerabilities; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
|
6
|
|
|
use Psr\Log\LoggerInterface; |
|
7
|
|
|
use Aszone\Vulnerabilities\Log\Logger; |
|
8
|
|
|
|
|
9
|
|
|
class CrossSiteScripting implements VulnerabilityScanner |
|
10
|
|
|
{ |
|
11
|
|
|
const EXPLOIT1 = '<script>alert(aaabbbccc);</script>'; |
|
12
|
|
|
const EXPLOIT2 = '<h1>aaabbbccc</h1>'; |
|
13
|
|
|
const EXPLOIT1REGEX = "<script>alert\(aaabbbccc\);<\/script>"; |
|
14
|
|
|
const EXPLOIT2REGEX = "<h1>aaabbbccc<\/h1>"; |
|
15
|
|
|
|
|
16
|
|
|
private $compare; |
|
17
|
|
|
|
|
18
|
|
|
private $client; |
|
19
|
|
|
|
|
20
|
|
|
private $logger; |
|
21
|
|
|
|
|
22
|
|
View Code Duplication |
public function __construct(ClientInterface $client, array $compare, LoggerInterface $logger = null) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->client = $client; |
|
25
|
|
|
$this->compare = $compare; |
|
26
|
|
|
|
|
27
|
|
|
if (empty($logger)) { |
|
28
|
|
|
$logger = new Logger; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$this->logger = $logger; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function isVulnerable($target) |
|
37
|
|
|
{ |
|
38
|
|
|
if ($this->isXssPossible($target)) { |
|
39
|
|
|
return $this->verify($target); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function isXssPossible($target) |
|
46
|
|
|
{ |
|
47
|
|
|
return (bool) preg_match("/\?|(.+?)\=/", (string) $target); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
View Code Duplication |
public function verify($target) |
|
51
|
|
|
{ |
|
52
|
|
|
|
|
53
|
|
|
$urls = $this->generateUrls($target); |
|
54
|
|
|
|
|
55
|
|
|
$this->logger->info("\n"); |
|
56
|
|
|
|
|
57
|
|
|
foreach ($urls as $url) { |
|
58
|
|
|
if ($this->attack($url)) { |
|
59
|
|
|
$this->logger->info('Is Vull'); |
|
60
|
|
|
|
|
61
|
|
|
return true; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
View Code Duplication |
public function attack($url) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->logger->info('.'); |
|
71
|
|
|
|
|
72
|
|
|
try { |
|
73
|
|
|
$body = $this->client->get($url)->getBody()->getContents(); |
|
74
|
|
|
if ($body && $this->checkSuccess($body) && $this->checkCompare($body)) { |
|
75
|
|
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
} catch (\Exception $e) { |
|
78
|
|
|
$this->logger->error('#'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function checkSuccess($body) |
|
85
|
|
|
{ |
|
86
|
|
|
return (bool) preg_match('/'.static::EXPLOIT1REGEX.'|'.static::EXPLOIT2REGEX.'/', $body); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
View Code Duplication |
public function generateUrls($target) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->logger->info("\n".$target); |
|
92
|
|
|
$urls1 = $this->generateUrlsByExploit($target, static::EXPLOIT1); |
|
93
|
|
|
$urls2 = $this->generateUrlsByExploit($target, static::EXPLOIT2); |
|
94
|
|
|
return array_merge($urls1, $urls2); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function generateUrlsByExploit($target, $exploit) |
|
98
|
|
|
{ |
|
99
|
|
|
$explodeUrl = parse_url($target); |
|
100
|
|
|
$explodeQuery = explode('&', $explodeUrl['query']); |
|
101
|
|
|
|
|
102
|
|
|
if (!isset($explodeUrl['query'])) { |
|
103
|
|
|
return []; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$wordsValue = []; |
|
107
|
|
|
|
|
108
|
|
View Code Duplication |
foreach ($explodeQuery as $query) { |
|
109
|
|
|
$explodeQueryEqual = explode('=', $query); |
|
110
|
|
|
$wordsValue[$explodeQueryEqual[0]] = ''; |
|
111
|
|
|
|
|
112
|
|
|
if (isset($explodeQueryEqual[1])) { |
|
113
|
|
|
$wordsValue[$explodeQueryEqual[0]] = $explodeQueryEqual[1]; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
View Code Duplication |
foreach ($wordsValue as $keyValue => $value) { |
|
118
|
|
|
$urls[] = str_replace($keyValue.'='.$value, $keyValue.'='.$exploit, $target); |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $urls; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function checkCompare($body) |
|
125
|
|
|
{ |
|
126
|
|
|
foreach ($this->compare as $compare) { |
|
127
|
|
|
|
|
128
|
|
|
$isValid = strpos($body, $compare); |
|
129
|
|
|
if ($isValid !== false) { |
|
130
|
|
|
return true; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return false; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.