Mar 27, 2012 07:52 AM
(This post was last modified: Mar 27, 2012 07:53 AM by hgdfhsdgdfgdfh.)
Again not maybe the best quality code.
Converts any byte array to a square bitmap and back.
GOT THE IDEA FROM:
https://hackforums.net/showthread.php?tid=2339901
Special thing: bitmap looks just the same as original bytes/file when opened in text editor (pixel values correspond to original bytes) :P
Works only if bitmap is also saved as bitmap file and not compressed etc.
My 0xViewer.exe converted:
[img]http://img685.imageshack.us/img685/4841/tmp5a88.png[/img]
BMP opened in notepad, you see the PE header:
[img]http://img684.imageshack.us/img684/5541/tmp1742.png[/img]
Maybe gonna add also encryption capability: have other picture with same size used as mask: xor all pixel values. Original data only can be restored using the mask picture :P
NOTE: Avira detects the BITMAP file as DR/FakePic or something like that if exe file is converted to bmp xD
Small sample code I used for test
Converts any byte array to a square bitmap and back.
GOT THE IDEA FROM:
https://hackforums.net/showthread.php?tid=2339901
Special thing: bitmap looks just the same as original bytes/file when opened in text editor (pixel values correspond to original bytes) :P
Works only if bitmap is also saved as bitmap file and not compressed etc.
My 0xViewer.exe converted:
[img]http://img685.imageshack.us/img685/4841/tmp5a88.png[/img]
BMP opened in notepad, you see the PE header:
[img]http://img684.imageshack.us/img684/5541/tmp1742.png[/img]
Maybe gonna add also encryption capability: have other picture with same size used as mask: xor all pixel values. Original data only can be restored using the mask picture :P
NOTE: Avira detects the BITMAP file as DR/FakePic or something like that if exe file is converted to bmp xD
Code
/*
* Convert binary data to bitmap and vice versa
* Copyright (C) 2012 0xDEADDEAD
* Removal of this copyright is prohibited
*/
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Text;
using System.Drawing;
namespace TestApp
{
public static class BitmapDataStorage
{
public static Bitmap CreateBitmapFromData(byte[] binaryData)
{
// calc padding amount
int paddedSize = binaryData.Length + (3 - binaryData.Length%3) + 6;
int pixelCount = paddedSize/3;
int countPerRow = (int) Math.Ceiling(Math.Sqrt(pixelCount));
Bitmap bmp = new Bitmap(countPerRow, countPerRow, PixelFormat.Format24bppRgb);
byte[] paddedData = new byte[paddedSize];
Buffer.BlockCopy(BitConverter.GetBytes(binaryData.Length), 0, paddedData, 0, 4);
Buffer.BlockCopy(binaryData, 0, paddedData, 4, binaryData.Length);
int columnIndex = 0;
int rowNumber = bmp.Height - 1;
for (int i = 0; i < pixelCount; i++)
{
if (columnIndex == countPerRow)
{
columnIndex = 0;
rowNumber--;
}
Color pixelColor = Color.FromArgb(
paddedData[i*3 + 2],
paddedData[i*3 + 1],
paddedData[i*3]);
bmp.SetPixel(columnIndex, rowNumber, pixelColor);
columnIndex++;
}
return bmp;
}
public static byte[] ReadDataFromBitmap(Bitmap bitmap)
{
byte[] buffer = new byte[bitmap.Width*bitmap.Height*3];
int i = 0;
for (int y = bitmap.Height - 1; y >= 0; y--)
{
for (int x = 0; x < bitmap.Width; x++)
{
Color pixelColor = bitmap.GetPixel(x, y);
buffer[i * 3 + 2] = pixelColor.R;
buffer[i * 3 + 1] = pixelColor.G;
buffer[i * 3] = pixelColor.B;
i++;
}
}
byte[] data = new byte[BitConverter.ToInt32(buffer, 0)];
Buffer.BlockCopy(buffer, 4, data, 0, data.Length);
return data;
}
}
}Small sample code I used for test
Code
byte[] buf = System.IO.File.ReadAllBytes("0xViewer.exe");
BitmapDataStorage.CreateBitmapFromData(buf).Save("bmp.bmp", ImageFormat.Bmp);
Bitmap loaded = (Bitmap)Bitmap.FromFile("bmp.bmp");
System.IO.File.WriteAllBytes("result.exe", BitmapDataStorage.ReadDataFromBitmap(loaded));

![[Image: rD7TvGj.gif]](https://i.imgur.com/rD7TvGj.gif)