Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode a file from base64 encoding with JavaScript

My company has a very strict intranet for work related, the net has a single doorway to allow files in and out. The doorway's security does not allow special kinds of files (*.txt, *.doc etc only), and even in those specific kinds of files, it searches for patterns that approve that the file is really that kind. (You can't simply disguise a *.zip file as a *.doc file.)

As a security project, I was told to find a way to bypass this system, and insert a single C language .exe file that says 'Hello World'.

What I thought was to change the extension to .txt, and base64 encode it so that it would be more acceptable for the system. The problem is, how to decode it once it's in. It's very easy on the outside, PHP or any other decent language can do it for me. However, in there, the only real language I have access to is JavaScript (on IE6 and maybe, MAYBE, on IE8).

So the question is as follows, can I use JavaScript to read a file from the file system, decode it, and write it back? or at least display the result for me?

Note that I don't ask for decoding/encoding a message, this one is easy, I look to decode encode a file.

Thanks.

like image 564
Madara's Ghost Avatar asked Nov 27 '11 09:11

Madara's Ghost


People also ask

How do I decode a file with Base64?

To decode a file with contents that are base64 encoded, you simply provide the path of the file with the --decode flag. As with encoding files, the output will be a very long string of the original file. You may want to output stdout directly to a file.

Can Base64 be decoded?

Base64 Decode is very unique tool to decode base64 data to plain text. This tool saves your time and helps to decode base64 data. This tool allows loading the Base64 data URL, which loads base64 encoded text and decodes to human readable text.

How do I decode Base64 strings in typescript?

Encoding and Decoding Strings with Base64 btoa() takes a string and encodes it to Base64. atob() takes a string and decodes it from Base64.

What does == mean in Base64?

When decoding Base64 text, four characters are typically converted back to three bytes. The only exceptions are when padding characters exist. A single = indicates that the four characters will decode to only two bytes, while == indicates that the four characters will decode to only a single byte.


2 Answers

JSON might be the answer you are looking for. It can actually do the trick.

  1. Encode your txt file in JSON format. It is very likely for it to pass your company's doorway security

    var myJsonData = { "text" : "SGVsbG8sIHdvcmxkIQ==" };  // <-- base64 for "Hello, world!"
    
  2. Import your txt file using plain html script syntax

    <script src="hello.txt" type="text/javascript"> </script>
    
  3. That's it! Now you can access a JSON object using the Syntax:

    alert(myJsonData.text);
    
  4. To complete your job, get this simple Javascript base64 decoder.

  5. You're done. Here's the (very simple) code I've used:

    <!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>
    
like image 66
loscuropresagio Avatar answered Oct 15 '22 20:10

loscuropresagio


Using only javascript (i.e. no plugins like AIR etc), browsers don't allow access to the file system. Not only is it not possible to write a file to the disk, it's not possible to even read it - browsers are very strict on that sort of thing, thank goodness.

like image 24
Jeff Avatar answered Oct 15 '22 19:10

Jeff