Skip to content

Commit 5dfa2c8

Browse files
author
yz
committedDec 8, 2019
添加JNI命令执行Demo
1 parent 50e3da3 commit 5dfa2c8

File tree

11 files changed

+473
-1
lines changed

11 files changed

+473
-1
lines changed
 
346 KB
Loading

‎gitbook/javase/CommandExecution/README.md

+153-1
Large diffs are not rendered by default.

‎javaweb-sec-source/javase/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
<scope>provided</scope>
3030
</dependency>
3131

32+
<dependency>
33+
<groupId>commons-io</groupId>
34+
<artifactId>commons-io</artifactId>
35+
<version>2.6</version>
36+
</dependency>
37+
3238
</dependencies>
3339

3440
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//package com.anbai.sec.cmd;
2+
//
3+
///**
4+
// * 本地命令执行类
5+
// * Creator: yz
6+
// * Date: 2019/12/6
7+
// */
8+
//public class CommandExecution {
9+
//
10+
// public static native String exec(String cmd);
11+
//
12+
//}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.anbai.sec.cmd;
2+
3+
import java.io.File;
4+
import java.lang.reflect.Method;
5+
6+
/**
7+
* Creator: yz
8+
* Date: 2019/12/8
9+
*/
10+
public class CommandExecutionTest {
11+
12+
private static final String COMMAND_CLASS_NAME = "com.anbai.sec.cmd.CommandExecution";
13+
14+
/**
15+
* JDK1.5编译的com.anbai.sec.cmd.CommandExecution类字节码,
16+
* 只有一个public static native String exec(String cmd);的方法
17+
*/
18+
private static final byte[] COMMAND_CLASS_BYTES = new byte[]{
19+
-54, -2, -70, -66, 0, 0, 0, 49, 0, 15, 10, 0, 3, 0, 12, 7, 0, 13, 7, 0, 14, 1,
20+
0, 6, 60, 105, 110, 105, 116, 62, 1, 0, 3, 40, 41, 86, 1, 0, 4, 67, 111, 100,
21+
101, 1, 0, 15, 76, 105, 110, 101, 78, 117, 109, 98, 101, 114, 84, 97, 98, 108,
22+
101, 1, 0, 4, 101, 120, 101, 99, 1, 0, 38, 40, 76, 106, 97, 118, 97, 47, 108, 97,
23+
110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 41, 76, 106, 97, 118, 97, 47, 108,
24+
97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 1, 0, 10, 83, 111, 117, 114,
25+
99, 101, 70, 105, 108, 101, 1, 0, 21, 67, 111, 109, 109, 97, 110, 100, 69, 120,
26+
101, 99, 117, 116, 105, 111, 110, 46, 106, 97, 118, 97, 12, 0, 4, 0, 5, 1, 0, 34,
27+
99, 111, 109, 47, 97, 110, 98, 97, 105, 47, 115, 101, 99, 47, 99, 109, 100, 47, 67,
28+
111, 109, 109, 97, 110, 100, 69, 120, 101, 99, 117, 116, 105, 111, 110, 1, 0, 16,
29+
106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, 116, 0, 33, 0,
30+
2, 0, 3, 0, 0, 0, 0, 0, 2, 0, 1, 0, 4, 0, 5, 0, 1, 0, 6, 0, 0, 0, 29, 0, 1, 0, 1,
31+
0, 0, 0, 5, 42, -73, 0, 1, -79, 0, 0, 0, 1, 0, 7, 0, 0, 0, 6, 0, 1, 0, 0, 0, 7, 1,
32+
9, 0, 8, 0, 9, 0, 0, 0, 1, 0, 10, 0, 0, 0, 2, 0, 11
33+
};
34+
35+
public static void main(String[] args) {
36+
String cmd = "ifconfig";// 定于需要执行的cmd
37+
38+
try {
39+
ClassLoader loader = new ClassLoader(CommandExecutionTest.class.getClassLoader()) {
40+
@Override
41+
protected Class<?> findClass(String name) throws ClassNotFoundException {
42+
try {
43+
return super.findClass(name);
44+
} catch (ClassNotFoundException e) {
45+
return defineClass(COMMAND_CLASS_NAME, COMMAND_CLASS_BYTES, 0, COMMAND_CLASS_BYTES.length);
46+
}
47+
}
48+
};
49+
50+
// 测试时候换成自己编译好的lib路径
51+
File libPath = new File("/Users/yz/IdeaProjects/javaweb-sec/jni/libcmd.jnilib");
52+
53+
// load命令执行类
54+
Class commandClass = loader.loadClass("com.anbai.sec.cmd.CommandExecution");
55+
56+
// 可以用System.load也加载lib也可以用反射ClassLoader加载,如果loadLibrary0
57+
// 也被拦截了可以换java.lang.ClassLoader$NativeLibrary类的load方法。
58+
// System.load("/Users/yz/IdeaProjects/javaweb-sec/jni/libcmd.jnilib");
59+
Method loadLibrary0Method = ClassLoader.class.getDeclaredMethod("loadLibrary0", Class.class, File.class);
60+
loadLibrary0Method.setAccessible(true);
61+
loadLibrary0Method.invoke(loader, commandClass, libPath);
62+
63+
String content = (String) commandClass.getMethod("exec", String.class).invoke(null, cmd);
64+
System.out.println(content);
65+
} catch (Exception e) {
66+
e.printStackTrace();
67+
}
68+
}
69+
70+
}

