-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrippleFlipper.java
70 lines (64 loc) · 1.72 KB
/
CrippleFlipper.java
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
import java.io.IOException;
import java.io.RandomAccessFile;
/*
* Cripple a JAR or other zip file by flipping a bit in the end of central directory record
* This process can be reversed by flipping the bit back.
* Useful for rendering JARs non-executable.
*/
public class CrippleFlipper
{
// The end of central directory record value is little-endian 0x06054b50. The file can be crippled by setting it to 0x07054b50
private void modifyEndOfCentralDirSig(String fileName, byte val, int sig1, int sig2)
{
try
{
RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
long filePos = raf.length();
boolean keepSeeking = true;
int readVal = 0;
int prevReadVal = 0;
while (keepSeeking)
{
filePos--;
raf.seek(filePos);
prevReadVal = readVal;
readVal = raf.read();
if (readVal == sig1 && prevReadVal == sig2)
{
raf.write(new byte[]{val});
keepSeeking = false;
}
}
raf.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public void crippleFile(String fileName)
{
byte val = 7;
this.modifyEndOfCentralDirSig(fileName, val, 5, 6);
}
public void unCrippleFile(String fileName)
{
byte val = 6;
this.modifyEndOfCentralDirSig(fileName, val, 5, 7);
}
public static void main(final String[] args) throws IOException
{
try
{
CrippleFlipper cf = new CrippleFlipper();
if (args[1].equals("c"))
cf.crippleFile(args[0]);
if (args[1].equals("u"))
cf.unCrippleFile(args[0]);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}