fr1kin

Java org.objectweb.asm findPattern

May 2nd, 2016
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. // maybe I have been bad and haven't completely tested everything
  2.  
  3. public class AsmHelper {
  4.     public static void main(String[] args) {}
  5.  
  6.     public static void printPatternAndMask(AbstractInsnNode start) {
  7.         StringBuilder pattern = new StringBuilder(), mask = new StringBuilder();
  8.         AbstractInsnNode next = start;
  9.         do {
  10.             pattern.append("0x");
  11.             if(next.getOpcode() != F_NEW) {
  12.                 mask.append("x");
  13.                 pattern.append(Integer.toHexString(next.getOpcode()));
  14.             } else {
  15.                 mask.append("?");
  16.                 pattern.append("00");
  17.             }
  18.             pattern.append("\\");
  19.             next = next.getNext();
  20.         } while(next != null);
  21.  
  22.         System.out.printf("\nPattern: " + pattern.toString() + "\n");
  23.         System.out.printf("\nMask: " + mask.toString() + "\n");
  24.     }
  25.  
  26.     public static int[] convertPattern(String pattern) {
  27.         if(pattern.startsWith("\0"))
  28.             pattern = pattern.substring(1);
  29.         String[] hex = pattern.split("\0");
  30.         int[] buff = new int[hex.length];
  31.         int index = 0;
  32.         for(String number : hex) {
  33.             if(number.startsWith("0x"))
  34.                 number = number.substring(2);
  35.             else if(number.startsWith("x"))
  36.                 number = number.substring(1);
  37.             buff[index++] = Integer.parseInt(number, 16);
  38.         }
  39.         return buff;
  40.     }
  41.  
  42.     public static AbstractInsnNode findPattern(AbstractInsnNode start, String pattern, String mask) {
  43.         return findPattern(start,
  44.                 convertPattern(pattern),
  45.                 mask.toCharArray());
  46.     }
  47.  
  48.     public static AbstractInsnNode findPattern(AbstractInsnNode start, int[] pattern, char[] mask) {
  49.         if(start != null &&
  50.                 pattern.length == mask.length) {
  51.             int found = 0;
  52.             AbstractInsnNode next = start;
  53.             do {
  54.                 switch(mask[found]) {
  55.                     // Analyze this node
  56.                     case 'x': {
  57.                         // Check if node and pattern have same opcode
  58.                         if(next.getOpcode() == pattern[found]) {
  59.                             // Increment number of matched opcodes
  60.                             found++;
  61.                         } else {
  62.                             // Go back to the next node of the first matching node
  63.                             for(int i = 1; i <= (found - 1); i++)
  64.                                 next = next.getPrevious();
  65.                             // Reset the number of opcodes found
  66.                             found = 0;
  67.                         }
  68.                         break;
  69.                     }
  70.                     // Skips over this node
  71.                     default:
  72.                     case '?':
  73.                         found++;
  74.                         break;
  75.                 }
  76.                 // Check if found entire pattern
  77.                 if(found >= mask.length) {
  78.                     // Go back to top node
  79.                     for(int i = 1; i <= (found - 1); i++)
  80.                         next = next.getPrevious();
  81.                     return next;
  82.                 }
  83.                 next = next.getNext();
  84.             } while(next != null &&
  85.                     found < mask.length);
  86.         }
  87.         return null;
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment