提问人:Madara's Ghost 提问时间:11/27/2011 最后编辑:NakilonMadara's Ghost 更新时间:1/7/2023 访问量:18294
如何使用 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 上,也许,也许,在 IE8 上)。
那么问题来了,我可以用JavaScript从文件系统中读取一个文件,解码它,然后写回来吗?或者至少为我显示结果?
请注意,我不要求对消息进行解码/编码,这很容易,我希望对文件进行解码/编码。
答:
仅使用 javascript(即没有 AIR 等插件),浏览器不允许访问文件系统。不仅无法将文件写入磁盘,甚至无法读取它 - 谢天谢地,浏览器对这种事情非常严格。
评论
你不能在浏览器中使用直接的 JS 来做到这一点,安全上下文和 DOM 不允许文件系统访问。
你不能用当前版本的闪存来做到这一点,旧版本(7 IIRC之前)有一些安全漏洞,允许文件系统访问。
您可以使用自定义插件,也可以使用签名的 Java 小程序或 COM(ActiveX 组件,仅限 IE)来执行此操作。
我建议与 IT 部门就您的 Intranet 进行合作,以打开在这种情况下所需的上下文/权限,因为这可能是实现您在此处想要的内容的最短路径。或者,您可以创建一个命令行实用程序,以轻松加密/解密由通用密钥签名的给定文件。
评论
JSON 可能是您正在寻找的答案。它实际上可以解决问题。
以 JSON 格式对 txt 文件进行编码。它很有可能通过贵公司的门口安检
var myJsonData = { "text" : "SGVsbG8sIHdvcmxkIQ==" }; // <-- base64 for "Hello, world!"
使用纯 html 脚本语法导入 txt 文件
<script src="hello.txt" type="text/javascript"> </script>
就是这样!现在,您可以使用以下语法访问 JSON 对象:
alert(myJsonData.text);
要完成您的工作,请获取这个简单的 Javascript base64 解码器。
大功告成。这是我使用的(非常简单的)代码:
<!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>
评论
这完全取决于您如何获取文件。如果您将 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 你问错了问题。
评论