1. 程式人生 > >fckeditor檔案上傳漏洞(.NET)

fckeditor檔案上傳漏洞(.NET)

前一段時間,由於用了fckeditor,沒有修改fckeditor的漏洞,被人家上傳了木馬,使得網站上出現不良資訊,伺服器被封半個月,心理很鬱悶,所以今天把fckeditor的漏洞以及能徹底解決這些漏洞的辦法寫出來。

    你到網上搜索一下fckeditor漏洞,可以出現很多文章,都是關於它的很多漏洞,可能是轉為他太出名了吧。漏洞太多。

    你仔細看看,所有漏洞,都是是關於一些上傳檔案的。原理是由於IIS對一些改名的檔案解析所導致的。

    由於Fckeditor對第一次上傳123.asp;123.jpg 這樣的格式做了過濾。也就是IIS6解析漏洞。
    上傳第一次。被過濾為123_asp;123.jpg 從而無法執行。
    但是第2次上傳同名檔案123.asp;123.jpg後。由於"123_asp;123.jpg"已經存在。
    檔名被命名為123.asp;123(1).jpg 123.asp;123(2).jpg這樣的編號方式。
    所以。IIS6的漏洞繼續執行了。。。
    然後通過抓包。獲取上傳後地址即可。。

    這樣,就會上傳一個木馬了。我的網站就出現這樣的漏洞,使得整臺伺服器被封IP。
  
    現在我想了一個辦法,修改上傳檔案,把上傳的檔案自動以日期來命名。
   修改方法如下:

    首先找到FCKeditor.Net_2.6.3的原始碼。開啟FileBrowser目錄裡的FileWorkerBase.cs檔案。
   
    找到如下程式碼:
int iErrorNumber = 0;
   int iCounter = 0;

   while ( true )
   {
    string sFilePath = System.IO.Path.Combine( sServerDir, sFileName );

    if ( System.IO.File.Exists( sFilePath ) )
    {
     iCounter++;
     sFileName =
      System.IO.Path.GetFileNameWithoutExtension( oFile.FileName ) +
      "(" + iCounter + ")." +
      sExtension;

     iErrorNumber = 201;
    }
    else
    {
     oFile.SaveAs( sFilePath );
     break;
    }
   }

   TypeConfig typeConfig = this.Config.TypeConfig[resourceType] ;

   string sFileUrl = isQuickUpload ? typeConfig.GetQuickUploadPath() : typeConfig.GetFilesPath() ;
   sFileUrl += sFileName;

   this.SendFileUploadResponse( iErrorNumber, isQuickUpload, sFileUrl, sFileName );


改成如下程式碼:
int iErrorNumber = 0;
   //int iCounter = 0;

            sFileName = this.SanitizeFileName(this.getfilename() + "." + sExtension);
            string sFilePath = System.IO.Path.Combine(sServerDir, sFileName);
            oFile.SaveAs(sFilePath);


  TypeConfig typeConfig = this.Config.TypeConfig[resourceType] ;

            string sFileUrl = "uploadfile/";//isQuickUpload ? typeConfig.GetQuickUploadPath() : typeConfig.GetFilesPath();
   sFileUrl += sFileName;

上面用到的getfilename()類是檔案新的檔名。類程式碼如下:
        public string getfilename()
        {
            System.DateTime currentTime = new System.DateTime();
            currentTime = System.DateTime.Now;
            Random rnd = new Random();
            string rndStr = "";
            for (int i = 0; i <= 4; i++)
            {
                rndStr += rnd.Next(10).ToString();
            }

            return currentTime.ToString("yyyyMMddhhmmssffffff") + rndStr;
        }

這樣所有上傳的檔案都重新命名,不會有重複,而且檔案字尾是程式獲得的字尾.JPG。