Il2cppHackerApi.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import {il2cppApi} from "../il2cppApi";
  2. import {log, LogColor, logColor} from "../../logger";
  3. import {soName} from "../../config";
  4. let il2CppImageArray = new Array();
  5. let il2cppBaseAddr = undefined;
  6. let needLog =false;
  7. let methodAddrMap =new Map();
  8. export var Il2cppHackerApi = {
  9. getGameObjectName: function (addr) {
  10. },
  11. getMethodAddr(Il2cppImageName, spaceze, className, methodName, methodCount) {
  12. //判断缓存是否有
  13. let cache = getCache(Il2cppImageName,spaceze,className,methodName,methodCount);
  14. if (cache!==undefined){
  15. return cache;
  16. }
  17. let il2cppImage = this.getTargetCppImage(Il2cppImageName);
  18. let il2CppClass = il2cppApi.il2cpp_class_from_name(il2cppImage, spaceze, className);
  19. // log("il2cppClass:" + il2CppClass.namespaze() + " name:" + il2CppClass.name());
  20. let methodInfo = il2cppApi.il2cpp_class_get_method_from_name(il2CppClass, methodName, methodCount);
  21. if (il2cppBaseAddr === undefined) {
  22. il2cppBaseAddr = Process.findModuleByName(soName);
  23. }
  24. if (needLog){
  25. logColor("-------------------------------------start---------------------------------------------------------", LogColor.RED);
  26. }
  27. let addr;
  28. let number;
  29. let methodPointer;
  30. if (!methodInfo.isNull()) {
  31. methodPointer = methodInfo.getMethodPointer();
  32. number = methodPointer - il2cppBaseAddr.base;
  33. addr = "0x" + number.toString(16).toUpperCase();
  34. if (spaceze!==""){
  35. let key = getKey(Il2cppImageName,spaceze,className,methodName,methodCount);
  36. methodAddrMap.set(key,methodPointer);
  37. }
  38. } else {
  39. addr = "0x0";
  40. number=0;
  41. methodPointer=0;
  42. }
  43. if (!needLog){
  44. return methodPointer;
  45. }
  46. logColor(" Dll:"+Il2cppImageName,LogColor.C97);
  47. logColor(" class:"+spaceze+"."+il2CppClass.name(),LogColor.C97);
  48. logColor(" methodPointer offset in IDA:" + addr,LogColor.C97);
  49. let methodContent;
  50. //返回类型
  51. let returnType = methodInfo.getReturnType();
  52. let return_cls = il2cppApi.il2cpp_class_from_type(returnType);
  53. let name1 = return_cls.name();
  54. if (name1.indexOf("`") !== -1) {
  55. let split = name1.split("`");
  56. name1 = split[0];
  57. name1 = name1 + return_cls.getGenericName();
  58. }
  59. methodContent = name1 + " " + methodInfo.name() + "(";
  60. let paramCount = methodInfo.getParamCount();
  61. // log("paramCount:" + paramCount);
  62. if (paramCount > 0) {
  63. for (let i = 0; i < paramCount; i++) {
  64. let paramType = methodInfo.getParam(i);
  65. let paramCls = il2cppApi.il2cpp_class_from_type(paramType);
  66. let name = paramCls.name();
  67. //获取泛型
  68. if (name.indexOf("`") !== -1) {
  69. let split = name.split("`");
  70. name = split[0];
  71. name = name + paramCls.getGenericName();
  72. }
  73. methodContent = methodContent + name + " " + methodInfo.getParamName(i);
  74. if (i + 1 !== paramCount) {
  75. methodContent = methodContent + ", ";
  76. } else {
  77. methodContent = methodContent + ") { }\n";
  78. }
  79. }
  80. } else {
  81. methodContent = methodContent + "){ }\n";
  82. }
  83. logColor(" "+methodContent,LogColor.C97);
  84. logColor("-------------------------------------end----------------------------------------------------------", LogColor.RED);
  85. return methodPointer;
  86. },
  87. getTargetCppImage(name) {
  88. let cppImageArray = this.getCppImageArray();
  89. if (cppImageArray.length > 0) {
  90. for (let i = 0; i < cppImageArray.length; i++) {
  91. let Il2CppImage = cppImageArray[i];
  92. let nameNoExt = Il2CppImage.name();
  93. if (nameNoExt === name) {
  94. return Il2CppImage;
  95. }
  96. }
  97. }
  98. },
  99. getCppImageArray: function () {
  100. if (il2CppImageArray.length !== 0) {
  101. return il2CppImageArray;
  102. }
  103. let domain = il2cppApi.il2cpp_domain_get();
  104. let size_t = Memory.alloc(Process.pointerSize);
  105. //可能还没加载
  106. let assemblies = il2cppApi.il2cpp_domain_get_assemblies(domain, size_t);
  107. let assemblies_count = size_t.readInt();
  108. log("assemblies_count:" + assemblies_count);
  109. for (let i = 0; i < assemblies_count; i++) {
  110. let assembly = assemblies.add(Process.pointerSize * i).readPointer();
  111. let Il2CppImage = il2cppApi.il2cpp_assembly_get_image(assembly);
  112. // log(" name:" + Il2CppImage.name());
  113. il2CppImageArray.push(Il2CppImage);
  114. }
  115. return il2CppImageArray;
  116. }
  117. }
  118. function getCache(Il2cppImageName,spaceze,className,methodName,methodCount){
  119. let key = getKey(Il2cppImageName,spaceze,className,methodName,methodCount);
  120. let newVar = methodAddrMap.get(key);
  121. if (newVar===undefined || newVar===null){
  122. return undefined;
  123. }else {
  124. return newVar;
  125. }
  126. }
  127. function getKey(Il2cppImageName,spaceze,className,methodName,methodCount){
  128. return Il2cppImageName + "_" + spaceze + "_" + className + "_" + methodName + "_" + methodCount;
  129. }