


PHP code audit learning dvwa_sql, PHP audit dvwa_sql_PHP tutorial
php code audit learning dvwa_sql, php audit dvwa_sql
0x00
Since I switched to OneNote, I haven’t published a new essay for a long time, but it is still very necessary to think about it. I have started to learn PHP code auditing in the past few days, so let’s start posting these essays first!
First of all, let’s start learning through dvwa, the top ten test platforms. First, bring the links of famous experts for reference here. Thanks for sharing
1.http://drops.wooyun.org/papers/483
2.http://www.lxway.com/86980986.htm is_numeric function bypasses
3.http://www.cnblogs.com/Safe3/archive/2008/08/22/1274095.html Character encoding bypass Wide byte injection
0x01
Here we first bring in the simplest low-level php code
<span> $id</span> = <span>$_GET</span>['id'<span>];//未作任何过滤,防注入处理 </span><span>$getid</span> = "SELECT first_name, last_name FROM users WHERE user_id = '<span>$id</span>'"<span>; </span><span>$result</span> = <span>mysql_query</span>(<span>$getid</span>) or <span>die</span>('<pre class="brush:php;toolbar:false">' . <span>mysql_error</span>() . '' );
Seeing this, we can know that this code actually does not process id, resulting in sql injection vulnerability, ok, all kinds of injections are possible, here it is No more details!
0x02
Medium level, code:
<span> $id</span>=<span>$_GET</span>['id'<span>]; </span><span>$id</span>=<span>mysql_real_escape_string</span>(<span>$id</span><span>);//这里对id进行了转义的操作 </span><span>$getid</span>="SELECTfirst_name,last_nameFROMusersWHEREuser_id=<span>$id</span>";
The mysql_real_escape_string function escapes the id parameter. Common escapes include
- ' => '
- " => "
- => \
- n => \n
Here I think there should be 2 ways to bypass this processing:
1. Numerical injection
Since this function is mainly aimed at processing character-type special characters, we can inject without using special characters, that is, numerical injection
<span> 构造:1</span> untion <span>select</span> <span>user</span>,password <span>from</span> users
From this you can get the account password in the users table. Of course you will ask, what if you don’t know the specific table name and column name? ok, we can try to use union bool injection
<span> 构造:1</span><span>+</span><span>union</span><span>+</span><span>select</span><span>+</span><span>1</span>,(<span>select</span><span>+</span><span>case</span><span>+</span><span>when</span><span>+</span><span>char</span>(<span>72</span>)<span>=</span>(<span>select</span> mid(table_name,<span>0</span>,<span>1</span>) <span>from</span> information_schema.tables limit <span>0</span>,<span>1</span>)<span>+</span><span>then</span><span>+</span><span>2</span><span>+</span><span>end</span>)
The value in char() needs to be transformed and limited, which may take more time. We can write a python script (ps: take a pit first). In fact, the same effect can be achieved using delayed injection
2. Wide byte injection
mysql_real_escape_string The way to escape parameters is to add a '', and its url encoding is , so we are Add �' to the parameter, where � is a legal gbk character
Then after processing by this function, you can find that it will become �\', like this � will swallow a and become a gbk character \'
And the escape character of mysql is also '' is equivalent to injecting a single quote
<span> 构造:1</span><span>%</span>df<span>%</span>5c<span>%</span><span>27</span><span>%</span><span>20</span><span>||</span><span>1</span><span>+</span><span>--</span><span>+ </span>
The same addslashes function also has the same problem. For details, please refer to the link at the beginning of the article
0x02
high级别的php代码
<span>$id</span>=<span>$_GET</span>['id'<span>]; </span><span>$id</span>=<span>stripslashes</span>(<span>$id</span><span>);//剔除参数中的斜杠 </span><span>$id</span>=<span>mysql_real_escape_string</span>(<span>$id</span><span>);//对id中的特殊字符进行转义 </span><span>if</span>(<span>is_numeric</span>(<span>$id</span><span>)){//判断是否是数值或数值字符串 </span>...
好吧,这样一来,我觉得还是变得很安全了,前面2个函数对字符型的注入进行了处理,紧接着is_numeric函数则对数值型注入进行了处理。
然而这样子仍然可以造成sql注入,不过是二次注入,且限制的条件也比较苛刻但是仍有机会造成注入
比如执行sql语句
<span> insert</span> <span>into</span> test(type) <span>values</span>($s);
此时传入的字符串$s=0x31206f722031
这样看可以知道这是一个16进制数,可以通过该函数的检测,然后对16进制解码我们可以发现$s其实实际的值为 ‘1 or 1’
那么这样操作数据库里会变成什么样子
可以看到数据库将这串16进制数进行了转码变成了1 or 1 那么到时候进行数据库取值然后不经处理带入到另一个sql语句中就会造成二次注入.所以我们在写代码的时候不能盲目的信任数据库里的数据,在取出数据时仍需要进行检测。
0x03
sql部分的代码就分析到这里,如有不正确的地方,欢迎拍砖!
下篇准备sql blind :)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
