-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathWinPE.java
More file actions
executable file
·125 lines (105 loc) · 3.25 KB
/
Copy pathWinPE.java
File metadata and controls
executable file
·125 lines (105 loc) · 3.25 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
package javaforce.utils;
import javaforce.api.windows.WindowsAPI;
import java.io.*;
import javaforce.*;
import javaforce.api.*;
/** Tool to add resources to Windows PE exe files.
*
* Based on WinRun4J source code (converted to Java).
*
* @author pquiring
*/
public class WinPE {
public static void usage() {
System.out.println("WinPE : Add resources to Windows EXE PE files");
System.out.println("Usage : WinPE exeFile file(s)");
System.out.println("Supported : .ico .manifest .cfg");
System.exit(0);
}
private static final int RT_ICON = 3;
private static final int RT_GROUP_ICON = 11 + 3;
private static final int RT_STRING = 6; //too complex
private static final int RT_RCDATA = 10; //raw data (used to store .cfg file)
private static final int RT_MANIFEST = 24;
public static void main(String[] args) {
if (!JF.isWindows()) {System.out.println("For windows only"); return;}
if (args == null || args.length < 2) usage();
String exeFile = args[0];
for(int a=1;a<args.length;a++) {
String file = args[a];
if (file.endsWith(".ico")) {
addIcon(exeFile, file);
}
else if (file.endsWith(".manifest")) {
addManifest(exeFile, file);
}
else if (file.endsWith(".cfg")) {
addCfg(exeFile, file);
}
else {
System.out.println("Unsupported file:" + file);
}
}
}
public static void addIcon(String exeFile, String icoFile) {
try {
//LoadIcon
FileInputStream fis = new FileInputStream(icoFile);
byte[] ico = JF.readAll(fis);
fis.close();
//Begin
long exe = WindowsAPI.getInstance().peBegin(exeFile);
if (exe == 0) {
System.out.println("Unable to open:" + exeFile);
return;
}
//add icon(s)
WindowsAPI.getInstance().peAddIcon(exe, ico, 0, ico.length);
//end
WindowsAPI.getInstance().peEnd(exe);
System.out.println("Added:" + icoFile);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void addManifest(String exeFile, String manifestFile) {
try {
FileInputStream fis = new FileInputStream(manifestFile);
byte[] str = JF.readAll(fis);
fis.close();
//Begin
long exe = WindowsAPI.getInstance().peBegin(exeFile);
if (exe == 0) {
System.out.println("Unable to open:" + exeFile);
return;
}
//add manifest
WindowsAPI.getInstance().peAddString(exe, RT_MANIFEST, 1, str, 0, str.length);
//end
WindowsAPI.getInstance().peEnd(exe);
System.out.println("Added:" + manifestFile);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void addCfg(String exeFile, String cfgFile) {
try {
FileInputStream fis = new FileInputStream(cfgFile);
byte[] str = JF.readAll(fis);
fis.close();
//Begin
long exe = WindowsAPI.getInstance().peBegin(exeFile);
if (exe == 0) {
System.out.println("Unable to open:" + exeFile);
return;
}
//add cfg
WindowsAPI.getInstance().peAddString(exe, RT_RCDATA, 1, str, 0, str.length);
//end
WindowsAPI.getInstance().peEnd(exe);
System.out.println("Added:" + cfgFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}