This repository was archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVulnHandlerPlugin.php
144 lines (105 loc) · 3.72 KB
/
VulnHandlerPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* Created by PhpStorm.
* User: TeppeiIsayama
* Date: 2016/05/16
* Time: 21:15
*/
namespace CPEBach\Composer;
use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use CPEBach\Entity\Cve;
use CPEBach\Entity\Package;
use CPEBach\Entity\PackageSet;
use CPEBach\VulnHandler\Reference;
/**
* Class VulnHandlerPlugin
* @package CPEBach\Composer
*
* @property Composer $composer
* @property IOInterface $io
*/
class VulnHandlerPlugin implements PluginInterface, EventSubscriberInterface
{
protected $composer;
protected $io;
/**
* Apply plugin modifications to Composer
*
* @param Composer $composer
* @param IOInterface $io
*/
public function activate(Composer $composer, IOInterface $io)
{
/** @var Composer composer */
$this->composer = $composer;
$this->io = $io;
}
public static function getSubscribedEvents()
{
return array(
'post-autoload-dump' => 'referPackages'
);
}
public function referPackages(Event $event)
{
$this->io->write(array(
'',
'<fg=black;bg=cyan>VulnHandlerPlugin powered by php-bach.org</>',
''
));
$localRepo = $event->getComposer()->getRepositoryManager()->getLocalRepository();
$packageSet = new PackageSet();
foreach ($localRepo->getPackages() as $package) {
$packageSet->add(new Package($package->getPrettyName(), $package->getPrettyVersion()));
}
//api url
//null is default(php-bach)
$apiUrl = null;
if ($event->getComposer()->getConfig()->has('vuln-handler-plugin.url')) {
$apiUrl = (string) $event->getComposer()->getConfig()->get('vuln-handler-plugin.url');
}
$reference = new Reference();
$reference->referencePackages($packageSet, $this->io, $apiUrl);
$this->io->write(
array(
'==============================',
"Results",
'=============================='
));
/** @var Package $package */
foreach ($packageSet as $package) {
if (count($package->getCves())) {
//CVEsがある
$this->io->writeError("<info>The package: \"{$package->getPackageName()}\" ({$package->getVersion()}) has following CVEs.</info>");
/** @var Cve $cve */
foreach ($package->getCves() as $cve) {
//値域ごとに色分け
$cvssMessage = ' ';
if ($score = $cve->getCvssV2Score()) {
if (((float) $score) >= 7.0) {
$cvssMessage .= '<fg=white;bg=red>' . $score . '(HIGH)</>';
} elseif (((float) $score) >= 4.0) {
$cvssMessage .= '<fg=white;bg=blue>' . $score . '(MEDIUM)</>';
} elseif (((float) $score) >= 0.0) {
$cvssMessage .= '<fg=white;bg=yellow>' . $score . '(LOW)</>';
}
}
//表示メッセージを生成
$displayMessages = array(
'',
$cve->getCveName() . $cvssMessage,
'Summary:', ' ' . $cve->getSummary()
);
if ($refUrl = $cve->getReferenceUrl()) {
$displayMessages[] = 'Reference: ' . $refUrl;
}
$this->io->write($displayMessages);
}
}
}
}
}