‎javaweb-sec-source/javase/src/main/webapp/load_library.jsp

+127
Large diffs are not rendered by default.

‎javaweb-sec-source/javaweb-sec-utils/pom.xml

+13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@
1212
<modelVersion>4.0.0</modelVersion>
1313
<artifactId>javaweb-sec-utils</artifactId>
1414

15+
<build>
16+
<plugins>
17+
<plugin>
18+
<groupId>org.apache.maven.plugins</groupId>
19+
<artifactId>maven-compiler-plugin</artifactId>
20+
<configuration>
21+
<source>7</source>
22+
<target>7</target>
23+
</configuration>
24+
</plugin>
25+
</plugins>
26+
</build>
27+
1528
<dependencies>
1629

1730
<dependency>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.io.File;
2+
import java.io.IOException;
3+
import java.nio.file.Files;
4+
import java.util.Arrays;
5+
6+
/**
7+
* 字符串、文件快速转换成byte数组
8+
* Creator: yz
9+
* Date: 2019/12/8
10+
*/
11+
public class Bytes {
12+
13+
public static void main(String[] args) throws IOException {
14+
15+
if (args.length > 0) {
16+
String str = args[0];
17+
byte[] bytes = null;
18+
19+
if (args.length == 2 && str.equals("-f")) {
20+
File file = new File(args[1]);
21+
bytes = Files.readAllBytes(file.toPath());
22+
} else {
23+
bytes = str.getBytes();
24+
}
25+
26+
System.out.println(Arrays.toString(bytes));
27+
} else {
28+
System.out.println("Examples:");
29+
System.out.println("java Bytes [string]");
30+
System.out.println("java Bytes -f [path]");
31+
}
32+
}
33+
34+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Created by yz on 2019/12/6.
3+
//
4+
#include <iostream>
5+
#include <stdlib.h>
6+
#include <cstring>
7+
#include <string>
8+
#include "com_anbai_sec_cmd_CommandExecution.h"
9+
10+
using namespace std;
11+
12+
JNIEXPORT jstring
13+
14+
JNICALL Java_com_anbai_sec_cmd_CommandExecution_exec
15+
(JNIEnv *env, jclass jclass, jstring str) {
16+
17+
if (str != NULL) {
18+
jboolean jsCopy;
19+
const char *cmd = env->GetStringUTFChars(str, &jsCopy);
20+
FILE *fd = popen(cmd, "r");
21+
22+
if (fd != NULL) {
23+
string result;
24+
char buf[128];
25+
26+
while (fgets(buf, sizeof(buf), fd) != NULL) {
27+
result +=buf;
28+
}
29+
30+
pclose(fd);
31+
return env->NewStringUTF(result.c_str());
32+
}
33+
34+
}
35+
36+
return NULL;
37+
}

‎jni/com_anbai_sec_cmd_CommandExecution.h

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎jni/libcmd.jnilib

15.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.