220 lines
11 KiB
PHP
220 lines
11 KiB
PHP
<?php
|
|
// PHP-Fehler anzeigen (nur für Entwicklung, im Produktivsystem deaktivieren)
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
// Erhöhen Sie das Speicherlimit und die Ausführungszeit für potenziell große E-Mails
|
|
ini_set('memory_limit', '256M');
|
|
set_time_limit(120);
|
|
|
|
function parseEmailHeaders($emailContent) {
|
|
$headers = [];
|
|
$lines = explode("\n", $emailContent);
|
|
$inHeaders = true;
|
|
$currentHeader = '';
|
|
|
|
foreach ($lines as $line) {
|
|
$line = rtrim($line, "\r"); // Entferne CR am Ende
|
|
if (empty($line)) {
|
|
$inHeaders = false; // Leere Zeile bedeutet Ende der Header
|
|
continue;
|
|
}
|
|
|
|
if ($inHeaders) {
|
|
// Wenn die Zeile mit Leerzeichen oder Tab beginnt, ist es eine Fortsetzung des vorherigen Headers
|
|
if (preg_match('/^\s/', $line)) {
|
|
if ($currentHeader !== '') {
|
|
$headers[$currentHeader] .= ' ' . trim($line);
|
|
}
|
|
} else {
|
|
// Neuer Header
|
|
if (preg_match('/^([^:]+):(.*)$/', $line, $matches)) {
|
|
$headerName = trim($matches[1]);
|
|
$headerValue = trim($matches[2]);
|
|
$headers[$headerName] = $headerValue;
|
|
$currentHeader = $headerName;
|
|
} else {
|
|
// Zeile sieht nicht nach einem Header aus, aber wir sind noch in den Headern
|
|
// Das könnte ein Problem mit der E-Mail-Formatierung sein.
|
|
// Wir versuchen, es dem letzten Header zuzuweisen oder ignorieren es.
|
|
if ($currentHeader !== '') {
|
|
$headers[$currentHeader] .= "\n" . $line; // Anfügen als neue Zeile im Wert
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
// Nach den Headern sind wir im Body, wir brauchen nur die Header
|
|
break;
|
|
}
|
|
}
|
|
return $headers;
|
|
}
|
|
|
|
function checkPhishing($headers, $emailContent) {
|
|
$warnings = [];
|
|
|
|
// 1. Absenderprüfung (From vs. Return-Path vs. Reply-To)
|
|
$from = isset($headers['From']) ? strtolower($headers['From']) : '';
|
|
$returnPath = isset($headers['Return-Path']) ? strtolower($headers['Return-Path']) : '';
|
|
$replyTo = isset($headers['Reply-To']) ? strtolower($headers['Reply-To']) : '';
|
|
|
|
// Extrahieren der Domain
|
|
preg_match('/@([^>]+)/', $from, $fromDomainMatch);
|
|
$fromDomain = isset($fromDomainMatch[1]) ? trim($fromDomainMatch[1]) : '';
|
|
|
|
preg_match('/@([^>]+)/', $returnPath, $returnPathDomainMatch);
|
|
$returnPathDomain = isset($returnPathDomainMatch[1]) ? trim($returnPathDomainMatch[1]) : '';
|
|
|
|
preg_match('/@([^>]+)/', $replyTo, $replyToDomainMatch);
|
|
$replyToDomain = isset($replyToDomainMatch[1]) ? trim($replyToDomainMatch[1]) : '';
|
|
|
|
|
|
if ($fromDomain && $returnPathDomain && $fromDomain !== $returnPathDomain) {
|
|
$warnings[] = "Achtung: Die Absenderdomain ('" . htmlspecialchars($fromDomain) . "') stimmt nicht mit der 'Return-Path'-Domain ('" . htmlspecialchars($returnPathDomain) . "') überein. Dies könnte ein Indikator für Spoofing sein.";
|
|
}
|
|
if ($fromDomain && $replyToDomain && $fromDomain !== $replyToDomain) {
|
|
$warnings[] = "Warnung: Die Absenderdomain ('" . htmlspecialchars($fromDomain) . "') stimmt nicht mit der 'Reply-To'-Domain ('" . htmlspecialchars($replyToDomain) . "') überein. Eine Diskrepanz kann auf Betrug hindeuten.";
|
|
}
|
|
|
|
// 2. Ungewöhnliche Zeichen oder Encoding im Betreff oder Absender
|
|
$subject = isset($headers['Subject']) ? $headers['Subject'] : '';
|
|
// Eine tiefergehende Prüfung würde hier das Decodieren des Betreffs erfordern
|
|
// und dann die Prüfung auf Homoglyphen oder ungewöhnliche Zeichen.
|
|
|
|
// 3. Häufige Phishing-Keywords im Betreff oder Body (sehr rudimentär)
|
|
$keywords = ['bestätigung', 'konto', 'passwort', 'sicherheit', 'aktualisieren', 'blockiert', 'dringend', 'zahlung', 'rechnung', 'gewinn', 'glückwunsch'];
|
|
foreach ($keywords as $keyword) {
|
|
if (stripos($subject, $keyword) !== false) {
|
|
$warnings[] = "Hinweis: Das Wort '" . htmlspecialchars($keyword) . "' wurde im Betreff gefunden. Dies ist oft in Phishing-Mails zu finden.";
|
|
break;
|
|
}
|
|
}
|
|
foreach ($keywords as $keyword) {
|
|
if (stripos($emailContent, $keyword) !== false) {
|
|
$warnings[] = "Hinweis: Das Wort '" . htmlspecialchars($keyword) . "' wurde im E-Mail-Text gefunden. Dies ist oft in Phishing-Mails zu finden.";
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
// 4. Links prüfen (verbessert: prüft auf offensichtliche URL-Diskrepanzen)
|
|
if (preg_match_all('/https?:\/\/[^\s"\']+/i', $emailContent, $matches)) {
|
|
foreach ($matches[0] as $url) {
|
|
$urlParts = parse_url($url);
|
|
$urlDomain = isset($urlParts['host']) ? strtolower($urlParts['host']) : '';
|
|
|
|
// Check for direct IP address in URL
|
|
if (filter_var($urlDomain, FILTER_VALIDATE_IP)) {
|
|
$warnings[] = "Warnung: Ein Link enthält eine direkte IP-Adresse ('" . htmlspecialchars($url) . "'). Dies ist oft verdächtig.";
|
|
}
|
|
|
|
// Check if the URL domain is significantly different from the From/Return-Path domain
|
|
if ($urlDomain && $fromDomain && !empty($fromDomain) && strpos($urlDomain, $fromDomain) === false && strpos($fromDomain, $urlDomain) === false) {
|
|
$warnings[] = "Warnung: Die Domain im Link ('" . htmlspecialchars($urlDomain) . "') weicht von der Absenderdomain ('" . htmlspecialchars($fromDomain) . "') ab. Vorsicht!";
|
|
}
|
|
// Add check for URL shorteners (simple regex)
|
|
$shortenerDomains = ['bit.ly', 'goo.gl', 'tinyurl.com', 'ow.ly', 't.co']; // Add more as needed
|
|
foreach ($shortenerDomains as $shortener) {
|
|
if (strpos($urlDomain, $shortener) !== false) {
|
|
$warnings[] = "Hinweis: Ein Link verwendet einen URL-Shortener ('" . htmlspecialchars($url) . "'). Dies kann zur Verschleierung bösartiger Ziele genutzt werden.";
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// 5. Fehlende oder seltsame Header
|
|
$importantHeaders = ['From', 'To', 'Subject', 'Date', 'Message-ID'];
|
|
foreach ($importantHeaders as $header) {
|
|
if (!isset($headers[$header])) {
|
|
$warnings[] = "Warnung: Der wichtige Header '" . htmlspecialchars($header) . "' fehlt.";
|
|
}
|
|
}
|
|
|
|
// 6. X-Mailer / User-Agent Prüfung
|
|
$xMailer = isset($headers['X-Mailer']) ? strtolower($headers['X-Mailer']) : '';
|
|
$userAgent = isset($headers['User-Agent']) ? strtolower($headers['User-Agent']) : '';
|
|
|
|
if (empty($xMailer) && empty($userAgent)) {
|
|
$warnings[] = "Hinweis: 'X-Mailer' oder 'User-Agent' Header fehlen. Dies ist manchmal bei automatisierten oder ungewöhnlichen Mail-Systemen der Fall.";
|
|
} elseif (
|
|
(strpos($xMailer, 'microsoft outlook') === false && strpos($xMailer, 'thunderbird') === false &&
|
|
strpos($xMailer, 'mail.app') === false && strpos($xMailer, 'gmail') === false && !empty($xMailer)) ||
|
|
(strpos($userAgent, 'microsoft outlook') === false && strpos($userAgent, 'thunderbird') === false &&
|
|
strpos($userAgent, 'mail.app') === false && strpos($userAgent, 'gmail') === false && !empty($userAgent))
|
|
) {
|
|
$warnings[] = "Hinweis: Ungewöhnlicher oder unbekannter E-Mail-Client ('" . htmlspecialchars($xMailer . $userAgent) . "') im 'X-Mailer'/'User-Agent' Header erkannt. Kann ein Indikator für Massenversand oder Phishing sein.";
|
|
}
|
|
|
|
|
|
// 7. Prüfung von Authentication-Results Header (SPF, DKIM, DMARC) - NEU!
|
|
$authResults = isset($headers['Authentication-Results']) ? $headers['Authentication-Results'] : '';
|
|
if (!empty($authResults)) {
|
|
if (stripos($authResults, 'spf=fail') !== false || stripos($authResults, 'spf=softfail') !== false) {
|
|
$warnings[] = "Kritische Warnung: SPF-Authentifizierung fehlgeschlagen oder Softfail. Die Absenderdomain ist möglicherweise gefälscht.";
|
|
}
|
|
if (stripos($authResults, 'dkim=fail') !== false) {
|
|
$warnings[] = "Kritische Warnung: DKIM-Authentifizierung fehlgeschlagen. Die E-Mail wurde möglicherweise manipuliert oder ist gefälscht.";
|
|
}
|
|
if (stripos($authResults, 'dmarc=fail') !== false || stripos($authResults, 'dmarc=quarantine') !== false || stripos($authResults, 'dmarc=reject') !== false) {
|
|
$warnings[] = "Kritische Warnung: DMARC-Authentifizierung fehlgeschlagen oder Aktion ausgelöst (Quarantine/Reject). Dies ist ein starkes Anzeichen für Phishing.";
|
|
}
|
|
} else {
|
|
$warnings[] = "Hinweis: 'Authentication-Results' Header fehlen. Dies kann auf eine fehlende oder nicht standardmäßige E-Mail-Authentifizierung hindeuten.";
|
|
}
|
|
|
|
|
|
return $warnings;
|
|
}
|
|
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (isset($_FILES['email_file']) && $_FILES['email_file']['error'] === UPLOAD_ERR_OK) {
|
|
$fileTmpPath = $_FILES['email_file']['tmp_name'];
|
|
$fileName = $_FILES['email_file']['name'];
|
|
$fileSize = $_FILES['email_file']['size'];
|
|
$fileType = $_FILES['email_file']['type'];
|
|
$fileNameCmps = explode(".", $fileName);
|
|
$fileExtension = strtolower(end($fileNameCmps));
|
|
|
|
$allowedfileExtensions = array('eml', 'txt');
|
|
|
|
if (in_array($fileExtension, $allowedfileExtensions)) {
|
|
$emailContent = file_get_contents($fileTmpPath);
|
|
|
|
if ($emailContent === false) {
|
|
header('Location: index.php?error=' . urlencode('Fehler beim Lesen der hochgeladenen Datei.'));
|
|
exit();
|
|
}
|
|
|
|
$headers = parseEmailHeaders($emailContent);
|
|
$phishingChecks = checkPhishing($headers, $emailContent);
|
|
|
|
// Eine Vorschau des E-Mail-Inhalts für die Debugging-Zwecke
|
|
$fullEmailPreview = substr($emailContent, 0, 2000); // Max. 2000 Zeichen
|
|
|
|
$result = [
|
|
'headers' => $headers,
|
|
'phishing_check' => $phishingChecks,
|
|
'full_email_preview' => $fullEmailPreview
|
|
];
|
|
|
|
// Ergebnisse als JSON enkodieren und base64 enkodieren, um sie in der URL zu übergeben
|
|
$encodedResult = base64_encode(json_encode($result));
|
|
header('Location: index.php?result=' . urlencode($encodedResult));
|
|
exit();
|
|
|
|
} else {
|
|
header('Location: index.php?error=' . urlencode('Ungültiger Dateityp. Bitte laden Sie eine .eml- oder .txt-Datei hoch.'));
|
|
exit();
|
|
}
|
|
} else {
|
|
header('Location: index.php?error=' . urlencode('Fehler beim Hochladen der Datei: ' . $_FILES['email_file']['error']));
|
|
exit();
|
|
}
|
|
} else {
|
|
header('Location: index.php');
|
|
exit();
|
|
} |