-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVirusTotal.php
More file actions
231 lines (199 loc) · 7.27 KB
/
VirusTotal.php
File metadata and controls
231 lines (199 loc) · 7.27 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* VirusTotal API PHP
* API Documentation: https://www.virustotal.com/advanced.html#publicapi
*
* @author Thomas Stig Jacobsen
* @since 20-09-2011
* @date 22-09-2011
* @copyright Thomas Stig Jacobsen
* @version 1.0
* @license BSD http://www.opensource.org/licenses/bsd-license.php
*/
class VirusTotal {
const OBJECT = 'object';
const JSON = 'json';
const FILE = 'file';
const URL = 'url';
const API_URL = 'https://www.virustotal.com/api/';
const VERSION = '0.1';
/**
* The API-key
*
* @var string
*/
private $_apiKey;
/**
* The default return format
*
* @var VirusTotal::OBJECT or VirusTotal::JSON
*/
private $_format;
/**
* The available return formats
*
* @var array
*/
private $_formatsArray = array(VirusTotal::OBJECT, VirusTotal::JSON);
/**
* The available types
*
* @var array
*/
private $_typesArray = array(VirusTotal::FILE, VirusTotal::URL);
/**
* Default constructor
*
* @param string $apiKey API-key from VirusTotal
* @param const[optional] $defaultFormat Default return format
*/
public function __construct($apiKey, $defaultFormat = VirusTotal::OBJECT) {
$this->setApiKey($apiKey);
$this->setFormat($defaultFormat);
}
/**
* Send and scan a file
*
* @param string $filePath Relative path to the file
* @param const[optional] $format Return format for this function
*
* @return VirusTotal::OBJECT or VirusTotal::JSON
*/
public function scanFile($filePath, $format = null) {
if ( ! file_exists($filePath)) {
return false;
}
$realPath = realpath($filePath);
$pathInfo = pathinfo($realPath);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$postData = array('file' => '@' . $realPath . ';type=' . finfo_file($finfo, $filePath) . ';filename=' . $pathInfo['basename']);
$response = $this->_makeCall('scan_file.json', $postData, $format);
return $response;
}
/**
* Retrieve a file scan report
*
* @param string $resource Hash of the file (MD5, SHA1 or SHA256)
* @param const[optional] $format Return format for this function
*
* @return VirusTotal::OBJECT or VirusTotal::JSON
*/
public function getScanReport($resource, $format = null) {
$postData = array('resource' => $resource);
$response = $this->_makeCall('get_file_report.json', $postData, $format);
return $response;
}
/**
* Retrieve a URL scan report
*
* @param string $resource URL of the site
* @param const[optional] $format Return format for this function
*
* @return VirusTotal::OBJECT or VirusTotal::JSON
*/
public function scanURL($url, $format = null) {
$postData = array('url' => $url);
$response = $this->_makeCall('scan_url.json', $postData, $format);
return $response;
}
/**
* Retrieve a URL scan report
*
* @param string $resource URL of the site or permalink identifier (md5-timestamp)
* @param bool $scan Scan the URL if not found in database (will return the scan_id)
* @param const[optional] $format Return format for this function
*
* @return VirusTotal::OBJECT or VirusTotal::JSON
*/
public function getURLReport($resource, $scan = false, $format = null) {
$postData = array('resource' => $resource, 'scan' => (int) $scan);
$response = $this->_makeCall('get_url_report.json', $postData, $format);
return $response;
}
/**
* Make comments on files and URLs
*
* @param string $resource Hash of the file (MD5, SHA1 or SHA256) or URL
* @param string $comment The comment to the file or URL
* @param array[optional] $tags List of standard file or URL tags (see API documentation for updates)
* The standard file tags are: goodware, malware, spamattachmentorlink, p2pdownload, impropagating, networkworm, drivebydownload.
* The standard URL tags are: malicious, benign, malwaredownload, phishingsite, browserexploit, spamlink.
* @param const[optional] $type Type of hash or URL: VirusTotal::FILE or VirusTotal::URL
* @param const[optional] $format Return format for this function
*
* @return VirusTotal::OBJECT or VirusTotal::JSON
*/
public function createComment($resource, $comment, array $tags = array(), $type = VirusTotal::FILE, $format = null) {
$typeName = (in_array($type, $this->_typesArray)) ? $type : VirusTotal::FILE;
$postData = array($typeName => $resource, 'comment' => $comment);
if (count($tags) > 0) {
$postData['tags'] = implode($tags, ';');
}
$response = $this->_makeCall('make_comment.json', $postData, $format);
return $response;
}
/**
* Setter for the API-key
*
* @param string $apiKey API-key
*/
public function setApiKey($apiKey) {
$this->_apiKey = (string) $apiKey;
}
/**
* Getter for the API-key
*
* @return string API-key
*/
public function getApiKey() {
return $this->_apiKey;
}
/**
* Setter for the default return format
*
* @param const $format
*/
public function setFormat($format) {
if(in_array($format, $this->_formatsArray)) {
$this->_format = $format;
} else {
$this->_format = VirusTotal::OBJECT;
}
}
/**
* Getter for the default return format
*
* @return const
*/
public function getFormat() {
return $this->_format;
}
/**
* Makes the call to the API
*
* @param string $function API specific function name for in the URL
* @param array $post The POST data to send to the API
* @param const $format Return format for this function
*
* @return VirusTotal::OBJECT or VirusTotal::JSON
*/
private function _makeCall($function, array $post = array(), $format) {
$format = ( ! empty($format) && in_array($format, $this->_formatsArray)) ? $format : $this->getFormat();
$postData = array('key' => $this->_apiKey);
$postData = array_merge($post, $postData);
$ch = curl_init(VirusTotal::API_URL . $function);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
curl_close($ch);
if ($format == VirusTotal::OBJECT) {
$response = json_decode($response);
}
return $response;
}
}
?>