forked from 0xrumble/BytecodeScreen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJarUtil.java
More file actions
148 lines (137 loc) · 6.09 KB
/
JarUtil.java
File metadata and controls
148 lines (137 loc) · 6.09 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package tools;
import basic_class.ClassFile;
import org.slf4j.Logger;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
public class JarUtil {
@SuppressWarnings("all")private static Logger logger;
private static final Set<ClassFile> classFileSet = new HashSet<>();
public static List<ClassFile> resolveNormalfolderFile(String jarPath) {
List<String> fileNames = new ArrayList<String>();
File file = new File(jarPath);
resolvefile(file,fileNames);
for (String value : fileNames) {
String md = value.replace(file.getAbsolutePath()+"\\","").replace("\\","/");
Path path = Paths.get(value);
ClassFile classFile = new ClassFile(md, path);
classFileSet.add(classFile);
}
return new ArrayList<>(classFileSet);
}
public static List<ClassFile> resolveNormalJarFile(String jarPath) {
try {
final Path tmpDir = Files.createTempDirectory(
Paths.get(jarPath).getFileName().toString() + "_");
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
DirUtil.removeDir(tmpDir.toFile());
}));
resolve(jarPath, tmpDir);
return new ArrayList<>(classFileSet);
} catch (Exception e) {
}
return new ArrayList<>();
}
private static void resolve(String jarPath, Path tmpDir) {
try {
InputStream is = new FileInputStream(jarPath);
JarInputStream jarInputStream = new JarInputStream(is);
JarEntry jarEntry;
while ((jarEntry = jarInputStream.getNextJarEntry()) != null) {
Path fullPath = tmpDir.resolve(jarEntry.getName());
if (!jarEntry.isDirectory()) {
if (!jarEntry.getName().endsWith(".class")) {
continue;
}
Path dirName = fullPath.getParent();
if (!Files.exists(dirName)) {
Files.createDirectories(dirName);
}
OutputStream outputStream = Files.newOutputStream(fullPath);
IOUtil.copy(jarInputStream, outputStream);
ClassFile classFile = new ClassFile(jarEntry.getName(), fullPath);
// fullpath C:\Users\hll\AppData\Local\Temp\CIDemo.jar_4817332695572281497\BOOT-INF\classes\org\sec\cidemo\web\XXEController.class
//classFIle BOOT-INF/classes/org/sec/cidemo/web/XXEController.class
//System.out.println("jarentry "+jarEntry.getName()); //获取到类名和springboot()
//System.out.println("fullpath "+fullPath);
// System.out.println("classFIle "+classFile.getClassName());
classFileSet.add(classFile);
}
}
} catch (Exception e) {
}
}
public static List<ClassFile> resolveSpringBootJarFile(String jarPath, boolean useAllLib) {
try {
final Path tmpDir = Files.createTempDirectory(
Paths.get(jarPath).getFileName().toString() + "_");
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
DirUtil.removeDir(tmpDir.toFile());
}));
resolve(jarPath, tmpDir);
//判断是否加入其它springboot中自带的依赖
if (useAllLib) {
resolveBoot(jarPath, tmpDir);
Files.list(tmpDir.resolve("BOOT-INF/lib")).forEach(p ->
resolveNormalJarFile(p.toFile().getAbsolutePath()));
}
return new ArrayList<>(classFileSet);
} catch (Exception e) {
}
return new ArrayList<>();
}
private static void resolveBoot(String jarPath, Path tmpDir) {
try {
InputStream is = new FileInputStream(jarPath);
JarInputStream jarInputStream = new JarInputStream(is);
JarEntry jarEntry;
while ((jarEntry = jarInputStream.getNextJarEntry()) != null) {
Path fullPath = tmpDir.resolve(jarEntry.getName());
if (!jarEntry.isDirectory()) {
if (!jarEntry.getName().endsWith(".jar")) {
continue;
}
Path dirName = fullPath.getParent();
if (!Files.exists(dirName)) {
Files.createDirectories(dirName);
}
OutputStream outputStream = Files.newOutputStream(fullPath);
IOUtil.copy(jarInputStream, outputStream);
}
}
} catch (Exception e) {
}
}
public static void resolvefile(File dir, List<String> fileNames) {
if (!dir.exists() || !dir.isDirectory()) {// 判断是否存在目录
if (dir.isFile()) {// 如果文件
int is = dir.getName().lastIndexOf('.');
System.out.println(dir.getName().substring(is+1));
if(dir.getName().substring(is+1).equals("class")){
fileNames.add(dir.getAbsolutePath());// 添加文件全路径名
}
}
System.out.println("error:路径无法获取");
return;
}
String[] files = dir.list();// 读取目录下的所有目录文件信息
for (int i = 0; i < files.length; i++) {// 循环,添加文件名或回调自身
File file = new File(dir, files[i]);
if (file.isFile()) {// 如果文件
int is = files[i].lastIndexOf('.');
if(files[i].substring(is+1).equals("class")){
fileNames.add(dir + "\\" + file.getName());// 添加文件全路径名
}
} else {// 如果是目录
resolvefile(file, fileNames);// 回调自身继续查询
}
}
}
}