Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
199 lines (172 sloc) 7.07 KB
/*
Author:
Lance Mueller
Website: www.forensickb.com
Twitter: @lancemueller
Usage:
This EnScript will generate hash values for all tagged files and send the hash value to VirusTotal for scoring.
No file content is ever sent.
Any files with a VT score greater than zero are bookmarked.
This EnScript will work with a public or private VT API key. For public API keys, VT limits the number of
queries to four per minute. This EnScript will automatically sleep for one minute once that limit is reached and then continue processing.
License:
Copyright (c) 2017 Lance Mueller
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
class InputDialogClass: DialogClass {
StringEditClass TextAreaEdit;
InputDialogClass(DialogClass parent, String &apikey):
DialogClass(parent, "Check tagged files to VirusTotal"),
TextAreaEdit(this, "VirusTotal API Key (public or private):", SAME, NEXT, 300, 8, 0, apikey, 72, REQUIRED)
{
}
}
class MainClass {
NameValueClass list;
String address;
uint port, options;
String relativeUrl, api;
BookmarkClass folder;
MainClass ():
list(),
address = "www.virustotal.com",
port = SocketClass::HTTPSPORT,
options = WebClientClass::SSL | WebClientClass::VALIDATESERVERCERT;
relativeUrl = "/vtapi/v2/file/report"
{
}
void Main(CaseClass c) {
SystemClass::ClearConsole(1);
InputDialogClass diag(null, api);
StoreUpdate(0, api);
if (diag.Execute() == SystemClass::OK) {
StoreUpdate(StorageClass::WRITE, api);
DateClass start.Now();
Console.WriteLine("Starting: " + start.GetString());
folder = new BookmarkClass(c.BookmarkRoot(), "VirusTotal Tagged File Results", NodeClass::FOLDER);
WebClientClass client();
WebServiceClass::RequestClass request();
request.Command = WebServiceClass::RequestClass::GET;
ProcessHashValues(c);
Calculate(client, request, c);
DateClass end.Now();
Console.WriteLine("Finishing: " + end.GetString());
uint mins = (end.GetUnix() - start.GetUnix()) / 60;
Console.WriteLine("Total Mins: " + mins);
}
}
void Bookmark(CaseClass &c, String &desc, String path){
ItemIteratorClass iter (c, ItemIteratorClass::NORECURSE, ItemIteratorClass::ALL, NOPROXY);
EntryClass root = iter.GetNextEntry();
uint len = c.Name().GetLength();
path = path.SubString(len+1, -1);
len = path.Find("\\");
path = path.SubString(len+1, -1);
EntryClass entry = root.Find(path);
if (entry){
BookmarkItemClass bic(folder, entry.Name(), 0);
bic.CopyItemData(entry);
bic.SetComment(desc);
}
else {
Console.WriteLine("Could not find " + path);
}
}
void ProcessHashValues(CaseClass &c){
for (ItemIteratorClass iter (c, ItemIteratorClass::NORECURSE, ItemIteratorClass::ALL); EntryClass entry = iter.GetNextEntry();) {
if (entry.Tags().Contains("Check VT")){
if (entry.HashValue()) {
NameValueClass key(list, entry.HashValue());
key.SetValue(entry.TruePath());
}
}
}
}
void Calculate(WebClientClass &client, WebServiceClass::RequestClass &request, CaseClass &c){
uint total = list.Count();
Console.WriteLine("Sending: " + total + " hash values to VirusTotal");
String md5;
forall (NameValueClass l in list){
md5 = l.Name();
if (md5 != ""){
do {
int reply = RequestVT(md5, client, request, c);
} while (reply == 204);
}
else {
Console.WriteLine("Hash value empty!");
}
}
}
int RequestVT(String &md5, WebClientClass &client, WebServiceClass::RequestClass &request, CaseClass &c){
NameVariantClass root();
WebServiceClass::ReplyClass reply();
if (client.Open(address, port, options)) {
request.URL = relativeUrl + "?apikey=" + api + "&resource=" + md5;
if (!client.Command(request, reply)){
Console.WriteLine("Command could not be sent to the server: " + reply.Code);
}
else if (reply.Code == 200){
if (reply.File && reply.File.IsOpen()) {
String line;
reply.File.SetCodePage(CodePageClass::ANSI);
while (reply.File.More()) {
if(root.ReadJSON(reply.File)){
forall(NameVariantClass l in root){
if (l.Name() == "response_code")
if (l.Value){
NameVariantClass p = l.Parent().Find("positives");
Console.WriteLine("Hash: " + md5 + ", Score: " + uint::Convert(p.Value, int::DECIMAL));
if (uint::Convert(p.Value, int::DECIMAL) > 0){
NameVariantClass h = l.Parent().Find("resource");
NameVariantClass scans = l.Parent().Find("scans");
String malwarenames;
forall (NameVariantClass s in scans){
if (s.Name() == "result"){
if (s.Value) {
malwarenames+= s.Parent().Name() + "_" + s.Value + "|";
}
}
}
malwarenames.Trim("|", TRIMEND);
NameValueClass val = list.Find(h.Value);
String desc = "Score: " + p.Value + " - " + malwarenames;
Bookmark (c, desc, val.Value());
}
}
else {
Console.WriteLine("Hash: " + md5 + ", Score: VT - Unknown Hash value");
}
}
}
}
}
}
else if (reply.Code == 204) {
SystemClass::Sleep(60000);
}
else {
Console.WriteLine("Could not complete request. Result Code: " + reply.Code);
}
}
return reply.Code;
}
void StoreUpdate(uint props, String &apikey){
StorageClass storage("Check tagged files to VT", props);
storage.Value("api", apikey);
}
}