Advertisement
chuotconleader

Untitled

Aug 19th, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. static class Program
  2. {
  3. static byte[] lockkey;//自行从libtolua.so中取出
  4. static byte[] unlockkey;//自行从libtolua.so中取出
  5.  
  6. static void Main(string[] args)
  7. {
  8. var bytes = File.ReadAllBytes(args[0]);
  9. var reader = new BinaryReader(new MemoryStream(bytes));
  10. //_read_header
  11. var magic = reader.ReadBytes(3);
  12. var version = reader.ReadByte();
  13. var bits = reader.ReadUleb128();
  14. var is_stripped = ((bits & 2u) != 0u);
  15. if (!is_stripped)
  16. {
  17. var length = reader.ReadUleb128();
  18. var name = Encoding.UTF8.GetString(reader.ReadBytes((int)length));
  19. }
  20. //_read_prototypes
  21. while (reader.BaseStream.Position < reader.BaseStream.Length)
  22. {
  23. var size = reader.ReadUleb128();
  24. if (size == 0)
  25. break;
  26. var next = reader.BaseStream.Position + size;
  27. bits = reader.ReadByte();//_read_flags
  28. var arguments_count = reader.ReadByte();//_read_counts_and_sizes
  29. var framesize = reader.ReadByte();
  30. var upvalues_count = reader.ReadByte();
  31. var complex_constants_count = reader.ReadUleb128();
  32. var numeric_constants_count = reader.ReadUleb128();
  33. var instructions_count = reader.ReadUleb128();
  34. var start = (int)reader.BaseStream.Position;
  35. //加密
  36. /*bytes[3] = 0x80;
  37. bytes = lj_bclock(start, bytes, (int)instructions_count);*/
  38. //解密
  39. bytes[3] = 2;
  40. bytes = lj_bcunlock(start, bytes, (int)instructions_count);
  41. //
  42. reader.BaseStream.Position = next;
  43. }
  44. File.WriteAllBytes(args[0], bytes);
  45. }
  46.  
  47. static byte[] lj_bclock(int start, byte[] bytes, int count)
  48. {
  49. var result = start;
  50. result += 4;
  51. var v2 = 0;
  52. do
  53. {
  54. var v3 = bytes[result - 4];
  55. result += 4;
  56. var v4 = bytes[result - 7] ^ v2++;
  57. bytes[result - 8] = (byte)(lockkey[v3] ^ v4);
  58. }
  59. while (v2 != count);
  60. return bytes;
  61. }
  62.  
  63. static byte[] lj_bcunlock(int start, byte[] bytes, int count)
  64. {
  65. var result = start;
  66. result += 4;
  67. var v2 = 0;
  68. do
  69. {
  70. var v3 = bytes[result - 4];
  71. result += 4;
  72. var v4 = bytes[result - 7] ^ v3 ^ (v2++ & 0xFF);
  73. bytes[result - 8] = unlockkey[v4];
  74. }
  75. while (v2 != count);
  76. return bytes;
  77. }
  78.  
  79. static public uint ReadUleb128(this BinaryReader reader)
  80. {
  81. uint value = reader.ReadByte();
  82. if (value >= 0x80)
  83. {
  84. var bitshift = 0;
  85. value &= 0x7f;
  86. while (true)
  87. {
  88. var b = reader.ReadByte();
  89. bitshift += 7;
  90. value |= (uint)((b & 0x7f) << bitshift);
  91. if (b < 0x80)
  92. break;
  93. }
  94. }
  95. return value;
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement