Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

[Tutorial][Source] Store an EXE as an image! [Free]

Store an EXE as an image!
Tutorial and Code by MrZander

Hello HF, here is my second tutorial on this site.
After seeing a vb.NET example of storing a string as an image, I decided to tackle this.

This is CLI, I didn't bother making a GUI.

First is our imports:

Code
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;
import javax.imageio.ImageIO;

EXE To Image

Now, get the location of the EXE and open read all of the bytes
This process takes a while depending on size of file!

Code
public static void filetoimage(){
    try{
  String dir="";
  Scanner s = new Scanner(System.in);
  System.out.println("Enter the location of your exe.");
  dir = s.next();
        FileInputStream fstream = new
        FileInputStream(dir);
        DataInputStream in = new DataInputStream(fstream);
        byte b[]= new byte[in.available()];
        int count=0;
        System.out.println("Reading bytes, please wait . . .");
  while (in.available()!=0){
            b[count]=(in.readByte());
            count=count+1;
        }
        in.close();

Next, get the location to save the image and create it
Code
        imagebytes=b;
        System.out.println("Enter the DIRECTORY to save your file to.");
        saveto = s.next();
        int c = 0;
        int c2=1;
        int size = (int) Math.sqrt(b.length)+2;
        int r, g, b2, rgb;
        BufferedImage buff = new BufferedImage(size,size, BufferedImage.TYPE_INT_RGB);
        System.out.println("Creating image . . . 0/"+Integer.toString(b.length));
            for(int y = 0;y<size;y++){
                System.out.println("Creating image . . . "+Integer.toString(c)+"/"+Integer.toString(b.length));
                for(int x=0;x<size;x++){
        if(c<b.length-1){
        r=b[c];
        g=c2;
        b2=c2;
        rgb = 0xff<<24 | r<<16 | g<<8 | b2;
        buff.setRGB(x, y, rgb); //ALL BYTE DATA IS STORED IN THE RED VALUE
        c=c+1;
        c2 = c2+1;
        if(c2 == 253){
        c2=1; //c2 is simply used to change the color of Green and Blue, looks prettier :P
        }
        }
                }
            }
Finally, save it!
This is obviously customizable, change the name of the executable and so on.

Code
        System.out.println("Saving image.");
        ImageIO.write(buff, "png", new File(saveto+"image.png"));
      System.out.println("Image saved:");
      System.out.println(saveto+"image.png");
        System.out.println("Finished.");
        }
      catch (Exception e){
        System.err.println(e.getMessage());
      }
    }

Image to EXE

Here is where the fun starts!
Retrieving the EXE from the image:

First, get the location of the image using a scanner

Code
public static void imagetofile(){
    String dir="";
    Scanner s = new Scanner(System.in);
    System.out.println("Enter the location of your image.");
    dir = s.next();

Next, We created a new BufferedImage and populate it with the image selected. Then we get the attributes of the image (Height and Width).
Code
try
    {
  BufferedImage image = ImageIO.read(new File(dir));
  byte b[] = new byte[image.getHeight()*image.getWidth()];
  int xmax = image.getWidth()-1;
  int ymax = image.getHeight()-1;
  int count=0;
  System.out.println("Opening image. . . ");

Now for the meat of it, we read the [b]RED value of each pixel in the image.[/b]
Code
for(int y = 0;y<=ymax;y++){
           System.out.println(Integer.toString(count)+"/"+Integer.toString(b.length));
          for(int x = 0;x<=xmax;x++){
              int pixel = image.getRGB(x,y);
                   int red = (pixel >> 16) & 0xff;
                   b[count]=(byte)red;
                   count=count+1;
      }
  }

And Finally!
We get the directory to export to and we write all the bytes in bytearray b[].

Code
System.out.println("Enter the DIRECTORY to save the exe to: ");
  String saveto = s.next();
  try
  {
  FileOutputStream fos = new FileOutputStream(saveto+"executable.exe");
  fos.write(b);
  fos.close();
  }
  catch(Exception ex)
  {
  System.out.println("FileNotFoundException : " + ex);
  }
  System.out.println("File saved:");
  System.out.println(saveto+"executable.exe");
  System.out.println("Finished!");
    }
    catch (Exception e)
    {
    System.out.println(e.getMessage());
    }
    }

