Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static class Program
- {
- static byte[] lockkey;//自行从libtolua.so中取出
- static byte[] unlockkey;//自行从libtolua.so中取出
- static void Main(string[] args)
- {
- var bytes = File.ReadAllBytes(args[0]);
- var reader = new BinaryReader(new MemoryStream(bytes));
- //_read_header
- var magic = reader.ReadBytes(3);
- var version = reader.ReadByte();
- var bits = reader.ReadUleb128();
- var is_stripped = ((bits & 2u) != 0u);
- if (!is_stripped)
- {
- var length = reader.ReadUleb128();
- var name = Encoding.UTF8.GetString(reader.ReadBytes((int)length));
- }
- //_read_prototypes
- while (reader.BaseStream.Position < reader.BaseStream.Length)
- {
- var size = reader.ReadUleb128();
- if (size == 0)
- break;
- var next = reader.BaseStream.Position + size;
- bits = reader.ReadByte();//_read_flags
- var arguments_count = reader.ReadByte();//_read_counts_and_sizes
- var framesize = reader.ReadByte();
- var upvalues_count = reader.ReadByte();
- var complex_constants_count = reader.ReadUleb128();
- var numeric_constants_count = reader.ReadUleb128();
- var instructions_count = reader.ReadUleb128();
- var start = (int)reader.BaseStream.Position;
- //加密
- /*bytes[3] = 0x80;
- bytes = lj_bclock(start, bytes, (int)instructions_count);*/
- //解密
- bytes[3] = 2;
- bytes = lj_bcunlock(start, bytes, (int)instructions_count);
- //
- reader.BaseStream.Position = next;
- }
- File.WriteAllBytes(args[0], bytes);
- }
- static byte[] lj_bclock(int start, byte[] bytes, int count)
- {
- var result = start;
- result += 4;
- var v2 = 0;
- do
- {
- var v3 = bytes[result - 4];
- result += 4;
- var v4 = bytes[result - 7] ^ v2++;
- bytes[result - 8] = (byte)(lockkey[v3] ^ v4);
- }
- while (v2 != count);
- return bytes;
- }
- static byte[] lj_bcunlock(int start, byte[] bytes, int count)
- {
- var result = start;
- result += 4;
- var v2 = 0;
- do
- {
- var v3 = bytes[result - 4];
- result += 4;
- var v4 = bytes[result - 7] ^ v3 ^ (v2++ & 0xFF);
- bytes[result - 8] = unlockkey[v4];
- }
- while (v2 != count);
- return bytes;
- }
- static public uint ReadUleb128(this BinaryReader reader)
- {
- uint value = reader.ReadByte();
- if (value >= 0x80)
- {
- var bitshift = 0;
- value &= 0x7f;
- while (true)
- {
- var b = reader.ReadByte();
- bitshift += 7;
- value |= (uint)((b & 0x7f) << bitshift);
- if (b < 0x80)
- break;
- }
- }
- return value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement