Skip to content

Commit a1ce0d3

Browse files
committed
* Added in memory hash calculation methods
1 parent ecf09c9 commit a1ce0d3

File tree

11 files changed

+1175
-294
lines changed

11 files changed

+1175
-294
lines changed

core/.idea/workspace.xml

Lines changed: 313 additions & 227 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/src/main/java/droidefense/cli/APKUnpacker.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import apkr.external.modules.helpers.log4j.Log;
44
import apkr.external.modules.helpers.log4j.LoggerType;
55
import droidefense.handler.APKToolHandler;
6-
import droidefense.handler.AXMLDecoderHandler;
76
import droidefense.handler.DirScannerHandler;
87
import droidefense.handler.FileUnzipVFSHandler;
98
import droidefense.handler.base.DirScannerFilter;
109
import droidefense.sdk.model.base.APKFile;
1110
import droidefense.sdk.model.base.DroidefenseProject;
1211
import droidefense.sdk.model.base.HashedFile;
12+
import droidefense.util.UnpackAction;
1313

1414
import java.io.File;
1515
import java.util.ArrayList;
@@ -28,7 +28,7 @@ public ArrayList<HashedFile> unpackWithTechnique(DroidefenseProject currentProje
2828
Log.write(LoggerType.TRACE, "Listing unpacked files...");
2929

3030
//enumerate unpacked files and get information
31-
DirScannerHandler dirHandler = new DirScannerHandler(outputDir, GENERATE_HASHES, new DirScannerFilter() {
31+
DirScannerHandler dirHandler = new DirScannerHandler(outputDir, true, new DirScannerFilter() {
3232
@Override
3333
public boolean addFile(File f) {
3434
return true;
@@ -48,37 +48,11 @@ public boolean addFile(File f) {
4848
@Override
4949
public ArrayList<HashedFile> unpackWithTechnique(DroidefenseProject currentProject, APKFile apkFile, File outputDir) {
5050
//only unpacks
51-
FileUnzipVFSHandler handler = new FileUnzipVFSHandler(currentProject, apkFile);
51+
FileUnzipVFSHandler handler = new FileUnzipVFSHandler(currentProject, apkFile, UnpackAction.GENERATE_HASH);
5252
handler.doTheJob();
53-
Log.write(LoggerType.TRACE, "Listing unpacked files...");
54-
55-
//TODO generate hashes of the files
56-
DirScannerHandler dirHandler = new DirScannerHandler(outputDir, GENERATE_HASHES, new DirScannerFilter() {
57-
@Override
58-
public boolean addFile(File f) {
59-
return true;
60-
}
61-
});
62-
handler.doTheJob();
63-
64-
//get extracted files
65-
ArrayList<HashedFile> files = dirHandler.getFiles();
66-
Log.write(LoggerType.TRACE, "Files found: " + files.size());
67-
68-
Log.write(LoggerType.TRACE, "Decoding XML resources");
69-
//decode unpacked files
70-
AXMLDecoderHandler decoder = new AXMLDecoderHandler(files);
71-
decoder.doTheJob();
72-
73-
//save metadata
74-
currentProject.setFolderCount(dirHandler.getNfolder());
75-
currentProject.setFilesCount(dirHandler.getNfiles());
76-
77-
return files;
53+
return handler.getFiles();
7854
}
7955
};
8056

81-
public static final boolean GENERATE_HASHES = true;
82-
8357
public abstract ArrayList<HashedFile> unpackWithTechnique(DroidefenseProject currentProject, APKFile apkFile, File outputDir);
8458
}

core/src/main/java/droidefense/handler/FileUnzipVFSHandler.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import droidefense.handler.base.AbstractHandler;
66
import droidefense.sdk.model.base.DroidefenseProject;
77
import droidefense.sdk.model.base.HashedFile;
8+
import droidefense.util.UnpackAction;
89

910
import java.io.FileInputStream;
1011
import java.io.FileNotFoundException;
1112
import java.io.IOException;
13+
import java.util.ArrayList;
1214
import java.util.zip.ZipEntry;
1315
import java.util.zip.ZipInputStream;
1416

@@ -23,7 +25,7 @@ public class FileUnzipVFSHandler extends AbstractHandler {
2325

2426
private HashedFile source;
2527

26-
public FileUnzipVFSHandler(DroidefenseProject project, HashedFile source) {
28+
public FileUnzipVFSHandler(DroidefenseProject project, HashedFile source, UnpackAction generateHash) {
2729
this.source = source;
2830
this.root = VirtualFolder.createFolder("unpack");
2931
this.parentNode = root;
@@ -70,6 +72,7 @@ public boolean doTheJob() {
7072
//close zip file access
7173
zipIn.close();
7274
project.setVFS(root);
75+
project.getVFS().print();
7376
return true;
7477
} catch (FileNotFoundException e) {
7578
e.printStackTrace();
@@ -83,4 +86,8 @@ public boolean doTheJob() {
8386
}
8487
return false;
8588
}
89+
90+
public ArrayList<HashedFile> getFiles() {
91+
return null;
92+
}
8693
}

core/src/main/java/droidefense/sdk/helpers/CheckSumGen.java

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import apkr.external.module.ssdeep.exception.SSDeepException;
66
import apkr.external.modules.helpers.log4j.Log;
77
import apkr.external.modules.helpers.log4j.LoggerType;
8+
import droidefense.util.SSDeep;
89

910
import java.io.*;
1011
import java.security.MessageDigest;
@@ -29,17 +30,25 @@ public static CheckSumGen getInstance() {
2930
public String calculateSSDeep(File f) throws SSDeepException {
3031
if (f == null) {
3132
Log.write(LoggerType.ERROR, "Could not create calculateSSDeep() Hash because of a null file reference");
32-
throw new SSDeepException("Apkr could not calculateSSDeep() SSDeep fuzzing hash for file\n\t" + "\nPossible reason: null file reference.");
33+
throw new SSDeepException("Droidefense could not calculateSSDeep() SSDeep fuzzing hash for file\n\t" + "\nPossible reason: null file reference.");
3334
}
3435
SsdeepHashGen test = new SsdeepHashGen();
3536
try {
3637
return test.fuzzy_hash_file(f);
3738
} catch (IOException e) {
38-
Log.write(LoggerType.ERROR, "Apkr could not calculateSSDee()p SSDeep fuzzing hash for file\n\t" + f.getAbsolutePath() + "\nPossible reason: " + e.getLocalizedMessage());
39-
throw new SSDeepException("Apkr could not calculateSSDee()p SSDeep fuzzing hash for file\n\t" + f.getAbsolutePath() + "\nPossible reason: " + e.getLocalizedMessage());
39+
Log.write(LoggerType.ERROR, "Droidefense could not calculateSSDee()p SSDeep fuzzing hash for file\n\t" + f.getAbsolutePath() + "\nPossible reason: " + e.getLocalizedMessage());
40+
throw new SSDeepException("Droidefense could not calculateSSDee()p SSDeep fuzzing hash for file\n\t" + f.getAbsolutePath() + "\nPossible reason: " + e.getLocalizedMessage());
4041
}
4142
}
4243

44+
public String calculateSSDeep(byte[] data) throws SSDeepException {
45+
if (data == null) {
46+
Log.write(LoggerType.ERROR, "Could not create calculateSSDeep() Hash because of a null file reference");
47+
throw new SSDeepException("Droidefense could not calculateSSDeep() SSDeep fuzzing hash for file\n\t" + "\nPossible reason: null file reference.");
48+
}
49+
return SSDeep.generateSSDeep(data);
50+
}
51+
4352
private String calculate(File f, String alg) throws NoSuchAlgorithmException, IOException {
4453
MessageDigest md = MessageDigest.getInstance(alg);
4554
FileInputStream fis = new FileInputStream(f);
@@ -83,7 +92,7 @@ private String calculate(byte[] data, String alg) throws NoSuchAlgorithmExceptio
8392
public long calculateCRC32(File f) throws NullPointerException {
8493
if (f == null) {
8594
Log.write(LoggerType.ERROR, "Could not create calculateCRC32() Hash because of a null file reference");
86-
throw new NullPointerException("Apkr could not calculateCRC32() hash for file\n\t" + "\nPossible reason: null file reference.");
95+
throw new NullPointerException("Droidefense could not calculateCRC32() hash for file\n\t" + "\nPossible reason: null file reference.");
8796
}
8897
try {
8998
InputStream inputStreamn = new FileInputStream(f);
@@ -99,10 +108,20 @@ public long calculateCRC32(File f) throws NullPointerException {
99108
return DEFAULT_RET_CRC32;
100109
}
101110

111+
public long calculateCRC32(byte[] data) throws NullPointerException {
112+
if (data == null) {
113+
Log.write(LoggerType.ERROR, "Could not create calculateCRC32() Hash because of a null file reference");
114+
throw new NullPointerException("Droidefense could not calculateCRC32() hash for file\n\t" + "\nPossible reason: null file reference.");
115+
}
116+
CRC32 crc = new CRC32();
117+
crc.update(data);
118+
return crc.getValue();
119+
}
120+
102121
public String calculateSHA1(File f) throws NullPointerException {
103122
if (f == null) {
104123
Log.write(LoggerType.ERROR, "Could not create calculateSHA1() Hash because of a null file reference");
105-
throw new NullPointerException("Apkr could not calculateSHA1() hash for file\n\t" + "\nPossible reason: null file reference.");
124+
throw new NullPointerException("Droidefense could not calculateSHA1() hash for file\n\t" + "\nPossible reason: null file reference.");
106125
}
107126
try {
108127
return calculate(f, SHA_1);
@@ -112,10 +131,23 @@ public String calculateSHA1(File f) throws NullPointerException {
112131
return DEFAULT_RET;
113132
}
114133

134+
public String calculateSHA1(byte[] data) throws NullPointerException {
135+
if (data == null) {
136+
Log.write(LoggerType.ERROR, "Could not create calculateSHA1() Hash because of a null file reference");
137+
throw new NullPointerException("Droidefense could not calculateSHA1() hash for file\n\t" + "\nPossible reason: null file reference.");
138+
}
139+
try {
140+
return calculate(data, SHA_1);
141+
} catch (NoSuchAlgorithmException | IOException e) {
142+
Log.write(LoggerType.ERROR, "Could not create SHA1 Hash because", e.getLocalizedMessage(), Arrays.toString(e.getStackTrace()));
143+
}
144+
return DEFAULT_RET;
145+
}
146+
115147
public String calculateMD5(File f) throws NullPointerException {
116148
if (f == null) {
117149
Log.write(LoggerType.ERROR, "Could not create calculateSHAMD5() Hash because of a null file reference");
118-
throw new NullPointerException("Apkr could not calculateMD5() hash for file\n\t" + "\nPossible reason: null file reference.");
150+
throw new NullPointerException("Droidefense could not calculateMD5() hash for file\n\t" + "\nPossible reason: null file reference.");
119151
}
120152
try {
121153
return calculate(f, MD5);
@@ -125,10 +157,23 @@ public String calculateMD5(File f) throws NullPointerException {
125157
return DEFAULT_RET;
126158
}
127159

160+
public String calculateMD5(byte[] data) throws NullPointerException {
161+
if (data == null) {
162+
Log.write(LoggerType.ERROR, "Could not create calculateMD5() Hash because of a null file reference");
163+
throw new NullPointerException("Droidefense could not calculateMD5() hash for file\n\t" + "\nPossible reason: null file reference.");
164+
}
165+
try {
166+
return calculate(data, MD5);
167+
} catch (NoSuchAlgorithmException | IOException e) {
168+
Log.write(LoggerType.ERROR, "Could not create MD5 Hash because", e.getLocalizedMessage(), Arrays.toString(e.getStackTrace()));
169+
}
170+
return DEFAULT_RET;
171+
}
172+
128173
public String calculateSHA256(File f) throws NullPointerException {
129174
if (f == null) {
130175
Log.write(LoggerType.ERROR, "Could not create calculateSHA256() Hash because of a null file reference");
131-
throw new NullPointerException("Apkr could not calculateSHA256() hash for file\n\t" + "\nPossible reason: null file reference.");
176+
throw new NullPointerException("Droidefense could not calculateSHA256() hash for file\n\t" + "\nPossible reason: null file reference.");
132177
}
133178
try {
134179
return calculate(f, SHA_256);
@@ -141,7 +186,7 @@ public String calculateSHA256(File f) throws NullPointerException {
141186
public String calculateSHA256(byte[] data) throws NullPointerException {
142187
if (data == null) {
143188
Log.write(LoggerType.ERROR, "Could not create calculateSHA256() Hash because of a null file reference");
144-
throw new NullPointerException("Apkr could not calculateSHA256() hash for file\n\t" + "\nPossible reason: null file reference.");
189+
throw new NullPointerException("Droidefense could not calculateSHA256() hash for file\n\t" + "\nPossible reason: null file reference.");
145190
}
146191
try {
147192
return calculate(data, SHA_256);
@@ -154,7 +199,7 @@ public String calculateSHA256(byte[] data) throws NullPointerException {
154199
public String calculateSHA512(File f) throws NullPointerException {
155200
if (f == null) {
156201
Log.write(LoggerType.ERROR, "Could not create calculateSHA512() Hash because of a null file reference");
157-
throw new NullPointerException("Apkr could not calculateSHA512() hash for file\n\t" + "\nPossible reason: null file reference.");
202+
throw new NullPointerException("Droidefense could not calculateSHA512() hash for file\n\t" + "\nPossible reason: null file reference.");
158203
}
159204
try {
160205
return calculate(f, SHA_512);
@@ -163,4 +208,17 @@ public String calculateSHA512(File f) throws NullPointerException {
163208
}
164209
return DEFAULT_RET;
165210
}
211+
212+
public String calculateSHA512(byte[] data) throws NullPointerException {
213+
if (data == null) {
214+
Log.write(LoggerType.ERROR, "Could not create calculateSHA512() Hash because of a null file reference");
215+
throw new NullPointerException("Droidefense could not calculateSHA512() hash for file\n\t" + "\nPossible reason: null file reference.");
216+
}
217+
try {
218+
return calculate(data, SHA_512);
219+
} catch (NoSuchAlgorithmException | IOException e) {
220+
Log.write(LoggerType.ERROR, "Could not create SHA512 Hash because", e.getLocalizedMessage(), Arrays.toString(e.getStackTrace()));
221+
}
222+
return DEFAULT_RET;
223+
}
166224
}

core/src/main/java/droidefense/sdk/helpers/Util.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ public static String getMIME(File fread) {
188188
return mimeTypesMap.getContentType(fread.getAbsolutePath());
189189
}
190190

191-
public static String getFileExtension(File resource) {
192-
if (resource.getName().contains(".")) {
193-
String[] data = resource.getName().split("\\.");
191+
public static String getFileExtension(final String name) {
192+
if (name.contains(".")) {
193+
String[] data = name.split("\\.");
194194
String extension = data[data.length - 1];
195195
return extension.toUpperCase();
196196
}

core/src/main/java/droidefense/sdk/model/base/DroidefenseProject.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public final class DroidefenseProject implements Serializable {
122122
private transient AbstractFlowMap followCallsMap;
123123
private MachineLearningResult machineLearningResult;
124124
private transient boolean headerReaded;
125+
private boolean VFS;
125126
//private transient DexHeaderReader dexHeaderReader;
126127

127128
public DroidefenseProject(final APKFile file) {
@@ -163,6 +164,7 @@ public void analyze(AbstractAndroidAnalysis analyzer) {
163164
//add this analyzer to used analyzer stack
164165
usedAnalyzers.add(analyzer);
165166
analyzer.setApkFile(sourceFile);
167+
analyzer.setCurrentProject(this);
166168
analyzer.analyzeCode();
167169
}
168170

@@ -683,6 +685,10 @@ private void setSummary(String data) {
683685
summary = data;
684686
}
685687

688+
public VirtualFileSystem getVFS() {
689+
return this.vfs;
690+
}
691+
686692
public void setVFS(VirtualFolder root) {
687693
this.vfs.add("unpack", root);
688694
}

core/src/main/java/droidefense/sdk/model/base/HashedFile.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package droidefense.sdk.model.base;
22

33
import apkr.external.module.ssdeep.exception.SSDeepException;
4+
import apkr.external.modules.vfs.model.impl.VirtualFile;
45
import droidefense.sdk.helpers.CheckSumGen;
56
import droidefense.sdk.helpers.Util;
67

@@ -23,20 +24,47 @@ public HashedFile(String apkPath, boolean generateInformation) {
2324
this(new File(apkPath), generateInformation);
2425
}
2526

27+
public HashedFile(VirtualFile vf, boolean generateInformation) {
28+
this.f = new File(vf.getPath());
29+
filesize = vf.getContentLength();
30+
filename = vf.getName();
31+
declaredExtension = Util.getFileExtension(this.filename);
32+
if (generateInformation) {
33+
if (this.filesize > 0) {
34+
beautyFilesize = Util.beautifyFileSize(this.filesize);
35+
} else {
36+
beautyFilesize = "0 b";
37+
}
38+
39+
//TODO POSSIBLE HASHING BOTTLENECK
40+
byte[] data = vf.getContent();
41+
crc32 = Util.toHexString(CheckSumGen.getInstance().calculateCRC32(data));
42+
md5 = CheckSumGen.getInstance().calculateMD5(data);
43+
sha1 = CheckSumGen.getInstance().calculateSHA1(data);
44+
sha256 = CheckSumGen.getInstance().calculateSHA256(data);
45+
sha512 = CheckSumGen.getInstance().calculateSHA512(data);
46+
try {
47+
ssdeep = CheckSumGen.getInstance().calculateSSDeep(data);
48+
} catch (SSDeepException e) {
49+
e.printStackTrace();
50+
}
51+
}
52+
}
53+
2654
public HashedFile(File parent, boolean generateInformation) {
2755
this.f = parent;
2856
if (this.f.isFile()) {
2957
filesize = this.f.length();
3058
filename = this.f.getName();
31-
declaredExtension = Util.getFileExtension(this.f);
59+
declaredExtension = Util.getFileExtension(this.filename);
3260
if (generateInformation) {
3361
if (this.filesize > 0) {
3462
beautyFilesize = Util.beautifyFileSize(this.filesize);
3563
} else {
3664
beautyFilesize = "0 b";
3765
}
3866

39-
//TODO HASHING BOTTLENECK
67+
//TODO HASHING TIME BOTTLENECK
4068
File currentFile = getThisFile();
4169
crc32 = Util.toHexString(CheckSumGen.getInstance().calculateCRC32(currentFile));
4270
md5 = CheckSumGen.getInstance().calculateMD5(currentFile);

0 commit comments

Comments
 (0)