Skip to content

Commit

Permalink
Backport the Job responsible to deal with late scans.
Browse files Browse the repository at this point in the history
  • Loading branch information
csavelief committed Sep 8, 2024
1 parent c5c8020 commit 8a90672
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Jobs\CheckServersHealth;
use App\Jobs\PullServersInfos;
use App\Modules\AdversaryMeter\Jobs\ImportHoneypotsEvents;
use App\Modules\AdversaryMeter\Jobs\ProcessLateScans;
use App\Modules\AdversaryMeter\Jobs\TriggerDiscoveryShallow;
use App\Modules\AdversaryMeter\Jobs\TriggerScan;
use Illuminate\Console\Scheduling\Schedule;
Expand Down Expand Up @@ -35,6 +36,7 @@ protected function schedule(Schedule $schedule)
$schedule->job(new CheckServersHealth())->everyFifteenMinutes();
$schedule->job(new PullServersInfos())->hourly();
$schedule->job(new AgeOffOsqueryEvents())->hourly();
$schedule->job(new ProcessLateScans())->hourly();
$schedule->job(new TriggerDiscoveryShallow())->daily();
$schedule->command('telescope:prune --hours=48')->daily();
// $schedule->job(new TriggerDiscoveryDeep())->weekly();
Expand Down
50 changes: 50 additions & 0 deletions app/Modules/AdversaryMeter/Jobs/ProcessLateScans.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Modules\AdversaryMeter\Jobs;

use App\Modules\AdversaryMeter\Events\EndPortsScan;
use App\Modules\AdversaryMeter\Events\EndVulnsScan;
use App\Modules\AdversaryMeter\Models\Asset;
use App\Modules\AdversaryMeter\Models\Scan;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProcessLateScans implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public $tries = 1;
public $maxExceptions = 1;
public $timeout = 3 * 180; // 9mn

public function __construct()
{
//
}

public function handle()
{
$dropAfter = config('towerify.adversarymeter.drop_scan_events_after_x_minutes');
Asset::whereNotNull('next_scan_id')
->where('is_monitored', true)
->get()
->flatMap(fn(Asset $asset) => $asset->scanInProgress())
->each(function (Scan $scan) use ($dropAfter) {
if ($scan->portsScanIsRunning()) {
$droppedAt = $scan->ports_scan_begins_at->addMinutes($dropAfter);
if ($droppedAt < Carbon::now()) {
event(new EndPortsScan(Carbon::now(), $scan->asset()->first(), $scan));
}
} elseif ($scan->vulnsScanIsRunning()) {
$droppedAt = $scan->vulns_scan_begins_at->addMinutes($dropAfter);
if ($droppedAt < Carbon::now()) {
event(new EndVulnsScan(Carbon::now(), $scan));
}
}
});
}
}
11 changes: 8 additions & 3 deletions app/Modules/AdversaryMeter/Listeners/EndVulnsScanListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,14 @@ private function setScreenshot(Port $port, array $task)
->map(fn(array $data) => json_decode($data['rawOutput'], true))
->filter(fn(array $screenshot) => !empty($screenshot['png']))
->each(function (array $screenshot) use ($port) {
$screenshot = Screenshot::create(['png' => "data:image/png;base64,{$screenshot['png']}"]);
$port->screenshot_id = $screenshot->id;
$port->save();
try {
$screenshot = Screenshot::create(['png' => "data:image/png;base64,{$screenshot['png']}"]);
$port->screenshot_id = $screenshot->id;
$port->save();
} catch (\Exception $exception) {
Log::error($exception);
Log::error($port);
}
});
}

Expand Down
6 changes: 6 additions & 0 deletions app/Modules/AdversaryMeter/Models/Scan.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Scan extends Model
Expand All @@ -30,6 +31,11 @@ class Scan extends Model
'vulns_scan_ends_at' => 'datetime',
];

public function asset(): BelongsTo
{
return $this->belongsTo(Asset::class);
}

public function ports(): HasMany
{
return $this->hasMany(Port::class, 'scan_id');
Expand Down

0 comments on commit 8a90672

Please sign in to comment.