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

Java 

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

(Apr 2, 2012 12:51 PM)novalyze Wrote:  pixel[i >> 2] = (b[i++] << 24) | (b[i++] & 0xff) << 16) | (b[i++] & 0xff) << 8) | (b[i++] & 0xff);
Exactly man! This method is very wasting, and can be compressed further. Because it's RGB+A, 4 bytes / pixel, store 4 bytes per pixel and leave PNG to the compression :-)

Able to reduce file size to as low as 15% of original size with this method, epic.
what compiler did you use? i mean how run it? newbie asking
Nice job, really clean and informative tutorial! Thanks for this :)
This would be amazing for a java driveby! Couldn't you make it so it downloads it as a jpg and then changes it back to an exe and excecutes it? I'm working on it now! :D
(Jun 22, 2013 07:04 PM)matt Wrote:  This would be amazing for a java driveby! Couldn't you make it so it downloads it as a jpg and then changes it back to an exe and excecutes it? I'm working on it now! :D

Big gravedig =| meh...

You could do what you said, but may as well just download it and run the exe, not really any difference unless the picture is used as trickery.
A more compressed version would store 3 or 4(RGB/RGBA) bytes of data in each pixel, to maximize data storage.

EDIT: Out of curiosity, I decided to make a quick file->PNG and vice versa converter, source:
Code
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import javax.imageio.ImageIO;

public class IMGEXE {
    public static void main(String[] args) {
        boolean toimg = true;
        File from = null;
        if (args.length >= 1) {
            from = new File(args[0]);
            if (!from.exists() || !from.isFile()) {
                System.out.println((from != null ? from.toString() : "") + " does not exist, or is not a file.");
                return;
            }
        }
        File to = null;
        if (args.length >= 2) {
            to = new File(args[1]);
        }
        toimg = !(from.getName().endsWith(".png"));
        try {
            to.mkdirs();
            to.delete();
            to.createNewFile();
        }catch (IOException e) {
            System.out.println("Could not write: " + to.toString());
            return;
        }
        if (toimg) {
            byte[] frm = new byte[] {};
            try {
                byte[] tmp = Files.readAllBytes(from.toPath());
                frm = new byte[tmp.length + 4];
                ByteBuffer bb = ByteBuffer.allocate(4);
                bb.putInt(tmp.length);
                byte[] tmp2 = new byte[4];
                bb.position(0);
                bb.get(tmp2);
                System.arraycopy(tmp2, 0, frm, 0, 4);
                System.arraycopy(tmp, 0, frm, 4, tmp.length);
            }catch (IOException e) {
                System.out.println("Could not read: " + from.toString());
                return;
            }
            int sqr = (int) Math.sqrt(frm.length / 4) + 1; // 4 bytes per pixel @ sqrt so can be squared for number of pixels inside image.
            BufferedImage img = new BufferedImage(sqr, sqr, BufferedImage.TYPE_4BYTE_ABGR);
            for (int x = 0; x < img.getWidth(); x++) {
                for (int y = 0; y < img.getHeight(); y++) {
        int index = (y * img.getWidth() + x) * 4;
        ByteBuffer bb = ByteBuffer.allocate(4);
        for (int p = 0; p < 4; p++) {
        if (index + p >= frm.length) {
        bb.put(p, (byte) 0x0);
        }else {
        bb.put(frm[index + p]);
        }
        }
        img.setRGB(x, y, bb.getInt(0));
                }
            }
            try {
                ImageIO.write(img, to.getName().substring(to.getName().lastIndexOf(".") + 1), to);
            }catch (IOException e) {
                System.out.println("Could not write: " + to.toString());
                return;
            }
        }else {
            BufferedImage img = null;
            try {
                img = ImageIO.read(from);
            }catch (IOException e) {
                System.out.println("Could not read: " + from.toString());
                return;
            }
            if (img != null) {
                ByteBuffer bb2 = ByteBuffer.allocate(4);
                bb2.putInt(0, img.getRGB(0, 0));
                int size = bb2.getInt(0);
                byte[] fil = new byte[size];
                for (int x = 0; x < img.getWidth(); x++) {
        for (int y = 0; y < img.getHeight(); y++) {
        int index = (y * img.getWidth() + x) * 4;
        if (index == 0) continue;
        ByteBuffer bb = ByteBuffer.allocate(4);
        bb.putInt(img.getRGB(x, y));
        for (int p = 0; p < 4; p++) {
        if (index + p - 4 < fil.length) {
        fil[index + p - 4] = bb.get(p);
        }
        }
        }
                }
                try {
        Files.write(to.toPath(), fil, StandardOpenOption.WRITE);
                }catch (IOException e) {
        System.out.println("Could not write: " + from.toString());
        return;
                }
            }
        }
        System.out.println("Done!");
    }
}

I especially like it since it uses the PNG compression algorithms on an EXE, reducing an 11.6mb file to a mere ~5.6 mb. This uses all 4 channels of RGBA, so no space is wasted. It also make some interesting images, similar to OP: [Image: 91EWapC.jpg] This is of Age of Empires 3.


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