Commit 965fd66e authored by Sergey Toshin's avatar Sergey Toshin

Adds more checks for file write ops

parent 7d3caa28
No preview for this file type
...@@ -3,6 +3,7 @@ package jadx.core.codegen; ...@@ -3,6 +3,7 @@ package jadx.core.codegen;
import jadx.api.CodePosition; import jadx.api.CodePosition;
import jadx.core.dex.attributes.nodes.LineAttrNode; import jadx.core.dex.attributes.nodes.LineAttrNode;
import jadx.core.utils.files.FileUtils; import jadx.core.utils.files.FileUtils;
import jadx.core.utils.files.ZipSecurity;
import java.io.File; import java.io.File;
import java.io.PrintWriter; import java.io.PrintWriter;
...@@ -272,10 +273,16 @@ public class CodeWriter { ...@@ -272,10 +273,16 @@ public class CodeWriter {
} }
public void save(File dir, String subDir, String fileName) { public void save(File dir, String subDir, String fileName) {
if(!ZipSecurity.isValidZipEntryName(subDir) || !ZipSecurity.isValidZipEntryName(fileName)) {
return;
}
save(dir, new File(subDir, fileName).getPath()); save(dir, new File(subDir, fileName).getPath());
} }
public void save(File dir, String fileName) { public void save(File dir, String fileName) {
if(!ZipSecurity.isValidZipEntryName(fileName)) {
return;
}
save(new File(dir, fileName)); save(new File(dir, fileName));
} }
......
...@@ -4,6 +4,7 @@ import jadx.api.IJadxArgs; ...@@ -4,6 +4,7 @@ import jadx.api.IJadxArgs;
import jadx.core.codegen.CodeWriter; import jadx.core.codegen.CodeWriter;
import jadx.core.dex.nodes.ClassNode; import jadx.core.dex.nodes.ClassNode;
import jadx.core.utils.exceptions.CodegenException; import jadx.core.utils.exceptions.CodegenException;
import jadx.core.utils.files.ZipSecurity;
import java.io.File; import java.io.File;
...@@ -25,7 +26,7 @@ public class SaveCode extends AbstractVisitor { ...@@ -25,7 +26,7 @@ public class SaveCode extends AbstractVisitor {
public static void save(File dir, IJadxArgs args, ClassNode cls) { public static void save(File dir, IJadxArgs args, ClassNode cls) {
CodeWriter clsCode = cls.getCode(); CodeWriter clsCode = cls.getCode();
String fileName = cls.getClassInfo().getFullPath() + ".java"; String fileName = cls.getClassInfo().getFullPath() + ".java";
if (args.isFallbackMode()) { if (args.isFallbackMode()) {
fileName += ".jadx"; fileName += ".jadx";
} }
clsCode.save(dir, fileName); clsCode.save(dir, fileName);
......
...@@ -10,7 +10,7 @@ public class ZipSecurity { ...@@ -10,7 +10,7 @@ public class ZipSecurity {
private static final Logger LOG = LoggerFactory.getLogger(ZipSecurity.class); private static final Logger LOG = LoggerFactory.getLogger(ZipSecurity.class);
// size of uncompressed zip entry shouldn't be bigger of compressed in MAX_SIZE_DIFF times // size of uncompressed zip entry shouldn't be bigger of compressed in MAX_SIZE_DIFF times
private static final int MAX_SIZE_DIFF = 5; private static final int MAX_SIZE_DIFF = 10;
private static boolean isInSubDirectory(File base, File file) { private static boolean isInSubDirectory(File base, File file) {
if (file == null) { if (file == null) {
...@@ -32,11 +32,11 @@ public class ZipSecurity { ...@@ -32,11 +32,11 @@ public class ZipSecurity {
if(isInSubDirectory(currentPath, canonical)) { if(isInSubDirectory(currentPath, canonical)) {
return true; return true;
} }
LOG.debug("Path traversal attack detected, invalid name: {}", entryName); LOG.error("Path traversal attack detected, invalid name: {}", entryName);
return false; return false;
} }
catch(Exception e) { catch(Exception e) {
LOG.debug("Path traversal attack detected, invalid name: {}", entryName); LOG.error("Path traversal attack detected, invalid name: {}", entryName);
return false; return false;
} }
} }
...@@ -48,7 +48,8 @@ public class ZipSecurity { ...@@ -48,7 +48,8 @@ public class ZipSecurity {
return true; return true;
} }
if(compressedSize * MAX_SIZE_DIFF < uncompressedSize) { if(compressedSize * MAX_SIZE_DIFF < uncompressedSize) {
LOG.debug("Zip bomp attack detected, invalid sizes: compressed {}, uncompressed {}", compressedSize, uncompressedSize); LOG.error("Zip bomp attack detected, invalid sizes: compressed {}, uncompressed {}, name {}",
compressedSize, uncompressedSize, entry.getName());
return true; return true;
} }
return false; return false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment