繁体   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