|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace VaasExamples; |
| 4 | + |
| 5 | +use Aws\Credentials\Credentials; |
| 6 | +use Aws\Signature\SignatureV4; |
| 7 | +use Dotenv\Dotenv; |
| 8 | +use Exception; |
| 9 | +use GuzzleHttp\Client; |
| 10 | +use GuzzleHttp\Exception\GuzzleException; |
| 11 | +use GuzzleHttp\Psr7\Request; |
| 12 | +use SimpleXMLElement; |
| 13 | +use VaasSdk\Authentication\ClientCredentialsGrantAuthenticator; |
| 14 | +use VaasSdk\Exceptions\VaasClientException; |
| 15 | +use VaasSdk\Options\VaasOptions; |
| 16 | +use VaasSdk\Vaas; |
| 17 | + |
| 18 | +include_once("./vendor/autoload.php"); |
| 19 | + |
| 20 | +$dotenv = Dotenv::createImmutable(__DIR__); |
| 21 | +$dotenv->load(); |
| 22 | + |
| 23 | +$CLIENT_ID = getenv("CLIENT_ID"); |
| 24 | +$CLIENT_SECRET = getenv("CLIENT_SECRET"); |
| 25 | +$VAAS_URL = getenv("VAAS_URL"); |
| 26 | +$TOKEN_URL = getenv("TOKEN_URL"); |
| 27 | +$S3_ACCESS_KEY = getenv("S3_ACCESS_KEY"); |
| 28 | +$S3_SECRET_KEY = getenv("S3_SECRET_KEY"); |
| 29 | +$S3_URL = getenv("S3_URL"); |
| 30 | +$S3_BUCKET = getenv("S3_BUCKET"); |
| 31 | +$S3_REGION = getenv("S3_REGION"); |
| 32 | + |
| 33 | +// Build VaaS |
| 34 | +$authenticator = new ClientCredentialsGrantAuthenticator( |
| 35 | + clientId: $CLIENT_ID, |
| 36 | + clientSecret: $CLIENT_SECRET, |
| 37 | + tokenUrl: $TOKEN_URL |
| 38 | +); |
| 39 | +$vaasOptions = new VaasOptions( |
| 40 | + useHashLookup: true, |
| 41 | + useCache: false, |
| 42 | + vaasUrl: $VAAS_URL, |
| 43 | + timeout: 300 |
| 44 | +); |
| 45 | +try { |
| 46 | + $vaas = Vaas::builder() |
| 47 | + ->withOptions($vaasOptions) |
| 48 | + ->withAuthenticator($authenticator) |
| 49 | + ->build(); |
| 50 | +} catch (VaasClientException $e) { |
| 51 | + fwrite(STDERR, "Error: " . $e->getMessage() . "\n"); |
| 52 | + exit(1); |
| 53 | +} |
| 54 | + |
| 55 | +// List S3 bucket |
| 56 | +$client = new Client(); |
| 57 | +$request = new Request("GET", "$S3_URL/$S3_BUCKET?list-type=2"); |
| 58 | +$credentials = new Credentials($S3_ACCESS_KEY, $S3_SECRET_KEY); |
| 59 | +$signer = new SignatureV4("s3", $S3_REGION); |
| 60 | +$signedRequest = $signer->signRequest($request, $credentials); |
| 61 | +$keys = []; |
| 62 | +try { |
| 63 | + $response = $client->send($signedRequest); |
| 64 | + $xml = new SimpleXMLElement($response->getBody()->getContents()); |
| 65 | + foreach ($xml->Contents as $content) { |
| 66 | + $keys[] = (string)$content->Key; |
| 67 | + } |
| 68 | +} catch (GuzzleException $e) { |
| 69 | + fwrite(STDERR, "Error: " . $e->getMessage() . "\n"); |
| 70 | + exit(1); |
| 71 | +} catch (Exception $e) { |
| 72 | + fwrite(STDERR, "Error: " . $e->getMessage() . "\n"); |
| 73 | + exit(1); |
| 74 | +} |
| 75 | + |
| 76 | +// Iterate over everything in S3 bucket and scan with VaaS |
| 77 | +$results = []; |
| 78 | +$progress = 0; |
| 79 | +$count = count($keys); |
| 80 | +$startTimeTotal = microtime(true); |
| 81 | +foreach ($keys as $key){ |
| 82 | + // Pretty print progress |
| 83 | + $progress++; |
| 84 | + $percentageDone = number_format($progress / $count * 100, 1) . "%"; |
| 85 | + echo chr(27).chr(91).'H'.chr(27).chr(91).'J'; |
| 86 | + echo "\nProgress: $percentageDone ["; |
| 87 | + $done = $progress / $count * 30; |
| 88 | + for ($i = 0; $i < 30; $i++) { |
| 89 | + echo $i < $done ? "=" : " "; |
| 90 | + } |
| 91 | + echo "]\n"; |
| 92 | + echo "Execution time: " . number_format(microtime(true) - $startTimeTotal, 3) . "s\n"; |
| 93 | + echo "Current key: $key\n\n"; |
| 94 | + |
| 95 | + // Download file from S3 to temp file |
| 96 | + $request = new Request("GET", "$S3_URL/$S3_BUCKET/$key"); |
| 97 | + $request->withHeader("Accept", "application/octet-stream"); |
| 98 | + $credentials = new Credentials($S3_ACCESS_KEY, $S3_SECRET_KEY); |
| 99 | + $signer = new SignatureV4("s3", $S3_REGION); |
| 100 | + $signedRequest = $signer->signRequest($request, $credentials); |
| 101 | + try { |
| 102 | + $response = $client->send($signedRequest); |
| 103 | + } catch (GuzzleException $e) { |
| 104 | + fwrite(STDERR, "Error: " . $e->getMessage() . "\n"); |
| 105 | + exit(1); |
| 106 | + } |
| 107 | + $sample = tempnam(sys_get_temp_dir(), "vaas-sample-"); |
| 108 | + $handle = fopen($sample, "w"); |
| 109 | + fwrite($handle, $response->getBody()); |
| 110 | + fclose($handle); |
| 111 | + |
| 112 | + // Scan file with VaaS and track time |
| 113 | + $startTime = microtime(true); |
| 114 | + $vaasVerdict = $vaas->forFileAsync($sample)->await(); |
| 115 | + $endTime = microtime(true); |
| 116 | + $executionTime = ($endTime - $startTime) * 1000; |
| 117 | + |
| 118 | + // Save VaaS verdict and execution time |
| 119 | + $results[] = [ |
| 120 | + "key" => $key, |
| 121 | + "executionTimeInMs" => number_format($executionTime, 3), |
| 122 | + "verdict" => [ |
| 123 | + "sha256" => $vaasVerdict->sha256, |
| 124 | + "verdict" => $vaasVerdict->verdict->value, |
| 125 | + "detection" => $vaasVerdict->detection, |
| 126 | + "fileType" => $vaasVerdict->fileType, |
| 127 | + "mimeType" => $vaasVerdict->mimeType |
| 128 | + ] |
| 129 | + ]; |
| 130 | + |
| 131 | + // Delete temp file |
| 132 | + unlink($sample); |
| 133 | +} |
| 134 | + |
| 135 | +$endTimeTotal = microtime(true); |
| 136 | +$executionTime = number_format($endTimeTotal - $startTimeTotal, 3); |
| 137 | + |
| 138 | +file_put_contents("results-$S3_BUCKET.json", json_encode($results, JSON_PRETTY_PRINT)); |
| 139 | + |
| 140 | +echo "Results written to results.json\n"; |
| 141 | +echo "Total execution time: " . $executionTime . "s\n"; |
0 commit comments