feat: Netzwerk-Segmentverwaltung, Dashboard, globale Suche v0.6.0

This commit is contained in:
2026-07-01 18:46:49 +02:00
parent 402537805d
commit 9fa20af87a
17 changed files with 1006 additions and 60 deletions
+86 -37
View File
@@ -8,6 +8,7 @@ use App\Models\NetworkHost;
use App\Models\NetworkScan;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class NetworkScanImporter
{
@@ -19,47 +20,84 @@ class NetworkScanImporter
/**
* Importiert eine Angry IP Scanner .txt Exportdatei.
*/
public function importAngryIpScannerFile(string $filePath, int $createdBy): NetworkScan
public function importAngryIpScannerFile(string $filePath, int $createdBy, ?int $segmentId = null): NetworkScan
{
$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$scanner = '';
$subnet = '';
$headers = [];
$rows = [];
// Datei einlesen und UTF-8 BOM entfernen
$content = file_get_contents($filePath);
$content = preg_replace('/^\xEF\xBB\xBF/', '', $content); // UTF-8 BOM
$content = str_replace("\r\n", "\n", $content); // Windows CRLF → LF
$content = str_replace("\r", "\n", $content); // altes Mac CR → LF
$lines = array_filter(explode("\n", $content), fn($l) => trim($l) !== '');
$lines = array_values($lines);
$scanner = '';
$subnet = '';
$headers = [];
$rows = [];
$splitPattern = null; // wird beim ersten Header-Treffer gesetzt
foreach ($lines as $line) {
if (str_starts_with($line, 'Erstellt von') || str_starts_with($line, 'Created by')) {
$scanner = trim(str_replace(['Erstellt von ', 'Created by '], '', $line));
$trimmed = trim($line);
if (str_starts_with($trimmed, 'Erstellt von') || str_starts_with($trimmed, 'Created by')) {
$scanner = trim(str_replace(['Erstellt von ', 'Created by '], '', $trimmed));
continue;
}
if (str_starts_with($line, 'Gescannt') || str_starts_with($line, 'Scanned')) {
// "Gescannt 192.168.86.0 - 192.168.86.255"
preg_match('/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/', $line, $m);
if (str_starts_with($trimmed, 'Gescannt') || str_starts_with($trimmed, 'Scanned')) {
preg_match('/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/', $trimmed, $m);
$subnet = $m[1] ?? '0.0.0.0';
continue;
}
if (str_starts_with($line, 'http') || str_starts_with($line, 'https')) {
continue; // URL-Zeile überspringen
}
// Header-Zeile (beginnt mit "IP")
if (str_starts_with($line, 'IP') && empty($headers)) {
$headers = preg_split('/\t+/', $line);
$headers = array_map('trim', $headers);
if (str_starts_with($trimmed, 'http')) {
continue;
}
// Datenzeile
if (!empty($headers) && str_starts_with(trim($line), '') && preg_match('/^\d{1,3}\./', trim($line))) {
$cols = preg_split('/\t+/', $line);
$row = [];
// Header-Zeile erkennen (beginnt mit "IP")
if (str_starts_with($trimmed, 'IP') && empty($headers)) {
// Trennzeichen auto-detektieren
if (substr_count($trimmed, "\t") >= 2) {
$splitPattern = '/\t/';
} elseif (substr_count($trimmed, ';') >= 2) {
$splitPattern = '/;/';
} elseif (substr_count($trimmed, ',') >= 2) {
$splitPattern = '/,/';
} else {
// Angry IP Scanner: mehrere normale Leerzeichen als Trennzeichen
// Non-Breaking Spaces (0xC2 0xA0) bleiben innerhalb von Werten erhalten
$splitPattern = '/ {2,}/';
}
$headers = array_map('trim', preg_split($splitPattern, $trimmed));
continue;
}
// Datenzeile: muss mit IP-Adresse beginnen
if (!empty($headers) && $splitPattern !== null
&& preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $trimmed)) {
$cols = array_map(
fn($v) => trim(str_replace("\xc2\xa0", ' ', $v)), // Non-Breaking Spaces → normale Spaces
preg_split($splitPattern, $line)
);
$row = [];
foreach ($headers as $i => $h) {
$row[$h] = trim($cols[$i] ?? '');
$row[$h] = $cols[$i] ?? '';
}
$rows[] = $row;
}
}
return DB::transaction(function () use ($rows, $scanner, $subnet, $createdBy) {
// DEBUG: Log parse results
Log::info('NetworkScanImporter: scanner=' . $scanner . ', subnet=' . $subnet);
Log::info('NetworkScanImporter: separator hex=' . bin2hex($separator));
Log::info('NetworkScanImporter: headers=' . json_encode($headers));
Log::info('NetworkScanImporter: rows found=' . count($rows));
if (!empty($rows)) {
Log::info('NetworkScanImporter: first row=' . json_encode($rows[0]));
}
return DB::transaction(function () use ($rows, $scanner, $subnet, $createdBy, $segmentId) {
$this->scan = NetworkScan::create([
'segment_id' => $segmentId,
'subnet' => $subnet,
'source' => 'import',
'scanner' => $scanner,
@@ -83,23 +121,23 @@ class NetworkScanImporter
private function processRow(array $row): void
{
$ip = $this->extractColumn($row, ['IP']);
$ping = $this->extractColumn($row, ['Ping']);
$host = $this->extractColumn($row, ['Hostname']);
$mac = $this->normalizeMAC($this->extractColumn($row, ['MAC Addresse', 'MAC Address']));
$vendor = $this->extractColumn($row, ['MAC Hersteller', 'MAC Vendor']);
$ttl = (int) $this->extractColumn($row, ['TTL']) ?: null;
$ports = $this->extractColumn($row, ['Ports']);
$netbios= $this->extractColumn($row, ['NetBIOS Info']);
$http = $this->extractColumn($row, ['HTTP Sender']);
$web = $this->extractColumn($row, ['Web Erkennung', 'Web Detection']);
$ip = trim($this->extractColumn($row, ['IP', 'IP-Adresse', 'IP Address', 'IP Adresse']));
$ping = trim($this->extractColumn($row, ['Ping', 'Ping (ms)']));
$host = $this->cleanValue($this->extractColumn($row, ['Hostname', 'Host Name', 'DNS-Hostname']));
$mac = $this->normalizeMAC($this->extractColumn($row, ['MAC Adresse', 'MAC Addresse', 'MAC Address', 'MAC-Adresse', 'MAC']));
$vendor = $this->cleanValue($this->extractColumn($row, ['MAC Hersteller', 'MAC Vendor', 'Hersteller', 'Vendor']));
$ttl = (int) $this->cleanValue($this->extractColumn($row, ['TTL'])) ?: null;
$ports = $this->cleanValue($this->extractColumn($row, ['Ports', 'Gefilterte Ports', 'Offene Ports']));
$netbios= $this->cleanValue($this->extractColumn($row, ['NetBIOS Info', 'NetBIOS-Info', 'NetBIOS']));
$http = $this->cleanValue($this->extractColumn($row, ['HTTP Sender', 'HTTP']));
$web = $this->cleanValue($this->extractColumn($row, ['Web Erkennung', 'Web Detection', 'Webserver']));
$pingMs = null;
$status = 'offline';
if (!empty($ping) && $ping !== 'n/a' && $ping !== '-') {
preg_match('/(\d+)/', $ping, $m);
$pingMs = isset($m[1]) ? (int)$m[1] : null;
// Online-Erkennung: Ping enthält eine Zahl gefolgt von "ms"
if (preg_match('/(\d+)\s*ms/i', $ping, $m)) {
$pingMs = (int) $m[1];
$status = 'online';
$this->onlineHosts++;
}
@@ -210,6 +248,17 @@ class NetworkScanImporter
return '';
}
/**
* Bereinigt Angry IP Scanner Platzhalterwerte wie [n/a], [n/s], [n/d] zu leerem String.
*/
private function cleanValue(string $value): string
{
if (preg_match('/^\[n\/[asd]\]$/i', trim($value))) {
return '';
}
return trim($value);
}
private function normalizeMAC(string $mac): string
{
// Verschiedene MAC-Formate normalisieren zu XX:XX:XX:XX:XX:XX