The latest Boolean injection attacks and code analysis technology

Click the star toreceive the latest tweets instantly

c7ddeb200652cb67b3956b766bbe268b.png

This article is selected from “Web Security Offense and Defense Penetration Testing Practical Guide (2nd Edition)”

Click on the picture to buy the book at 50% off

a3b6f2afb8bee60b91f860449adca768.gif

Boolean injection attack

The test address of Boolean injection attack is in Chapter 2 of this book.

When accessing this URL, the page returns yes, as shown in Figure 4-25.

89e39b06d69f9a872aae78742a4e8966.jpeg

Figure 4-25

Add a single quote after the URL to access it again, and then you will find that the return result changes from yes to no, as shown in Figure 4-26.

09d5645834b188cc114a1de7bd6be853.jpeg

Figure 4-26

Access id=1′ and 1=1#, id=1′ and 1=2#, and find that the returned results are yes and no respectively. If you change the value of ID, you will find that the returned value is still yes or no. It can be judged from this that the page only returns yes or no, but does not return the data in the database, so Union injection cannot be used here. You can try to use Boolean injection here. Boolean injection refers to constructing a SQL judgment statement, and by looking at the return results of the page to infer which SQL judgment conditions are true, so as to obtain the data in the database. We first determine the length of the database name. The statement is as follows:

1' and length(database())>=1-- + 

There are single quotes, so comment characters are needed to comment. The position of 1 can be any number, such as 1′ and length (database())>=1– +, 1′ and length (database())>=4– + or 1′ and length( database())>=5– +, construct such a statement, and then observe the return results of the page, as shown in Figure 4-27~Figure 4-29.

48495a51fd49f5dd3a15426977aae6b0.jpeg

Figure 4-27

fcd3029e22474df08c65fbfb2bb7191e.jpeg

Figure 4-28

3aa09d1bd6a5a66eec77f27c9bce2e70.jpeg

Figure 4-29

It can be found that when the value is 4, the returned result is yes; and when the value is 5, the returned result is no. The meaning of the entire statement is that if the length of the database name is greater than or equal to 4, the result is yes; if the length of the database name is greater than or equal to 5, the result is no, so it is judged that the length of the database name is 4.

Next, use character-by-character judgment to obtain the database name. The database name generally ranges from a to z, 0 to 9, and may have some special characters. The letters here are not case-sensitive. The SQL statement that is judged character by character is as follows:

1' and substr(database(),1,1)='t'-- + 

Substr means interception, which means intercepting the value of database(), starting from the first character, and only returning one at a time.

The usage of substr is different from the usage of limit, so you need to pay attention. Limit sorts from 0, but here it sorts from 1. You can use Burp Suite’s blasting function to blast the ‘t’ value, as shown in Figure 4-30.

73f258ae037dcc2f7c21387efc893dcb.jpeg

Figure 4-30

It is found that when the value is t, the page returns yes, and other values return no. Therefore, it is judged that the first digit of the database name is t, as shown in Figure 4-31.

839c08ad417d2813fb0b43bc25da33a6.jpeg

Figure 4-31

You can also use ASCII code characters to query. The ASCII code of t is 116. In MySQL, the ASCII conversion function is ord, and the SQL statement that is judged character by character is as follows:

1' and ord(substr(database(),1,1))=116-- + 

As shown in Figure 4-32, the returned result is yes.

4bc300adc0ed9024fd1ebca3889e86db.jpeg

Figure 4-32

We already know from Union injection that the database name is ‘test’, so to determine whether the second letter is e, you can use the following statement:

1' and substr(database(),2,1)='e'-- + 

As shown in Figure 4-33, the returned result is yes.

77037da9e768a1e90422806a1e187fa0.jpeg

Figure 4-33

The statement to query the table name and field name should also be pasted in the database() position. From the Union injection, we already know that the first table name of the database ‘test’ is users, and the first letter should be u. The judgment statement is as follows :

1'and substr((select table_name from information_schema.tables where table_schema='test' limit 0,1),1,1)='u'-- + 

The result is shown in Figure 4-34. The conclusion is correct. By analogy, all table names and field names can be queried.

76530df99599eda1f9495e9a8de112cc.jpeg

Figure 4-34

ff9eed4c1366a1448c65450483a5e182.gif

Boolean injection code analysis

On the Boolean injection page, the program first obtains the GET parameter ID and uses preg_match to determine whether there are dangerous characters such as union/sleep/benchmark. Then splice the parameter ID into the SQL statement and query it in the database. If there is a result, yes is returned, otherwise no is returned. When accessing the page, the code returns yes or no based on the database query results without returning any data in the database, so only yes or no will be displayed on the page. The code is as follows:

<?php
error_reporting(0);
$con=mysqli_connect("localhost","root","123456","test");
if (mysqli_connect_errno())
{
echo "Connection failed: " . mysqli_connect_error();
}
$id = $_GET['id'];
if (preg_match("/union|sleep|benchmark/i", $id)) {
exit("no");
}
$result = mysqli_query($con,"select * from users where `id`='".$id."'");
$row = mysqli_fetch_array($result);
if ($row) {
exit("yes");
}else{
exit("no");
}
?>

When accessing id=1′ or 1=1#, the statement executed by the database is select * from users where `id`=’1′ or 1=1#. Since or 1=1 is a forever true condition, so At this point the page will definitely return yes. When accessing id=1′ and 1=2#, the statement executed by the database is select * from users where `id`= ‘1’ and 1=2#, because and ‘1’=’2 ‘ is a permanent false condition, so the page will definitely return no at this time.

18ef1fcf9464343a27990e879d8bd566.gif

MS08067 security laboratory video number is online

Welcome all students to follow and forward~

– Live training courses under the laboratory –

d8f099335ae70a7ab4c617fbef3fe550.png

6e3afde64428235d7f8ec50776b11eac.jpeg

9f8185eff6d4f2429cfae87206bc8674.jpega95f1ff0b43010f5e214960745fb4932.png

bae10c3c2bff906f48967e2c6b8a0fce.jpeg

9fe0c5b3d90a5f6b947042ae2229bedd.jpeg

bfe6d01fcba7c3c155f09e2aa6329e87.jpeg

9513b15769cfad39d3f82215d89411b7.jpeg

1fde633591f7fc45934b24283eee8771.jpeg


Join MS08067 to study together with 20,000+ classmates

ce5bf6a94992f3f685a840b6257640b6.gif

syntaxbug.com © 2021 All Rights Reserved.