|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Alessandro |
|
5
|
|
|
* Date: 02/12/2015 |
|
6
|
|
|
* Time: 13:47 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Padosoft\LaravelComposerSecurity; |
|
10
|
|
|
|
|
11
|
|
|
use Illuminate\Console\Command; |
|
12
|
|
|
use GuzzleHttp\Client; |
|
13
|
|
|
|
|
14
|
|
|
class SensiolabHelper |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
protected $guzzle; |
|
18
|
|
|
|
|
19
|
|
|
protected $command; |
|
20
|
|
|
|
|
21
|
|
|
protected $tableVulnerabilities = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* SensiolabHelper constructor. |
|
25
|
|
|
* @param Client $objguzzle |
|
26
|
|
|
* @param Command $objcommand |
|
27
|
|
|
*/ |
|
28
|
26 |
|
public function __construct(Client $objguzzle, Command $objcommand) |
|
29
|
|
|
{ |
|
30
|
26 |
|
$this->guzzle = $objguzzle; |
|
31
|
26 |
|
$this->command = $objcommand; |
|
32
|
26 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* |
|
36
|
|
|
* Send Request to sensiolab and return array of sensiolab vulnerabilities. |
|
37
|
|
|
* Empty array if here is no vulnerabilities. |
|
38
|
|
|
* |
|
39
|
|
|
* @param $fileLock path to composer.lock file. |
|
40
|
|
|
* |
|
41
|
|
|
* @return array |
|
42
|
|
|
*/ |
|
43
|
24 |
|
public function getSensiolabVulnerabilties($fileLock) |
|
44
|
|
|
{ |
|
45
|
24 |
|
$this->addVerboseLog('Send request to sensiolab: <info>' . $fileLock . '</info>'); |
|
46
|
|
|
|
|
47
|
24 |
|
$debug = false;//set to true to log into console output |
|
48
|
|
|
$headers = [ |
|
49
|
|
|
//OPTIONS |
|
50
|
|
|
'allow_redirects' => [ |
|
51
|
24 |
|
'max' => 3, // allow at most 10 redirects. |
|
52
|
24 |
|
'strict' => true, // use "strict" RFC compliant redirects. |
|
53
|
24 |
|
'referer' => true, // add a Referer header |
|
54
|
24 |
|
'protocols' => ['http', 'https'], // only allow http and https URLs |
|
55
|
|
|
'track_redirects' => false |
|
56
|
24 |
|
], |
|
57
|
24 |
|
'connect_timeout' => 20,//Use 0 to wait connection indefinitely |
|
58
|
24 |
|
'timeout' => 30, //Use 0 to wait response indefinitely |
|
59
|
24 |
|
'debug' => $debug, |
|
60
|
|
|
//HEADERS |
|
61
|
|
|
'headers' => [ |
|
62
|
|
|
'Accept' => 'application/json' |
|
63
|
24 |
|
], |
|
64
|
|
|
//UPLOAD FORM FILE |
|
65
|
|
|
'multipart' => [ |
|
66
|
|
|
[ |
|
67
|
24 |
|
'name' => 'lock', |
|
68
|
24 |
|
'contents' => fopen($fileLock, 'r') |
|
69
|
24 |
|
] |
|
70
|
24 |
|
] |
|
71
|
24 |
|
]; |
|
72
|
24 |
|
$response = null; |
|
73
|
|
|
|
|
74
|
|
|
try { |
|
75
|
24 |
|
$iResponse = $this->guzzle->request('POST', 'https://security.sensiolabs.org/check_lock', $headers); |
|
76
|
18 |
|
$responseBody = $iResponse->getBody()->getContents(); |
|
77
|
18 |
|
$response = json_decode($responseBody, true); |
|
78
|
24 |
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
|
79
|
4 |
|
$this->command->error("ClientException!\nMessage: " . $e->getMessage()); |
|
80
|
4 |
|
$colorTag = $this->getColorTagForStatusCode($e->getResponse()->getStatusCode()); |
|
81
|
4 |
|
$this->command->line("HTTP StatusCode: <{$colorTag}>" . $e->getResponse()->getStatusCode() . "<{$colorTag}>"); |
|
82
|
4 |
|
$resp = $e->getResponse(); |
|
83
|
4 |
|
if ($resp !== null) { |
|
84
|
4 |
|
$this->printMessage($resp); |
|
85
|
4 |
|
} |
|
86
|
4 |
|
$this->printMessage($e->getRequest()); |
|
87
|
6 |
|
} catch (\GuzzleHttp\Exception\RequestException $e) { |
|
88
|
4 |
|
$this->command->error("RequestException!\nMessage: " . $e->getMessage()); |
|
89
|
4 |
|
$this->printMessage($e->getRequest()); |
|
90
|
4 |
|
if ($e->hasResponse()) { |
|
91
|
2 |
|
$colorTag = $this->getColorTagForStatusCode($e->getResponse()->getStatusCode()); |
|
92
|
2 |
|
$this->command->line("HTTP StatusCode: <{$colorTag}>" . $e->getResponse()->getStatusCode() . "<{$colorTag}>"); |
|
93
|
2 |
|
$resp = $e->getResponse(); |
|
94
|
2 |
|
if ($resp !== null) { |
|
95
|
2 |
|
$this->printMessage($resp); |
|
96
|
2 |
|
} |
|
97
|
2 |
|
} |
|
98
|
|
|
} |
|
99
|
24 |
|
return $response; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param $name |
|
104
|
|
|
* @param $vulnerability |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
12 |
|
public function parseVulnerability($name, $vulnerability) |
|
108
|
|
|
{ |
|
109
|
|
|
$data = [ |
|
110
|
12 |
|
'name' => $name, |
|
111
|
12 |
|
'version' => $vulnerability['version'], |
|
112
|
12 |
|
'advisories' => array_values($vulnerability['advisories']) |
|
113
|
12 |
|
]; |
|
114
|
12 |
|
unset($this->tableVulnerabilities); |
|
115
|
12 |
|
foreach ($data['advisories'] as $key2 => $advisory) { |
|
116
|
|
|
$data2 = [ |
|
117
|
12 |
|
'title' => $advisory['title'], |
|
118
|
12 |
|
'link' => $advisory['link'], |
|
119
|
12 |
|
'cve' => $advisory['cve'] |
|
120
|
12 |
|
]; |
|
121
|
|
|
|
|
122
|
|
|
$dataTable = [ |
|
123
|
12 |
|
'name' => $data['name'], |
|
124
|
12 |
|
'version' => $data['version'], |
|
125
|
12 |
|
'advisories' => $data2["title"] |
|
126
|
12 |
|
]; |
|
127
|
|
|
|
|
128
|
12 |
|
$this->addVerboseLog($data['name'] . " " . $data['version'] . " " . $data2["title"], true); |
|
129
|
12 |
|
$this->tableVulnerabilities[] = $dataTable; |
|
130
|
12 |
|
} |
|
131
|
|
|
|
|
132
|
12 |
|
return $this->tableVulnerabilities; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param $key |
|
137
|
|
|
* @param $vulnerability |
|
138
|
|
|
* @param bool $tuttoOk |
|
139
|
|
|
* @return array |
|
140
|
|
|
*/ |
|
141
|
10 |
|
public function checkResponse($key, $vulnerability, $tuttoOk) |
|
142
|
|
|
{ |
|
143
|
10 |
|
$tableVulnerabilities = array(); |
|
144
|
|
|
|
|
145
|
10 |
|
foreach ($this->parseVulnerability($key, $vulnerability) as $vul) { |
|
146
|
10 |
|
$tableVulnerabilities[] = array_merge($vul, array('isOk' => $tuttoOk)); |
|
147
|
10 |
|
} |
|
148
|
|
|
|
|
149
|
10 |
|
return $tableVulnerabilities; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param string $msg |
|
154
|
|
|
* @param bool|false $error |
|
155
|
|
|
*/ |
|
156
|
26 |
|
private function addVerboseLog($msg, $error = false) |
|
157
|
|
|
{ |
|
158
|
26 |
|
$verbose = $this->command->option('verbose'); |
|
159
|
26 |
|
if ($verbose) { |
|
160
|
10 |
|
if ($error) { |
|
161
|
2 |
|
$this->command->error($msg); |
|
162
|
2 |
|
} else { |
|
163
|
8 |
|
$this->command->line($msg); |
|
164
|
|
|
} |
|
165
|
10 |
|
} |
|
166
|
26 |
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param \Psr\Http\Message\MessageInterface $message |
|
170
|
|
|
* |
|
171
|
|
|
* @throws \RuntimeException |
|
172
|
|
|
*/ |
|
173
|
6 |
|
private function printMessage(\Psr\Http\Message\MessageInterface $message) |
|
174
|
|
|
{ |
|
175
|
6 |
|
$type = ''; |
|
176
|
6 |
|
$body = ''; |
|
177
|
6 |
|
if (is_a($message, '\Psr\Http\Message\RequestInterface')) { |
|
178
|
6 |
|
$type = 'REQUEST'; |
|
179
|
6 |
|
$body = $message->getBody(); |
|
180
|
6 |
|
} elseif (is_a($message, '\Psr\Http\Message\ResponseInterface')) { |
|
181
|
6 |
|
$type = 'RESPONSE'; |
|
182
|
6 |
|
$body = $message->getBody()->getContents(); |
|
183
|
6 |
|
} |
|
184
|
6 |
|
$this->command->info("$type:"); |
|
185
|
6 |
|
$headers = ''; |
|
186
|
6 |
|
foreach ($message->getHeaders() as $name => $values) { |
|
187
|
|
|
$headers .= $name . ': ' . implode(', ', $values) . "\r\n"; |
|
188
|
6 |
|
} |
|
189
|
6 |
|
$this->command->comment($headers); |
|
190
|
6 |
|
if ($type == 'REQUEST') { |
|
191
|
6 |
|
$this->command->comment($body); |
|
192
|
6 |
|
} elseif ($type == 'RESPONSE') { |
|
193
|
6 |
|
$this->command->comment($body); |
|
194
|
6 |
|
} |
|
195
|
6 |
|
} |
|
196
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Get the color tag for the given status code. |
|
200
|
|
|
* |
|
201
|
|
|
* @param string $code |
|
202
|
|
|
* |
|
203
|
|
|
* @return string |
|
204
|
|
|
* |
|
205
|
|
|
* @see https://github.com/spatie/http-status-check/blob/master/src/CrawlLogger.php#L96 |
|
206
|
|
|
*/ |
|
207
|
6 |
|
protected function getColorTagForStatusCode($code) |
|
208
|
|
|
{ |
|
209
|
6 |
|
if (starts_with($code, '2')) { |
|
210
|
2 |
|
return 'info'; |
|
211
|
|
|
} |
|
212
|
4 |
|
if (starts_with($code, '3')) { |
|
213
|
|
|
return 'comment'; |
|
214
|
|
|
} |
|
215
|
4 |
|
return 'error'; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|