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
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: