Skip to content

Commit c50aa4c

Browse files
committed
sql.php: PDO support
1 parent 17455af commit c50aa4c

File tree

2 files changed

+56
-18
lines changed

2 files changed

+56
-18
lines changed

rev_shell.py

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def print_message(self, data):
187187
sys.stdout.flush()
188188

189189
def interactive(self):
190+
print("[ ] Switching to interactive mode")
190191
self.on_message.append(lambda x: self.print_message(x))
191192
while self.running and self.connection is not None:
192193
self.sendline(input())

sql.php

+55-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
<?php
22

33
error_reporting(E_ALL);
4+
if (function_exists("mysqli_connect")) {
5+
$db_driver = "mysqli";
6+
} else if (class_exists("PDO")) {
7+
$db_driver = "PDO";
8+
} else {
9+
die("Neither mysqli nor PDO could be found. Exiting.");
10+
}
411

512
if (php_sapi_name() === "cli") {
613
$username = $argv[1];
@@ -18,37 +25,67 @@
1825
$dump_all = isset($_REQUEST["dumpAll"]);
1926
}
2027

21-
$link = mysqli_connect($host, $username, $password, $database);
22-
if (!$link) {
23-
die("Error connecting to mysql: " . mysqli_connect_error() . " (" . mysqli_connect_errno() . ")");
28+
if ($db_driver === "mysqli") {
29+
$link = mysqli_connect($host, $username, $password, $database);
30+
if (!$link) {
31+
die("Error connecting to mysql: " . mysqli_connect_error() . " (" . mysqli_connect_errno() . ")");
32+
}
33+
} else if ($db_driver === "PDO") {
34+
$databaseStr = $database ? ";dbname=$database" : "";
35+
$link = new PDO("mysql:host=$host$databaseStr", $username, $password);
36+
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
2437
}
2538

2639
if ($dump_all) {
27-
$res = mysqli_query($link, "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA='$database'");
28-
$tables = array();
29-
while ($row = $res->fetch_assoc()) {
30-
$tables[] = $row["TABLE_NAME"];
31-
}
40+
$tables = array();
41+
42+
if ($db_driver === "mysqli") {
43+
$res = mysqli_query($link, "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA='$database'");
44+
while ($row = $res->fetch_assoc()) {
45+
$tables[] = $row["TABLE_NAME"];
46+
}
47+
} else if ($db_driver === "PDO") {
48+
$stmt = $link->query("SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA='$database'");
49+
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
50+
$tables[] = $row["TABLE_NAME"];
51+
}
52+
}
3253

3354
foreach ($tables as $tableName) {
3455
echo "-- DATA FOR TABLE: tableName\n";
35-
$res = mysqli_query($link, "SELECT * FROM $tableName");
36-
while ($row = $res->fetch_assoc()) {
37-
var_dump($row);
56+
if ($db_driver === "mysqli") {
57+
$res = mysqli_query($link, "SELECT * FROM $tableName");
58+
while ($row = $res->fetch_assoc()) {
59+
print_r($row);
60+
}
61+
} else if ($db_driver === "PDO") {
62+
$stmt = $link->query("SELECT * FROM $tableName");
63+
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
64+
print_r($row);
65+
}
3866
}
3967
echo "-- --------------------------\n\n";
4068
}
4169

4270
} else {
43-
$res = mysqli_query($link, $query);
44-
if (!$res) {
45-
die("Error executing query: " . mysqli_error($link));
71+
if ($db_driver === "mysqli") {
72+
$res = mysqli_query($link, $query);
73+
if (!$res) {
74+
die("Error executing query: " . mysqli_error($link));
75+
}
76+
77+
while ($row = $res->fetch_assoc()) {
78+
print_r($row);
79+
}
80+
} else if ($db_driver === "PDO") {
81+
$stmt = $link->query($query);
82+
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
83+
print_r($row);
84+
}
4685
}
4786
}
4887

49-
while ($row = $res->fetch_assoc()) {
50-
var_dump($row);
88+
if ($db_driver === "mysqli") {
89+
mysqli_close($link);
5190
}
52-
53-
mysqli_close($link);
5491
?>

0 commit comments

Comments
 (0)