Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package modify;
- import java.io.DataInputStream;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.util.Vector;
- /**
- *
- * @author kiriman
- */
- public class Main {
- private static DataInputStream dis;
- private static final int MAGIC = 0xCAFEBABE; // О_о
- public static final int CLASS = 7;
- public static final int FIELDREF = 9;
- public static final int METHODREF = 10;
- public static final int INTERFACEMETHODREF = 11;
- public static final int STRING = 8;
- public static final int INTEGER = 3;
- public static final int FLOAT = 4;
- public static final int LONG = 5;
- public static final int DOUBLE = 6;
- public static final int NAMEANDTYPE = 12;
- public static final int UTF8 = 1;
- private static int class_version, // версия класса
- count_constants_cp; // количество констант
- private static byte[] tag_cp, // тэги записей в пуле
- code; // основной код
- private static Vector cpool, mref; // сами записи
- public static void main(String[] args) {
- if (!isModifyClass()) {
- System.out.println("no");
- } else {
- System.out.println("yes");
- }
- }
- private static int divInt(int p, int l) {
- int a1, a2;
- a1 = (p >> l) & 0xff;
- a2 = (p >> (l - 8)) & 0xff;
- return ((a1 << 8) | (a2)) - 1;
- }
- private static boolean isModifyClass() {
- try {
- dis = new DataInputStream(new FileInputStream("Main.class"));
- dis.readInt(); // MAGIC
- class_version = dis.readInt();
- count_constants_cp = dis.readShort();
- tag_cp = new byte[count_constants_cp];
- cpool = new Vector();
- mref = new Vector(); // для METHODREF записей
- int skip = 10; // кол-во прочтенных байт(int + int + short)
- boolean z = false;
- for (int i = 0; i < count_constants_cp; i++) {
- tag_cp[i] = dis.readByte();
- skip++;
- switch (tag_cp[i]) {
- case METHODREF:
- int lol = dis.readInt();
- cpool.addElement(new Integer(lol));
- mref.addElement(new Integer(lol));
- skip += 4;
- break;
- case UTF8:
- String s = dis.readUTF();
- skip += s.length() + 2;
- cpool.addElement(s);
- if (s.contains("getResourceAsStream")) {
- z = true; // шоу продолжается!
- }
- break;
- case NAMEANDTYPE:
- case INTERFACEMETHODREF:
- case FIELDREF:
- case INTEGER:
- case FLOAT:
- cpool.addElement(new Integer(dis.readInt()));
- skip += 4;
- break;
- case DOUBLE:
- case LONG:
- cpool.addElement(new Long(dis.readLong()));
- skip += 8;
- break;
- case CLASS:
- case STRING:
- cpool.addElement(new Short(dis.readShort()));
- skip += 2;
- break;
- }
- }
- if (!z) { // класс годится для эмуляции
- return false;
- }
- /*
- * разбор METHODREF записей на составляющие(индекс класса и имя метода с типом возвращаемых значений)
- * проверяем, есть ли нужный нам метод - InputStream getResourceAsStream(String)
- * и изменяем класс, ведь его индекс у нас найдется
- */
- int len = mref.size();
- for (int i = 0; i < len; i++) {
- int m = ((Integer) mref.elementAt(i)).intValue();
- int class_index = divInt(m, 24);
- class_index = getClassIndex(class_index);
- int nat_index = divInt(m, 8);
- if (getNameAndType(nat_index).equals("name=getResourceAsStream, type=(Ljava/lang/String;)Ljava/io/InputStream;")) {
- cpool.setElementAt("SomeClass", class_index); // аллилуйя!!!
- }
- }
- } catch (IOException ex) {
- }
- return true;
- }
- private static int getClassIndex(int index) {
- Object element = cpool.elementAt(index);
- int k = ((Short) element).shortValue() - 1;
- if (tag_cp[index] == CLASS) {
- return k;
- } else {
- return index;
- }
- }
- private static String getNameAndType(int index) {
- int nat = ((Integer) (cpool.elementAt(index))).intValue();
- int index_name = divInt(nat, 24);
- String name = (String) cpool.elementAt(index_name);
- int index_type = divInt(nat, 8);
- String type = (String) cpool.elementAt(index_type);
- return "name=" + name + ", type=" + type;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement