Guest User

Untitled

a guest
Jan 21st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. /**
  2. This function is used to create a hook on the method described in:
  3. package_name.class_name.func_name(func_args)
  4.  
  5. The hook will not modify the functionality of the method but only
  6. print out the provided parameters and the result returned every time
  7. the method is called.
  8.  
  9. Arguments:
  10. package_name - The package name of the class the method is in
  11. class_name - The class name of the class the method is in
  12. func_name - The name of the method we want to monitor
  13. func_args - A list of arguments of the specific method, or undefined
  14. in case we want to hook all available overrides of the method
  15. */
  16. function monitorFunction(package_name, class_name, func_name, func_args) {
  17. try {
  18. const cls = Java.use(package_name + '.' + class_name);
  19. var should_hook = true;
  20. for (var index in cls[func_name].overloads) {
  21. var method_overload = cls[func_name].overloads[index];
  22. if (method_overload.hasOwnProperty('argumentTypes')) {
  23. var msg = "Hooking class: " + class_name + " Function: " + func_name;
  24. var args_types = [];
  25. var param_index = 0;
  26. for (j in method_overload.argumentTypes) {
  27. args_types.push(method_overload.argumentTypes[j].className);
  28. }
  29. // Check if we are looking for a specific overload
  30. if(func_args != undefined) {
  31. should_hook = false;
  32. if(method_overload.argumentTypes.length == func_args.length) {
  33. var num_of_same_args = 0;
  34. for (var a in method_overload.argumentTypes) {
  35. if(method_overload.argumentTypes[a] == func_args[a])
  36. num_of_same_args++;
  37. else break;
  38. }
  39. if(num_of_same_args = func_args.length) should_hook = true;
  40. }
  41. }
  42. if(should_hook) {
  43. send(msg + '(' + args_types.toString() + ')\n');
  44. try {
  45. method_overload.implementation = function () {
  46. var args = [].slice.call(arguments);
  47. var result = this[func_name].apply(this, args);
  48. var rstr = result.toString();
  49. send(func_name+'('+args.join(', ')+') => Result: '+rstr+'\n');
  50. return result;
  51. }
  52. }
  53. catch(e) { send("ERROR: " + e); }
  54. }
  55. }
  56. }
  57. }
  58. catch(e) {
  59. send(e);
  60. }
  61. }
Add Comment
Please, Sign In to add comment