Skip to content

Commit

Permalink
Bugfix : invalid tables names.
Browse files Browse the repository at this point in the history
  • Loading branch information
csavelief committed Sep 16, 2024
1 parent deabe0c commit 01caa19
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ public function infosFromAsset(string $assetBase64): array

// The asset cannot be identified: check if it is an IP address from a known range
if (IsValidIpAddress::test($domainOrIpOrRange)) {
$asset = Asset::select('assets.*')
->join('scans', 'scans.ports_scan_id', '=', 'assets.cur_scan_id')
->join('ports', 'ports.scan_id', '=', 'scans.id')
->where('ports.ip', $domainOrIpOrRange)
$asset = Asset::select('am_assets.*')
->join('am_scans', 'am_scans.ports_scan_id', '=', 'am_assets.cur_scan_id')
->join('am_ports', 'am_ports.scan_id', '=', 'am_scans.id')
->where('am_ports.ip', $domainOrIpOrRange)
->first();
if ($asset) {
return $this->infosFromAsset(base64_encode($asset->asset));
Expand Down
52 changes: 26 additions & 26 deletions app/Modules/AdversaryMeter/Http/Controllers/HoneypotController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public function __construct()
public function attackerIndex(Request $request): array
{
$totalNumberOfEvents = HoneypotEvent::count();
return Attacker::select('attackers.*')
->orderBy('attackers.name')
->orderBy('attackers.last_contact')
return Attacker::select('am_attackers.*')
->orderBy('am_attackers.name')
->orderBy('am_attackers.last_contact')
->get()
->map(function (Attacker $attacker) use ($totalNumberOfEvents) {
return [
Expand All @@ -60,12 +60,12 @@ public function recentEvents(Request $request): array
/** @var array $ips */
$ips = config('towerify.adversarymeter.ip_addresses');
$events = HoneypotEvent::select(
'honeypots_events.*',
DB::raw("CASE WHEN attackers.name IS NULL THEN '-' ELSE attackers.name END AS internal_name"),
DB::raw("CASE WHEN attackers.id IS NULL THEN '-' ELSE attackers.id END AS attacker_id"),
'am_honeypots_events.*',
DB::raw("CASE WHEN am_attackers.name IS NULL THEN '-' ELSE am_attackers.name END AS internal_name"),
DB::raw("CASE WHEN am_attackers.id IS NULL THEN '-' ELSE am_attackers.id END AS attacker_id"),
)
->whereNotIn('ip', $ips)
->leftJoin('attackers', 'attackers.id', '=', 'honeypots_events.attacker_id');
->leftJoin('am_attackers', 'am_attackers.id', '=', 'am_honeypots_events.attacker_id');

if ($auto && !$manual) {
$events->where('human', true);
Expand All @@ -83,26 +83,26 @@ public function blacklistIps(?int $attackerId = null)
/** @var array $ips */
$ips = config('towerify.adversarymeter.ip_addresses');
$events = HoneypotEvent::select(
'honeypots_events.ip',
DB::raw('MIN(honeypots_events.timestamp) AS first_contact'),
DB::raw('MAX(honeypots_events.timestamp) AS last_contact'),
DB::raw("MAX(honeypots_events.hosting_service_description) AS isp_name"),
DB::raw("MAX(honeypots_events.hosting_service_country_code) AS country_code"),
'am_honeypots_events.ip',
DB::raw('MIN(am_honeypots_events.timestamp) AS first_contact'),
DB::raw('MAX(am_honeypots_events.timestamp) AS last_contact'),
DB::raw("MAX(am_honeypots_events.hosting_service_description) AS isp_name"),
DB::raw("MAX(am_honeypots_events.hosting_service_country_code) AS country_code"),
)
->whereNotIn('honeypots_events.ip', $ips)
->join('attackers', 'attackers.id', '=', 'honeypots_events.attacker_id');
->whereNotIn('am_honeypots_events.ip', $ips)
->join('am_attackers', 'am_attackers.id', '=', 'am_honeypots_events.attacker_id');
if ($attackerId) {
$events->where('honeypots_events.attacker_id', $attackerId);
$events->where('am_honeypots_events.attacker_id', $attackerId);
}
return $events->groupBy('ip')->distinct()->get()->toArray();
}

public function getVulnerabilitiesWithAssetInfo(?int $attackerId = null): array
{
return Alert::select('alerts.*', 'assets.id AS asset_id')
->join('ports', 'ports.id', '=', 'alerts.port_id')
->join('scans', 'scans.id', '=', 'ports.scan_id')
->join('assets', 'assets.cur_scan_id', '=', 'scans.ports_scan_id')
return Alert::select('am_alerts.*', 'am_assets.id AS asset_id')
->join('am_ports', 'am_ports.id', '=', 'am_alerts.port_id')
->join('am_scans', 'am_scans.id', '=', 'am_ports.scan_id')
->join('am_assets', 'am_assets.cur_scan_id', '=', 'am_scans.ports_scan_id')
->get()
->filter(fn(Alert $alert) => !$attackerId || ($alert->cve_id && $alert->events($attackerId)->exists()))
->map(function (Alert $alert) use ($attackerId) {
Expand All @@ -118,11 +118,11 @@ public function getVulnerabilitiesWithAssetInfo(?int $attackerId = null): array

public function getVulnerabilitiesWithAssetInfo2(string $assetBase64): array
{
return Alert::select('alerts.*', 'assets.id AS asset_id')
->join('ports', 'ports.id', '=', 'alerts.port_id')
->join('scans', 'scans.id', '=', 'ports.scan_id')
->join('assets', 'assets.cur_scan_id', '=', 'ports.ports_scan_id')
->where('assets.asset', base64_decode($assetBase64))
return Alert::select('am_alerts.*', 'am_assets.id AS asset_id')
->join('am_ports', 'am_ports.id', '=', 'am_alerts.port_id')
->join('am_scans', 'am_scans.id', '=', 'am_ports.scan_id')
->join('am_assets', 'am_assets.cur_scan_id', '=', 'am_ports.ports_scan_id')
->where('am_assets.asset', base64_decode($assetBase64))
->get()
->map(function (Alert $alert) {
return [
Expand Down Expand Up @@ -230,8 +230,8 @@ public function getHoneypotEventStats(string $dns): array
DB::raw("SUM(CASE WHEN human = 1 OR targeted = 1 THEN 1 ELSE 0 END) AS human_or_targeted"),
DB::raw("SUM(CASE WHEN human = 0 AND targeted = 0 THEN 1 ELSE 0 END) AS not_human_or_targeted")
)
->join('honeypots', 'honeypots.id', '=', 'honeypots_events.honeypot_id')
->where('honeypots.dns', $dns)
->join('am_honeypots', 'am_honeypots.id', '=', 'am_honeypots_events.honeypot_id')
->where('am_honeypots.dns', $dns)
->groupBy('date')
->orderBy('date', 'desc')
->limit(30)
Expand Down
6 changes: 3 additions & 3 deletions app/Modules/AdversaryMeter/Models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ public function alerts(): Builder
->get()
->map(fn(HiddenAlert $marker) => addslashes($marker->title));

$ifUids = $hiddenUids->isEmpty() ? 'false' : "alerts.uid IN ('{$hiddenUids->join("','")}')";
$ifTypes = $hiddenTypes->isEmpty() ? 'false' : "alerts.type IN ('{$hiddenTypes->join("','")}')";
$ifTitles = $hiddenTitles->isEmpty() ? 'false' : "alerts.title IN ('{$hiddenTitles->join("','")}')";
$ifUids = $hiddenUids->isEmpty() ? 'false' : "am_alerts.uid IN ('{$hiddenUids->join("','")}')";
$ifTypes = $hiddenTypes->isEmpty() ? 'false' : "am_alerts.type IN ('{$hiddenTypes->join("','")}')";
$ifTitles = $hiddenTitles->isEmpty() ? 'false' : "am_alerts.title IN ('{$hiddenTitles->join("','")}')";
$case = "CASE WHEN {$ifUids} OR {$ifTypes} OR {$ifTitles} THEN true ELSE false END AS is_hidden";

return Alert::select('am_alerts.*', DB::raw($case))
Expand Down

0 comments on commit 01caa19

Please sign in to comment.