簡體   English   中英

如何使用JavaScript解碼base64編碼的文件

[英]How to decode a file from base64 encoding with JavaScript

我的公司有一個非常嚴格的內聯網工作相關,網絡有一個單一的門口允許文件進出。 門口的安全性不允許使用特殊類型的文件(僅限* .txt,* .doc等),即使在這些特定類型的文件中,它也會搜索批准文件真正屬於那種類型的模式。 (您不能簡單地將* .zip文件偽裝成* .doc文件。)

作為一個安全項目,我被告知找到繞過這個系統的方法,並插入一個單詞C語言.exe文件,上面寫着'Hello World'

我認為將擴展名更改為.txt,而base64對其進行編碼,以便系統更容易接受。 問題是,如果它進入后如何解碼它。在外面非常容易,PHP或任何其他體面的語言可以為我做。 但是,在那里,我可以訪問的唯一真正的語言是JavaScript(在IE6上,也許,MAYBE,在IE8上)。

所以問題如下,我可以使用JavaScript從文件系統中讀取文件,解碼並將其寫回來嗎? 或者至少為我顯示結果?

注意,我不要求解碼/編碼消息,這個很容易,我期待解碼編碼文件

謝謝。

僅使用javascript(即沒有像AIR等插件),瀏覽器不允許訪問文件系統。 不僅無法將文件寫入磁盤,甚至無法讀取它 - 瀏覽器對此類情況非常嚴格,謝天謝地。

JSON可能是您正在尋找的答案。 它實際上可以做到這一點。

  1. 以JSON格式對txt文件進行編碼。 它很可能通過貴公司的門口安檢

     var myJsonData = { "text" : "SGVsbG8sIHdvcmxkIQ==" }; // <-- base64 for "Hello, world!" 
  2. 使用普通的html腳本語法導入txt文件

     <script src="hello.txt" type="text/javascript"> </script> 
  3. 而已! 現在,您可以使用語法訪問JSON對象:

     alert(myJsonData.text); 
  4. 要完成您的工作,請使用這個簡單的Javascript base64解碼器。

  5. 你完成了。 這是我用過的(非常簡單的)代碼:

     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=windows-1250"> <meta name="generator" content="PSPad editor, www.pspad.com"> <title></title> <script src="base64utils.js" type="text/javascript"> </script> <script src="hello.txt" type="text/javascript"> </script> <script type="text/javascript"> function helloFunction() { document.getElementById("hello").innerHTML = decode64(myJsonData.text); } </script> </head> <body onload="helloFunction();"> <p id="hello"></p> </body> </html> 

你不能在瀏覽器中使用直接JS,安全上下文和DOM不允許文件系統訪問。

對於當前版本的閃存,你不能這樣做,舊版本(pre 7 IIRC)有一些允許文件系統訪問的安全漏洞。

您可以使用自定義插件,可能是簽名的Java小程序或COM(ActiveX組件,僅IE)。

我建議與IT部門合作開發內部網以打開本案例所需的上下文/權限,因為這可能是您在此處所需的最短路徑。 或者,您可以創建一個命令行實用程序,以便輕松加密/解密由公用密鑰簽名的給定文件。

這一切都取決於你如何獲取文件。如果你有base-64編碼的exe作為.txt,你可以很容易地使用Flash! 我不太確定你將如何實現它,但你可以使用flex將文件加載到flash和as3中。

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

    <mx:Script>
        <![CDATA[
            import flash.net.FileReference;
            import flash.net.FileFilter;

            import flash.events.IOErrorEvent;
            import flash.events.Event;

            import flash.utils.ByteArray;

            //FileReference Class well will use to load data
            private var fr:FileReference;

            //File types which we want the user to open
            private static const FILE_TYPES:Array = [new FileFilter("Text File", "*.txt;*.text")];

            //called when the user clicks the load file button
            private function onLoadFileClick():void
            {
                //create the FileReference instance
                fr = new FileReference();

                //listen for when they select a file
                fr.addEventListener(Event.SELECT, onFileSelect);

                //listen for when then cancel out of the browse dialog
                fr.addEventListener(Event.CANCEL,onCancel);

                //open a native browse dialog that filters for text files
                fr.browse(FILE_TYPES);
            }

            /************ Browse Event Handlers **************/

            //called when the user selects a file from the browse dialog
            private function onFileSelect(e:Event):void
            {
                //listen for when the file has loaded
                fr.addEventListener(Event.COMPLETE, onLoadComplete);

                //listen for any errors reading the file
                fr.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);

                //load the content of the file
                fr.load();
            }

            //called when the user cancels out of the browser dialog
            private function onCancel(e:Event):void
            {
                trace("File Browse Canceled");
                fr = null;
            }

            /************ Select Event Handlers **************/

            //called when the file has completed loading
            private function onLoadComplete(e:Event):void
            {
                //get the data from the file as a ByteArray
                var data:ByteArray = fr.data;

                //read the bytes of the file as a string and put it in the
                //textarea
                outputField.text = data.readUTFBytes(data.bytesAvailable);

                //clean up the FileReference instance

                fr = null;
            }

            //called if an error occurs while loading the file contents
            private function onLoadError(e:IOErrorEvent):void
            {
                trace("Error loading file : " + e.text);
            }

        ]]>
    </mx:Script>

    <mx:Button label="Load Text File" right="10" bottom="10" click="onLoadFileClick()"/>
    <mx:TextArea right="10" left="10" top="10" bottom="40" id="outputField"/>

</mx:Application>

要解碼它,請查看http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/utils/Base64Decoder.html

如果安全系統掃描文件中的模式,則它不太可能忽略文件中的base64編碼文件或base64編碼內容。 電子郵件附件是base64編碼的,如果系統有任何好處,它將掃描可能有害的電子郵件附件,即使它們被命名為.txt。 幾乎可以肯定它確認了一個EXE文件的base64編碼的開頭。 所以ISTM你問的是錯誤的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM