HP+MYSQL website SQL Injection attack and defense
WebjxCom Tip: Programmers pay attention to TDD (Test Driven Development) when writing code: before implementing a function, they will first write a test case, and then write the code to make it run. In fact, when hackers perform SQL Injection, it is also a TDD process: they will first try to make the program report an error, and then correct the parameter content bit by bit. When the program runs successfully again, the injection will follow |
Programmers pay attention to TDD (test-driven development) when writing code: before implementing a function, they will first write a test case, and then write the code to make it run. In fact, when hackers perform SQL Injection, it is also a TDD process: they will first try to make the program report an error, and then correct the parameter content bit by bit. When the program runs successfully again, the injection will be successful.
Attack:
Suppose you have a script similar to the following in your program:
$sql = "SELECT id, title, content FROM articles WHERE id = {$_GET[''id'']}";
During normal access, it The URL is as follows:
/articles.php?id=123
When hackers want to determine whether there is a SQL Injection vulnerability, the most common way is to add a single quote after the integer ID:
/articles.php?id=123''
Since we did not filter the $_GET[''id''] parameter, an error will inevitably be reported, which may be similar to the following information:
supplied argument is not a valid MySQL result resource in...
This information is enough to indicate the existence of the script There is a loophole, we can use some tricks:
/articles.php?id=0 union select 1,2,3
The reason why 1,2,3 is selected is because union requires the number of fields on both sides to be the same, and the front is id. There are three fields of title and content, followed by 1, 2, and 3, so no syntax error will be reported. Also, setting id=0 is a non-existent record, so the query result is 1, 2, 3, which is reflected in On the web page, 1 will be displayed where the id is originally displayed, 2 will be displayed where the title is displayed, and 3 will be displayed where the content is displayed.
As for how to continue to use it, it depends on the settings of magic_quotes_gpc:
When magic_quotes_gpc is off:
/articles.php?id=0 union select 1,2,load_file(''/etc/passwd'')
This is the case As a result, the contents of the /etc/passwd file will be displayed where the content was originally displayed.
When magic_quotes_gpc is on:
If you use load_file(''/etc/passwd'') directly at this time, it will be invalid because the single quotes are escaped, but there is a way:
/articles.php?id =0 union select 1,2,load_file(char(47,101,116,99,47,112,97,115,115,119,100))
The number is the ASCII of the /etc/passwd string: each character of the string is output in a loop ord(...)
except I think you can also use the hexadecimal format of the string: each character of the string is cyclically output dechex(ord(...))
/articles.php?id=0 union select 1,2,load_file(0x2f6574632f706173737764)
Here are just a few attack methods for numeric parameters, which are just the tip of the iceberg. For attack methods such as string parameters, please see the document link below.
Defense:
There are some software similar to SQL Injection Firewall available on the Internet, such as GreenSQL. If the website has begun to suffer SQL Injection attacks, using such a shortcut tool will often save your life. However, such software has a lot of challenges in its architecture. The role of a Proxy will probably affect the concurrent performance of the website, so it is best to make a careful decision based on objective conditions when choosing whether to choose one. In many cases, professional software is not necessary, and there are many lightweight solutions. Here is a demonstration of how to use awk to detect possible vulnerabilities.
Create the detect_sql_injection.awk script with the following content (remember not to include the line number if you want to copy the content):
01 #!/bin/gawk -f
02
03 /$_(GET|POST|COOKIE|REQUEST)s *[/ {
04 IGNORECASE = 1
05 if (match($0, /$.*(sql|query)/)) {
06 IGNORECASE = 0
07 output()
08 next
09 }
10 }
11
12 function output()
13 {
14 $1 = $1
15 print "CRUD: " $0 "nFILE: " FILENAME "nLINE: " FNR "n"
16 }
This script can match problem codes similar to the following , it is easy to extend the matching mode, just write the if match statement like a cat or a tiger.
1: $sql = "SELECT * FROM users WHERE username = ''{$_POST[''username'']}''";
2: $res = mysql_query("SELECT * FROM users WHERE username = ''{ $_POST[''username'']}''");
Don't forget to chmod +x detect_sql_injection.awk before using it. There are two calling methods:
1: ./detect_sql_injection.awk /path/to/php/ script/file
2: find /path/to/php/script/directory -name "*.php" | xargs ./detect_sql_injection.awk
will display the problematic code information, as follows:
CRUD: $sql = "SELECT * FROM users WHERE username = ''{$_POST[''username'']}''";
FILE: /path/to/file.php
LINE: 123
There are many applications of this script in the real environment Methods, such as regularly scanning program source files through CRON, or automatically matching through hook methods when SVN is submitted.
Whether using professional tools or detecting scripts, it is all passive defense. The root of the problem always depends on whether the programmer has the necessary security awareness. Here are some guidelines that must be kept in mind:
1: Digital type Parameters use methods like intval and floatval to force filtering.
2: Use methods like mysql_real_escape_string to force filter string parameters instead of simple addslashes.
3: It is best to abandon the splicing SQL query method such as mysql_query and use PDO's prepare binding method as much as possible.
4: Use rewrite technology to hide real script and parameter information, and filter suspicious parameters through rewrite regular rules.
5: Turn off error prompts and not provide sensitive information to attackers: display_errors=off.
6: Record error information in the form of a log: log_errors= />7: Do not use an account with FILE permissions (such as root) to connect to MySQL. This blocks dangerous functions such as load_file.
8:...
Website security is actually not complicated. It can be summed up in one sentence: filter input and escape output. Among them, the SQL Injection problem we have been discussing above belongs to the problem of filtering input. As for the problem of escaping output, its representative is Cross-site scripting, but it does not belong to the scope of this article, so I will not say more.
Documentation:
addslashes() Versus mysql_real_escape_string()
SQL Injection with MySQL
Advanced SQL Injection with MySQL
Research on exported field content in MySQL injection - Exporting WebShell through injection
Reprinted from: http://www.aspnetjia. com/Cont-328.html
The above has introduced the attack and defense of SQL Injection on the HP+MYSQL website, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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

AI Hentai Generator
Generate AI Hentai for free.

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

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode

"Usage of Division Operation in OracleSQL" In OracleSQL, division operation is one of the common mathematical operations. During data query and processing, division operations can help us calculate the ratio between fields or derive the logical relationship between specific values. This article will introduce the usage of division operation in OracleSQL and provide specific code examples. 1. Two ways of division operations in OracleSQL In OracleSQL, division operations can be performed in two different ways.

The famous activation script MAS2.2 version supports digital activation again. The method originated from @asdcorp and the team. The MAS author calls it HWID2. Download gatherosstate.exe (not original, modified) from https://github.com/massgravel/Microsoft-Activation-Scripts, run it with parameters, and generate GenuineTicket.xml. First take a look at the original method: gatherosstate.exePfn=xxxxxxx;DownlevelGenuineState=1 and then compare with the latest method: gatheros

Oracle and DB2 are two commonly used relational database management systems, each of which has its own unique SQL syntax and characteristics. This article will compare and differ between the SQL syntax of Oracle and DB2, and provide specific code examples. Database connection In Oracle, use the following statement to connect to the database: CONNECTusername/password@database. In DB2, the statement to connect to the database is as follows: CONNECTTOdataba

What is Identity in SQL? Specific code examples are needed. In SQL, Identity is a special data type used to generate auto-incrementing numbers. It is often used to uniquely identify each row of data in a table. The Identity column is often used in conjunction with the primary key column to ensure that each record has a unique identifier. This article will detail how to use Identity and some practical code examples. The basic way to use Identity is to use Identit when creating a table.
