Skip to content

Instantly share code, notes, and snippets.

@karelmikie3
Created August 7, 2018 21:52
Show Gist options
  • Save karelmikie3/b8365fdd46a4fac562798cc734c7054d to your computer and use it in GitHub Desktop.
Save karelmikie3/b8365fdd46a4fac562798cc734c7054d to your computer and use it in GitHub Desktop.
package com.karelmikie3.wendybot;
import java.io.File;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.util.*;
import java.util.jar.JarFile;
public class WendyBotAgent {
public static void premain(String args, Instrumentation instrumentation) {
//deploader for bot
File dir = new File("libs");
if (dir.listFiles() != null) {
for (File file : Objects.requireNonNull(dir.listFiles())) {
try {
instrumentation.appendToSystemClassLoaderSearch(new JarFile(file));
} catch (IOException e) {
e.printStackTrace();
}
}
}
//magic for enjoyment
instrumentation.addTransformer((loader, className, classBeingRedefined, protectionDomain, classfileBuffer) -> {
try {
LinkedList<Byte> list = new LinkedList<>(Arrays.asList(toObjects(classfileBuffer)));
if (className != null && className.startsWith("")) {
Map<Integer, Integer> UTFSpotSTRSpot = new HashMap<>();
Map<Integer, String> strIndexNewString = new HashMap<>();
int size = ((Byte.toUnsignedInt(list.get(8)) << 8) + Byte.toUnsignedInt(list.get(9)));
int spot = 1;
for (int i = 10; i < list.size(); i++) {
if (spot < size) {
Constant constant = Constant.constant(list.get(i));
switch (constant) {
case Utf8:
StringBuilder builder = new StringBuilder();
i += 2;
int limit = (Byte.toUnsignedInt(list.get(i - 1)) << 8) + Byte.toUnsignedInt(list.get(i));
for (int i1 = 0; i1 < limit; i1++)
builder.append((char) list.get(++i).byteValue());
if (UTFSpotSTRSpot.containsKey(spot) && builder.toString().contains("test"))
strIndexNewString.put(UTFSpotSTRSpot.get(spot), builder.toString().replaceAll("test", "This is definitely not a test"));
spot++;
break;
case String:
UTFSpotSTRSpot.put((Byte.toUnsignedInt(list.get(i + 1)) << 8) + Byte.toUnsignedInt(list.get(i + 2)), i);
default:
i += constant.size;
spot++;
}
} else if (spot == size) {
for (Map.Entry<Integer, String> entry : strIndexNewString.entrySet()) {
LinkedList<Byte> insert = new LinkedList<>();
insert.add((byte) Constant.Utf8.tag);
byte left = (byte) ((entry.getValue().length() & 0xFF00) >>> 8);
byte right = (byte) (entry.getValue().length() & 0xFF);
insert.add(left);
insert.add(right);
for (char c : entry.getValue().toCharArray())
insert.add((byte) c);
list.addAll(i, insert);
i += insert.size();
insert.clear();
byte str_left = (byte) ((spot & 0xFF00) >>> 8);
byte str_right = (byte) (spot & 0xFF);
list.set(entry.getKey() + 1, str_left);
list.set(entry.getKey() + 2, str_right);
spot++;
}
byte left = (byte) ((spot & 0xFF00) >>> 8);
byte right = (byte) (spot & 0xFF);
list.set(8, left);
list.set(9, right);
}
}
}
return toPrimitives(list.toArray(new Byte[0]));
} catch (Exception e) {
e.printStackTrace();
return classfileBuffer;
}
});
}
private static Byte[] toObjects(byte[] bytesPrim) {
Byte[] bytes = new Byte[bytesPrim.length];
Arrays.setAll(bytes, n -> bytesPrim[n]);
return bytes;
}
private static byte[] toPrimitives(Byte[] oBytes) {
byte[] bytes = new byte[oBytes.length];
for(int i = 0; i < oBytes.length; i++)
bytes[i] = oBytes[i];
return bytes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment