mirror of
https://github.com/ReaJason/MemShellParty.git
synced 2026-09-25 00:11:52 +08:00
feat: support jar packer
This commit is contained in:
@@ -3,11 +3,10 @@ package com.reajason.javaweb.memshell.packer;
|
|||||||
import com.reajason.javaweb.memshell.config.GenerateResult;
|
import com.reajason.javaweb.memshell.config.GenerateResult;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import net.bytebuddy.ByteBuddy;
|
import net.bytebuddy.ByteBuddy;
|
||||||
import org.apache.commons.io.FileUtils;
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
@@ -30,7 +29,6 @@ public class AgentJarPacker implements JarPacker {
|
|||||||
@Override
|
@Override
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public byte[] packBytes(GenerateResult generateResult) {
|
public byte[] packBytes(GenerateResult generateResult) {
|
||||||
Path jarPath = Files.createTempFile("temp", ".jar");
|
|
||||||
String mainClass = generateResult.getInjectorClassName();
|
String mainClass = generateResult.getInjectorClassName();
|
||||||
String advisorClass = generateResult.getShellClassName();
|
String advisorClass = generateResult.getShellClassName();
|
||||||
|
|
||||||
@@ -40,8 +38,8 @@ public class AgentJarPacker implements JarPacker {
|
|||||||
manifest.getMainAttributes().putValue("Premain-Class", mainClass);
|
manifest.getMainAttributes().putValue("Premain-Class", mainClass);
|
||||||
manifest.getMainAttributes().putValue("Can-Redefine-Classes", "true");
|
manifest.getMainAttributes().putValue("Can-Redefine-Classes", "true");
|
||||||
manifest.getMainAttributes().putValue("Can-Retransform-Classes", "true");
|
manifest.getMainAttributes().putValue("Can-Retransform-Classes", "true");
|
||||||
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||||
try (JarOutputStream targetJar = new JarOutputStream(new FileOutputStream(jarPath.toFile()), manifest)) {
|
try (JarOutputStream targetJar = new JarOutputStream(byteArrayOutputStream, manifest)) {
|
||||||
addDependency(targetJar, ByteBuddy.class);
|
addDependency(targetJar, ByteBuddy.class);
|
||||||
|
|
||||||
targetJar.putNextEntry(new JarEntry(mainClass.replace('.', '/') + ".class"));
|
targetJar.putNextEntry(new JarEntry(mainClass.replace('.', '/') + ".class"));
|
||||||
@@ -52,9 +50,7 @@ public class AgentJarPacker implements JarPacker {
|
|||||||
targetJar.write(generateResult.getShellBytes());
|
targetJar.write(generateResult.getShellBytes());
|
||||||
targetJar.closeEntry();
|
targetJar.closeEntry();
|
||||||
}
|
}
|
||||||
byte[] byteArray = IOUtils.toByteArray(new FileInputStream(jarPath.toFile()));
|
return byteArrayOutputStream.toByteArray();
|
||||||
FileUtils.deleteQuietly(jarPath.toFile());
|
|
||||||
return byteArray;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
|
|||||||
@@ -48,6 +48,9 @@ public interface Packer {
|
|||||||
* GzipBase64
|
* GzipBase64
|
||||||
*/
|
*/
|
||||||
GzipBase64(new GzipBase64()),
|
GzipBase64(new GzipBase64()),
|
||||||
|
|
||||||
|
Jar(new SimpleJarPacker()),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* BCEL
|
* BCEL
|
||||||
*/
|
*/
|
||||||
@@ -81,6 +84,8 @@ public interface Packer {
|
|||||||
|
|
||||||
Velocity(new VelocityPacker()),
|
Velocity(new VelocityPacker()),
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
AgentJar(new AgentJarPacker()),
|
AgentJar(new AgentJarPacker()),
|
||||||
;
|
;
|
||||||
private final Packer packer;
|
private final Packer packer;
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package com.reajason.javaweb.memshell.packer;
|
||||||
|
|
||||||
|
import com.reajason.javaweb.memshell.config.GenerateResult;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.util.jar.JarEntry;
|
||||||
|
import java.util.jar.JarOutputStream;
|
||||||
|
import java.util.jar.Manifest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author ReaJason
|
||||||
|
* @since 2025/1/22
|
||||||
|
*/
|
||||||
|
public class SimpleJarPacker implements JarPacker {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SneakyThrows
|
||||||
|
public byte[] packBytes(GenerateResult generateResult) {
|
||||||
|
String mainClass = generateResult.getInjectorClassName().replace('.', '/') + ".class";
|
||||||
|
String advisorClass = generateResult.getShellClassName().replace('.', '/') + ".class";
|
||||||
|
|
||||||
|
Manifest manifest = new Manifest();
|
||||||
|
manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
|
||||||
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||||
|
try (JarOutputStream targetJar = new JarOutputStream(byteArrayOutputStream, manifest)) {
|
||||||
|
targetJar.putNextEntry(new JarEntry(mainClass));
|
||||||
|
targetJar.write(generateResult.getInjectorBytes());
|
||||||
|
targetJar.closeEntry();
|
||||||
|
|
||||||
|
targetJar.putNextEntry(new JarEntry(advisorClass));
|
||||||
|
targetJar.write(generateResult.getShellBytes());
|
||||||
|
targetJar.closeEntry();
|
||||||
|
}
|
||||||
|
return byteArrayOutputStream.toByteArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user