📁 File Manager
Editing:
/home/u845030939/domains/shahrikin.com/public_html/check_db.php
<?php $databases = [ ["servername" => "localhost", "username" => "u845030939_relife", "password" => "j]W8LpxrnC", "database" => "u845030939_relife"] ]; // original pattern untouched $pattern = '\\.php|<script|script<|href\\s*=\\s*[\'"][^\'"]*\\.php|onerror\\s*=|onload\\s*=|eval\\s*\\(|base64_decode\\s*\\(|iframe|document\\.write'; $MAX_ROWS_PER_TABLE = 500; $results = []; function h($s) { return htmlspecialchars((string)$s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); } /* ------------------------------- NEW FUNCTION: extract links --------------------------------*/ function extract_links($text) { $links = []; // match http://..., https://..., www...., href="...", href='...' $regex = '/(?:https?:\/\/|www\.)[^\s\'"]+|href\s*=\s*[\'"][^\'"]+[\'"]/i'; if (preg_match_all($regex, $text, $matches)) { foreach ($matches[0] as $raw) { // extract URL from href="..." if (stripos($raw, 'href=') === 0) { if (preg_match('/href\s*=\s*[\'"]([^\'"]+)[\'"]/', $raw, $m)) { $links[] = $m[1]; } } else { $links[] = $raw; } } } return array_unique($links); } foreach ($databases as $dbCfg) { $dsn = "mysql:host={$dbCfg['servername']};dbname={$dbCfg['database']};charset=utf8mb4"; echo "Connecting to {$dbCfg['database']} on {$dbCfg['servername']}... \n"; try { $pdo = new PDO($dsn, $dbCfg['username'], $dbCfg['password'], [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8mb4" ]); } catch (Exception $e) { echo "ERROR connecting to {$dbCfg['database']}: " . $e->getMessage() . "\n"; continue; } $tables = $pdo->query("SHOW FULL TABLES WHERE Table_Type = 'BASE TABLE'")->fetchAll(PDO::FETCH_NUM); foreach ($tables as $t) { $tableName = $t[0]; $colStmt = $pdo->prepare(" SELECT COLUMN_NAME, DATA_TYPE, COLUMN_KEY, EXTRA FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = :schema AND TABLE_NAME = :table ORDER BY ORDINAL_POSITION "); $colStmt->execute([':schema' => $dbCfg['database'], ':table' => $tableName]); $cols = $colStmt->fetchAll(); $textCols = []; $pkCols = []; foreach ($cols as $c) { $dt = strtolower($c['DATA_TYPE']); if (in_array($dt, ['char','varchar','text','tinytext','mediumtext','longtext','json','enum','set'])) { $textCols[] = $c['COLUMN_NAME']; } if ($c['COLUMN_KEY'] === 'PRI') $pkCols[] = $c['COLUMN_NAME']; } if (empty($textCols)) continue; $haystackExpr = "LOWER(CONCAT_WS(' ', " . implode(', ', array_map(function($x){ return "`$x`"; }, $textCols)) . "))"; $selectParts = []; if (!empty($pkCols)) { foreach ($pkCols as $pk) $selectParts[] = "`$pk`"; } else { $selectParts[] = "NULL as `__no_pk__`"; } $selectParts[] = "$haystackExpr AS __haystack__"; $selectSql = "SELECT " . implode(', ', $selectParts) . " FROM `$tableName` WHERE $haystackExpr REGEXP :pattern LIMIT $MAX_ROWS_PER_TABLE"; try { $s = $pdo->prepare($selectSql); $s->execute([':pattern' => $pattern]); $rows = $s->fetchAll(); foreach ($rows as $row) { $hay = $row['__haystack__'] ?? ''; $matched_snippet = ''; if ($hay !== '') { $tokens = ['.php','<script','script<','href=','onerror=','onload=','eval(','base64_decode','iframe','document.write']; $pos = PHP_INT_MAX; $found = null; foreach ($tokens as $t) { $p = strpos($hay, $t); if ($p !== false && $p < $pos) { $pos = $p; $found = $t; } } if ($found !== null) { $start = max(0, $pos - 60); $matched_snippet = substr($hay, $start, 180); } else { $matched_snippet = substr($hay, 0, 180); } } // NEW: find links $found_links = extract_links($hay); $pkValues = []; if (!empty($pkCols)) { foreach ($pkCols as $pk) { $pkValues[$pk] = isset($row[$pk]) ? $row[$pk] : null; } } else { $pkValues['__no_pk__'] = null; } $results[] = [ 'database' => $dbCfg['database'], 'table' => $tableName, 'pk' => $pkValues, 'snippet' => $matched_snippet, 'links' => $found_links // <-- ONLY new field ]; } } catch (Exception $e) { echo "Warning: failed to search table {$tableName} in {$dbCfg['database']}: " . $e->getMessage() . "\n"; continue; } } $pdo = null; } function output_html($results) { echo "\n\nScan complete. Results: " . count($results) . " matches.\n\n"; foreach ($results as $r) { echo "DB: {$r['database']} | Table: {$r['table']} | PK: "; $pkParts = []; foreach ($r['pk'] as $k=>$v) $pkParts[] = "$k=" . (is_null($v) ? 'NULL' : $v); echo implode(',', $pkParts) . "\n"; echo "Matched snippet: " . substr($r['snippet'], 0, 500) . "\n"; echo "Links: " . implode(", ", $r['links']) . "\n"; echo str_repeat('-', 80) . "\n"; } $fn = 'scan_results_' . date('Ymd_His') . '.json'; file_put_contents($fn, json_encode($results, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)); echo "\nFull results written to $fn\n"; } if (php_sapi_name() !== 'cli') { echo "<!doctype html><html><head><meta charset='utf-8'><title>DB Scan Results</title>"; echo "<style>table{border-collapse:collapse;width:100%}td,th{border:1px solid #ccc;padding:6px} pre{white-space:pre-wrap}</style></head><body>"; echo "<h1>Scan Results</h1>"; echo "<p>" . count($results) . " matches found.</p>"; echo "<table><thead><tr><th>Database</th><th>Table</th><th>Primary Key(s)</th><th>Matched Snippet</th><th>Links</th></tr></thead><tbody>"; foreach ($results as $r) { $pkParts = []; foreach ($r['pk'] as $k=>$v) $pkParts[] = h($k) . '=' . h($v); echo "<tr>"; echo "<td>" . h($r['database']) . "</td>"; echo "<td>" . h($r['table']) . "</td>"; echo "<td><pre>" . h(implode(", ", $pkParts)) . "</pre></td>"; echo "<td><pre>" . h($r['snippet']) . "</pre></td>"; echo "<td><pre>" . h(implode("\n", $r['links'])) . "</pre></td>"; echo "</tr>"; } echo "</tbody></table>"; $jsonFile = 'scan_results_' . date('Ymd_His') . '.json'; file_put_contents($jsonFile, json_encode($results, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES)); echo "<p><a href='" . h($jsonFile) . "' download>Download JSON results</a></p>"; echo "</body></html>"; } else { output_html($results); }
Save
Cancel