当前位置:   article > 正文

PHP、MySQL 注入_mysql注入kali

mysql注入kali

Welcome to the NetSPI SQL Injection Wiki:https://sqlwiki.netspi.com/

因为需要了解下 SQL 注入,就使用 PHP 自己写了一个只有一个网页的网站测试下,现在记录下过程。。。

直接使用的 KALI系统 (KALI官网:Kali Linux | Penetration Testing and Ethical Hacking Linux Distribution)。KALI 是一个渗透测试的神器。集成了好多黑客工具,当然也就集成了许多开发所需的环境。

这里只涉及 MySQL 和 apache 

启动 MySQL :

root@kali:~# systemctl start mysql      //启动 mysql 服务
root@kali:~# systemctl status mysql     //查看 mysql 状态

SQL 建表脚本(添加一些测试数据):

MySQL样例数据库脚本:MySQL样例数据库脚本_数据库脚本-MySQL代码类资源-CSDN下载

  1. DROP SCHEMA IF EXISTS world;
  2. CREATE SCHEMA world;
  3. USE world;
  4. SET AUTOCOMMIT=0;
  5. --
  6. -- Table structure for table `City`
  7. --
  8. DROP TABLE IF EXISTS `City`;
  9. CREATE TABLE `City` (
  10. `ID` int(11) NOT NULL AUTO_INCREMENT,
  11. `Name` char(35) NOT NULL DEFAULT '',
  12. `CountryCode` char(3) NOT NULL DEFAULT '',
  13. `District` char(20) NOT NULL DEFAULT '',
  14. `Population` int(11) NOT NULL DEFAULT '0',
  15. PRIMARY KEY (`ID`),
  16. KEY `CountryCode` (`CountryCode`),
  17. CONSTRAINT `city_ibfk_1` FOREIGN KEY (`CountryCode`) REFERENCES `Country` (`Code`)
  18. ) ENGINE=InnoDB AUTO_INCREMENT=4080 DEFAULT CHARSET=latin1;
  19. --
  20. -- Dumping data for table `City`
  21. --
  22. -- ORDER BY: `ID`
  23. INSERT INTO `City` VALUES (1,'Kabul','AFG','Kabol',1780000);
  24. INSERT INTO `City` VALUES (2,'Qandahar','AFG','Qandahar',237500);
  25. INSERT INTO `City` VALUES (3,'Herat','AFG','Herat',186800);
  26. INSERT INTO `City` VALUES (4,'Mazar-e-Sharif','AFG','Balkh',127800);
  27. INSERT INTO `City` VALUES (5,'Amsterdam','NLD','Noord-Holland',731200);
  28. INSERT INTO `City` VALUES (6,'Rotterdam','NLD','Zuid-Holland',593321);
  29. INSERT INTO `City` VALUES (7,'Haag','NLD','Zuid-Holland',440900);
  30. INSERT INTO `City` VALUES (8,'Utrecht','NLD','Utrecht',234323);
  31. INSERT INTO `City` VALUES (9,'Eindhoven','NLD','Noord-Brabant',201843);
  32. INSERT INTO `City` VALUES (10,'Tilburg','NLD','Noord-Brabant',193238);
  33. COMMIT;
  34. --
  35. -- Table structure for table `Country`
  36. --
  37. DROP TABLE IF EXISTS `Country`;
  38. CREATE TABLE `Country` (
  39. `Code` char(3) NOT NULL DEFAULT '',
  40. `Name` char(52) NOT NULL DEFAULT '',
  41. `Continent` enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') NOT NULL DEFAULT 'Asia',
  42. `Region` char(26) NOT NULL DEFAULT '',
  43. `SurfaceArea` float(10,2) NOT NULL DEFAULT '0.00',
  44. `IndepYear` smallint(6) DEFAULT NULL,
  45. `Population` int(11) NOT NULL DEFAULT '0',
  46. `LifeExpectancy` float(3,1) DEFAULT NULL,
  47. `GNP` float(10,2) DEFAULT NULL,
  48. `GNPOld` float(10,2) DEFAULT NULL,
  49. `LocalName` char(45) NOT NULL DEFAULT '',
  50. `GovernmentForm` char(45) NOT NULL DEFAULT '',
  51. `HeadOfState` char(60) DEFAULT NULL,
  52. `Capital` int(11) DEFAULT NULL,
  53. `Code2` char(2) NOT NULL DEFAULT '',
  54. PRIMARY KEY (`Code`)
  55. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  56. --
  57. -- Dumping data for table `Country`
  58. --
  59. -- ORDER BY: `Code`
  60. INSERT INTO `Country` VALUES ('ABW','Aruba','North America','Caribbean',193.00,NULL,103000,78.4,828.00,793.00,'Aruba','Nonmetropolitan Territory of The Netherlands','Beatrix',129,'AW');
  61. INSERT INTO `Country` VALUES ('AFG','Afghanistan','Asia','Southern and Central Asia',652090.00,1919,22720000,45.9,5976.00,NULL,'Afganistan/Afqanestan','Islamic Emirate','Mohammad Omar',1,'AF');
  62. INSERT INTO `Country` VALUES ('AGO','Angola','Africa','Central Africa',1246700.00,1975,12878000,38.3,6648.00,7984.00,'Angola','Republic','Jos?Eduardo dos Santos',56,'AO');
  63. INSERT INTO `Country` VALUES ('AIA','Anguilla','North America','Caribbean',96.00,NULL,8000,76.1,63.20,NULL,'Anguilla','Dependent Territory of the UK','Elisabeth II',62,'AI');
  64. INSERT INTO `Country` VALUES ('ALB','Albania','Europe','Southern Europe',28748.00,1912,3401200,71.6,3205.00,2500.00,'Shqip雛ia','Republic','Rexhep Mejdani',34,'AL');
  65. INSERT INTO `Country` VALUES ('AND','Andorra','Europe','Southern Europe',468.00,1278,78000,83.5,1630.00,NULL,'Andorra','Parliamentary Coprincipality','',55,'AD');
  66. INSERT INTO `Country` VALUES ('ANT','Netherlands Antilles','North America','Caribbean',800.00,NULL,217000,74.7,1941.00,NULL,'Nederlandse Antillen','Nonmetropolitan Territory of The Netherlands','Beatrix',33,'AN');
  67. INSERT INTO `Country` VALUES ('ARE','United Arab Emirates','Asia','Middle East',83600.00,1971,2441000,74.1,37966.00,36846.00,'Al-Imarat al-碅rabiya al-Muttahida','Emirate Federation','Zayid bin Sultan al-Nahayan',65,'AE');
  68. INSERT INTO `Country` VALUES ('ARG','Argentina','South America','South America',2780400.00,1816,37032000,75.1,340238.00,323310.00,'Argentina','Federal Republic','Fernando de la R鷄',69,'AR');
  69. INSERT INTO `Country` VALUES ('ARM','Armenia','Asia','Middle East',29800.00,1991,3520000,66.4,1813.00,1627.00,'Hajastan','Republic','Robert Kot歛rjan',126,'AM');
  70. INSERT INTO `Country` VALUES ('ASM','American Samoa','Oceania','Polynesia',199.00,NULL,68000,75.1,334.00,NULL,'Amerika Samoa','US Territory','George W. Bush',54,'AS');
  71. COMMIT;
  72. --
  73. -- Table structure for table `CountryLanguage`
  74. --
  75. DROP TABLE IF EXISTS `CountryLanguage`;
  76. CREATE TABLE `CountryLanguage` (
  77. `CountryCode` char(3) NOT NULL DEFAULT '',
  78. `Language` char(30) NOT NULL DEFAULT '',
  79. `IsOfficial` enum('T','F') NOT NULL DEFAULT 'F',
  80. `Percentage` float(4,1) NOT NULL DEFAULT '0.0',
  81. PRIMARY KEY (`CountryCode`,`Language`),
  82. KEY `CountryCode` (`CountryCode`),
  83. CONSTRAINT `countryLanguage_ibfk_1` FOREIGN KEY (`CountryCode`) REFERENCES `Country` (`Code`)
  84. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  85. --
  86. -- Dumping data for table `CountryLanguage`
  87. --
  88. -- ORDER BY: `CountryCode`,`Language`
  89. INSERT INTO `CountryLanguage` VALUES ('ABW','Dutch','T',5.3);
  90. INSERT INTO `CountryLanguage` VALUES ('ABW','English','F',9.5);
  91. INSERT INTO `CountryLanguage` VALUES ('ABW','Papiamento','F',76.7);
  92. INSERT INTO `CountryLanguage` VALUES ('ABW','Spanish','F',7.4);
  93. INSERT INTO `CountryLanguage` VALUES ('AFG','Balochi','F',0.9);
  94. INSERT INTO `CountryLanguage` VALUES ('AFG','Dari','T',32.1);
  95. INSERT INTO `CountryLanguage` VALUES ('AFG','Pashto','T',52.4);
  96. INSERT INTO `CountryLanguage` VALUES ('AFG','Turkmenian','F',1.9);
  97. INSERT INTO `CountryLanguage` VALUES ('AFG','Uzbek','F',8.8);
  98. INSERT INTO `CountryLanguage` VALUES ('AGO','Ambo','F',2.4);
  99. INSERT INTO `CountryLanguage` VALUES ('AGO','Chokwe','F',4.2);
  100. COMMIT;
  101. SET AUTOCOMMIT=1;

启动 apache

root@kali:~# systemctl start apache2
root@kali:~# systemctl status apache2

apache 的默认主页是  /var/www/html/index.html。直接访问 http://localhost/index.html 

修改 index.html 为 index.php

index.php 内容如下: (数据库连接部分参考:https://www.runoob.com/php/php-pdo.html

  1. <?php
  2. ini_set("display_errors", "On");
  3. error_reporting(E_ALL | E_STRICT);
  4. print('Hello '); // 输出 "Hello " 并且没有换行符
  5. echo "World\n"; // 输出 "World" 并且换行
  6. echo "<br />";
  7. echo "<hr />";
  8. echo '<p align="center">DataBase connect test</p>';
  9. $dbms='mysql'; //数据库类型
  10. $host='127.0.0.1'; //数据库主机名
  11. $dbName='world'; //使用的数据库
  12. $user='root'; //数据库连接用户名
  13. $pass=''; //对应的密码
  14. $dsn="$dbms:host=$host;dbname=$dbName";
  15. try {
  16. // 连接到数据库
  17. $dbh = new PDO($dsn, $user, $pass);
  18. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  19. $dbh->exec('set names utf8');
  20. echo "连接成功<br/>";
  21. // sql 语句
  22. $strsql="SELECT id,name,countrycode FROM `City` LIMIT 5";
  23. //你还可以进行一次搜索操作
  24. foreach ($dbh->query($strsql) as $row) {
  25. //print_r($row); //你可以用 echo($GLOBAL); 来看到这些值
  26. echo "id: {$row['id']}     ";
  27. echo "name: {$row['name']}     ";
  28. echo "countrycode: {$row['countrycode']}     ";
  29. echo "<br />";
  30. }
  31. $dbh = null;
  32. } catch (PDOException $e) {
  33. die ("Error!: " . $e->getMessage() . "<br/>");
  34. }
  35. ?>
  36. <br />
  37. <hr />
  38. <p align="center">input test</p>
  39. <form>
  40. <div>
  41. Input Query ID:
  42. <input type="text" name="search" style="width:60%;" >
  43. <input type="submit" name="submit" value="Search" >
  44. <br /><br />
  45. SQL Query String :
  46. <?php
  47. if(isset($_GET['submit']))
  48. {
  49. $val = $_GET['search'];
  50. $str_sql = "SELECT id,name,countrycode FROM City where id = $val";
  51. echo "<b>$str_sql</b>";
  52. echo "<br />";
  53. $dbms='mysql'; //数据库类型
  54. $host='127.0.0.1'; //数据库主机名
  55. $dbName='world'; //使用的数据库
  56. $user='root'; //数据库连接用户名
  57. $pass=''; //对应的密码
  58. $dsn="$dbms:host=$host;dbname=$dbName";
  59. try {
  60. // 连接到数据库
  61. $dbh = new PDO($dsn, $user, $pass);
  62. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  63. $dbh->exec('set names utf8');
  64. echo "<br /><br />";
  65. // 遍历
  66. foreach ($dbh->query($str_sql) as $row)
  67. {
  68. //print_r($row); //你可以用 echo($GLOBAL); 来看到这些值
  69. echo '<table border="1">';
  70. echo "<tr>";
  71. echo "<td>";
  72. echo "id: {$row['id']}     ";
  73. echo "</td>";
  74. echo "<td>";
  75. echo "name: {$row['name']}     ";
  76. echo "</td>";
  77. echo "<td>";
  78. echo "countrycode: {$row['countrycode']}     ";
  79. echo "</td>";
  80. echo "</tr>";
  81. echo "</table>";
  82. }
  83. $dbh = null;
  84. }
  85. catch (PDOException $e)
  86. {
  87. die ("Error!: " . $e->getMessage() . "<br/>");
  88. }
  89. }
  90. else
  91. {
  92. echo "please input the number ID !!!";
  93. }
  94. ?>
  95. </div>
  96. </form>

浏览器直接访问:http://localhost/index.php

mysql 数据库中结果

到此,我的第一个 php 程序结束。。。。。

一个 简单的 SQL 注入验证

输入要查询的 ID (数字),点击 search 按钮,注意 浏览器 url 变化,传递一个参数 search=1 。然后下面显示查询结果。

现在修改 URL 传递的参数。

修改后的 URL 为 :http://localhost/index.php?search=1 or '1'='1'&submit=Search

再来一个复杂点的 SQL 注入验证:

URL:http://localhost/index.php?search=1 union select code,name,region from Country LIMIT 5;&submit=Search

一个读取文件的 SQL 注入

至此,一个简单的 SQL 注入验证完成。SQL 注入不止这些东西,以后慢慢学习研究。。。

SQL注入攻击与防御 第二版:http://download.csdn.net/detail/hx0_0_8/9284595

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/573878
推荐阅读
相关标签
  

闽ICP备14008679号