Here is an example of hjsplit(a file splitter) stored as an Image
Spoiler (Click to View)

And there we have it!
Thanks for reading and hope it helps.

FULL SOURCE:
Spoiler (Click to View)
There have been a lot of tutorials on this, but this is a really good one, nice job!
(Sep 16, 2010 06:01 PM)The Real Kobe Bryant Wrote:  There have been a lot of tutorials on this, but this is a really good one, nice job!

Thanks!
I searched for some, couldn't find any in Java however.
Nice tutorial !
I should try this. =D
(Sep 16, 2010 06:05 PM)MrZander Wrote:  I searched for some, couldn't find any in Java however.
I don't think it's a repost. Anyway, good job !

PS:
That hjsplit image is such a beautiful rainbow, hha.
So what is this actaully?
is it like if I open the image file(.jpg,etc) will I open the hidden EXE?
(Sep 19, 2010 08:19 AM)!!!! Wrote:  So what is this actaully?
is it like if I open the image file(.jpg,etc) will I open the hidden EXE?

No, it stores the bytes of an exe file in the red value of each pixel in an image, so that you can transfer the exe as an image, and later, extract it for execution.
From what I understand, this is basically a secret way of sending a .exe in a jpg file, the receiver has to then extract and voila he has the .exe.
Wow, great tutorial!
Respect !
Im learning java in school atm. so going to be active allot in the java section ;p
Lucky [2012]! I'm stuck doing dreamweaver and learning how mice work in school. >>

on topic: Yeah, It looks pretty helpful. Although I'm fairly sure it saves it as a .png instead of a .jpg due to the type of compression.
Dude this is dank. +rep on this tut
Thanks guys!

I'm working on some more projects, I hope to post them soon.

Let's hope this section starts getting active!
Looks interesting...but how its going on? i need another exe to decrypt and execute this hidden source?
(Sep 23, 2010 01:24 PM)xms Wrote:  Looks interesting...but how its going on? i need another exe to decrypt and execute this hidden source?

Yes, but it is much easier to bypass an AV with this.
So brainstorming. . .

Let's say we have an archive of two files. We could name the first file keygen.exe and the second logo.png,jpg w/e.

We bind an extractor.exe to ... a keygen, so that when the keygen runs it first reads the "logo" image file, and executes the examined bytes. Then after it has loaded the keylogger,rat etc etc. . . it could load the keygen so it doesn't look suspicious?

Because either way we need a .exe on the other computer that will decode the image, right?

----
So isn't this kind of pointless (as of right now), because we are going to need a .exe in the picture somewhere. And if someone had a FUD keylogger,server anyway then they could just bind THAT to the keygen instead of an extractor to read the secret code in the image?
(Sep 30, 2010 08:42 PM)Ablaze Wrote:  So brainstorming. . .

Let's say we have an archive of two files. We could name the first file keygen.exe and the second logo.png,jpg w/e.

We bind an extractor.exe to ... a keygen, so that when the keygen runs it first reads the "logo" image file, and executes the examined bytes. Then after it has loaded the keylogger,rat etc etc. . . it could load the keygen so it doesn't look suspicious?

Because either way we need a .exe on the other computer that will decode the image, right?

----
So isn't this kind of pointless (as of right now), because we are going to need a .exe in the picture somewhere. And if someone had a FUD keylogger,server anyway then they could just bind THAT to the keygen instead of an extractor to read the secret code in the image?

It's just a POC
You can use it however you want to.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by C++Dec 29, 2019 06:16 PM 16 818 Dec 29, 2019 06:16 PM
Last Post: DNA
[Python] by DistrictApr 7, 2019 03:14 AM 4 222 Apr 7, 2019 03:14 AM
Last Post: lockhead
[.NET] by D3n1s1990Oct 18, 2017 10:26 PM 0 208 Oct 18, 2017 10:26 PM
Last Post: D3n1s1990