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 SqlInjection implements VulnerabilityScanner |
10
|
|
|
{ |
11
|
|
|
const EXPLOIT = "'"; |
12
|
|
|
|
13
|
|
|
private $errors; |
14
|
|
|
|
15
|
|
|
private $client; |
16
|
|
|
|
17
|
|
|
private $logger; |
18
|
|
|
|
19
|
|
View Code Duplication |
public function __construct(ClientInterface $client, array $errors, LoggerInterface $logger = null) |
20
|
|
|
{ |
21
|
|
|
$this->client = $client; |
22
|
|
|
$this->errors = $errors; |
23
|
|
|
|
24
|
|
|
if (empty($logger)) { |
25
|
|
|
$logger = new Logger; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$this->logger = $logger; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function isVulnerable($target) |
32
|
|
|
{ |
33
|
|
|
if ($this->isSqlInjectionPossible($target)) { |
34
|
|
|
return $this->verify($target); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function isSqlInjectionPossible($target) |
41
|
|
|
{ |
42
|
|
|
return isset(parse_url($target)['query']); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
protected function verify($target) |
46
|
|
|
{ |
47
|
|
|
$urls = $this->generateUrlByExploit($target); |
48
|
|
|
|
49
|
|
|
foreach ($urls as $url) { |
50
|
|
|
$this->logger->info("\n url =>".$url."\n"); |
51
|
|
|
|
52
|
|
|
if ($this->attack($url)) { |
53
|
|
|
$this->logger->info('Is Vull'); |
54
|
|
|
|
55
|
|
|
return true; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function generateUrlByExploit($target) |
63
|
|
|
{ |
64
|
|
|
$explodeUrl = parse_url($target); |
65
|
|
|
$explodeQuery = explode('&', $explodeUrl['query']); |
66
|
|
|
|
67
|
|
View Code Duplication |
foreach ($explodeQuery as $keyQuery => $query) { |
|
|
|
|
68
|
|
|
$explodeQueryEqual = explode('=', $query); |
69
|
|
|
$wordsValue[$explodeQueryEqual[0]] = $explodeQueryEqual[1]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
View Code Duplication |
foreach ($wordsValue as $keyValue => $value) { |
|
|
|
|
73
|
|
|
$urls[] = str_replace($keyValue.'='.$value, $keyValue.'='.$value.static::EXPLOIT, $target); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $urls; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function attack($url) |
80
|
|
|
{ |
81
|
|
|
try { |
82
|
|
|
$body = $this->client->get($url)->getBody()->getContents(); |
83
|
|
|
|
84
|
|
|
if ($body) { |
85
|
|
|
if ($this->checkError($body)) { |
86
|
|
|
return $url; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} catch (\Exception $e) { |
90
|
|
|
if ($e->getCode() != '404' and $e->getCode() != '403') { |
91
|
|
|
return $url; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->logger->error('Error code => '.$e->getCode()."\n"); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function checkError($body) |
101
|
|
|
{ |
102
|
|
|
foreach ($this->errors as $error) { |
103
|
|
|
$isValid = strpos($body, $error); |
104
|
|
|
|
105
|
|
|
if ($isValid !== false) { |
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.