如何使用 JavaScript 从 base64 编码解码文件

How to decode a file from base64 encoding with JavaScript

提问人:Madara's Ghost 提问时间:11/27/2011 最后编辑:NakilonMadara's Ghost 更新时间:1/7/2023 访问量:18294

问:

我公司有一个非常严格的内部网,用于工作相关,网络有一个单一的门口,允许文件进出。门口的安全性不允许特殊类型的文件(*.txt,*.doc等),即使在这些特定类型的文件中,它也会搜索批准文件确实是那种类型的模式。(您不能简单地将 *.zip 文件伪装成 *.doc 文件。

作为一个安全项目,我被告知要找到绕过这个系统的方法,并插入一个C语言.exe文件,上面写着.'Hello World'

我的想法是将扩展名更改为 .txt,并对其进行 base64 编码,以便系统更容易接受。问题是,一旦它进入,如何解码它。从外面看,这很容易,PHP或任何其他像样的语言都可以为我做到这一点。然而,在那里,我唯一可以访问的真正语言是 JavaScript(在 IE6 上,也许,也许,在 IE8 上)。

那么问题来了,我可以用JavaScript从文件系统中读取一个文件,解码它,然后写回来吗?或者至少为我显示结果?

请注意,我不要求对消息进行解码/编码,这很容易,我希望对文件进行解码/编码。

JavaScript 文件 base64

评论

1赞 x4u 11/27/2011
您要解码用户下载并保存到磁盘的文件吗?恐怕这是不可能的,但如果支持,您可以使用 https 通过防火墙/代理隧道传输文件。
0赞 AsTheWormTurns 11/27/2011
@Truth:也许,您可以使用 AIR:检查文件 API,google.it/...
0赞 Madara's Ghost 11/27/2011
这是一个内部系统。我很确定那里没有 AIR。不过我会检查的。
0赞 AsTheWormTurns 11/27/2011
@Truth:IE8 DOM 存储解决方案对您有帮助吗?msdn.microsoft.com/en-us/library/......- cookbooks.adobe.com/...

答:

6赞 Jeff 11/28/2011 #1

仅使用 javascript(即没有 AIR 等插件),浏览器不允许访问文件系统。不仅无法将文件写入磁盘,甚至无法读取它 - 谢天谢地,浏览器对这种事情非常严格。

评论

2赞 Jeff 11/28/2011
一位朋友刚刚建议,如果安装了 flash,它可能会起作用。
0赞 Madara's Ghost 11/28/2011
它已安装。我不知道是哪个版本,但我检查了一下。如果您有闪存解决方案,请添加闪存解决方案。
0赞 Jeff 11/28/2011
对不起,我自己不熟悉 flash - 你可能会更幸运地问另一个问题。
0赞 Jeff 12/1/2011
如果你还在寻找这样的东西,我刚刚发现了一些你可能喜欢的有趣的东西:developer.mozilla.org/en/DOM/FileReader
0赞 Jeff 12/2/2011
哦,爆炸,我忘记了:浏览器兼容性在底部:只有 IE10 支持它。这是合理的,因为我相信它是基于 HTML5 的......对不起。
3赞 Tracker1 12/9/2011 #2

你不能在浏览器中使用直接的 JS 来做到这一点,安全上下文和 DOM 不允许文件系统访问。

你不能用当前版本的闪存来做到这一点,旧版本(7 IIRC之前)有一些安全漏洞,允许文件系统访问。

您可以使用自定义插件,也可以使用签名的 Java 小程序或 COM(ActiveX 组件,仅限 IE)来执行此操作。

我建议与 IT 部门就您的 Intranet 进行合作,以打开在这种情况下所需的上下文/权限,因为这可能是实现您在此处想要的内容的最短路径。或者,您可以创建一个命令行实用程序,以轻松加密/解密由通用密钥签名的给定文件。

评论

0赞 Madara's Ghost 12/9/2011
关键是“闯入”。我不应该与内部人士合作,因为那样会扼杀重点。您能解释一下有关命令行实用程序的更多信息吗?
6赞 loscuropresagio 12/10/2011 #3

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>
    

评论

0赞 loscuropresagio 12/10/2011
我已经在 Ubuntu 中使用 winetricks tu run IE6 对其进行了测试。它有效。我不知道它是否也适用于您的环境,但恕我直言,值得一试。
0赞 Madara's Ghost 12/10/2011
我会尝试的。太糟糕了,赏金将在此之前过期:X
0赞 Madara's Ghost 12/11/2011
到目前为止,您的问题最详细,成功的机会最大。虽然我没有测试过,但赏金是你的,干得好。我会在适当的时候测试它,如果我发现它是正确的,我也会接受答案。干的好!干杯。
2赞 Ari Lotter 12/10/2011 #4

这完全取决于您如何获取文件。如果您将 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

1赞 PointedEars 12/11/2011 #5

如果安全系统扫描文件中的模式,则不太可能忽略文件中的 base64 编码文件或 base64 编码的内容。电子邮件附件是 base64 编码的,如果系统有任何好处,它将扫描可能有害的电子邮件附件,即使它们被命名为 .txt。几乎可以肯定,EXE 文件的 base64 编码开头可以被它识别。所以 ISTM 你问错了问题。

评论

1赞 Madara's Ghost 12/11/2011
关键是要证明这个系统不是很聪明,可以被熟练的攻击者轻易地愚弄。我不确定它是否会检测到它。但我一定会尝试的。谢谢你的回答!