Advertisement
builderboy256

Untitled

Feb 4th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.94 KB | None | 0 0
  1. local source = function()
  2.  
  3.  
  4. -- Copy/paste the script you'd like to encrypt between the lines below --
  5. -- The encrypted script will be printed to the output --
  6.  
  7.  
  8.  
  9. --------------------------------------------------------------------------
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. --------------------------------------------------------------------------
  18.  
  19.  
  20.  
  21. end
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28. -- by NotAshley
  29.  
  30. -- thanks to MrNicNac for the original encryption method, and and NecroBumpist for the VM
  31.  
  32. print("-------------------- TOP OF SCRIPT --------------------\n")
  33.  
  34. local encrypt = function(str)
  35. local Return = "local data = {"
  36. local LengthDiv = 120
  37. for i = 1, str:len() do
  38. if i == str:len() then
  39. Return = Return .. "'\\" .. str:sub(i,i):byte() .. "'"
  40. if Return:len() > LengthDiv then
  41. print(Return)
  42. Return = ""
  43. end
  44. else
  45. Return = Return .. "'\\" .. str:sub(i,i):byte() .. "',"
  46. if Return:len() > LengthDiv then
  47. print(Return)
  48. Return = ""
  49. end
  50. end
  51. end
  52. Return = Return .. "}\n"
  53. print(Return)
  54. end
  55.  
  56. encrypt(string.dump(source))
  57.  
  58. print ([[
  59. local lua_opcode_types = {
  60. "ABC", "ABx", "ABC", "ABC",
  61. "ABC", "ABx", "ABC", "ABx",
  62. "ABC", "ABC", "ABC", "ABC",
  63. "ABC", "ABC", "ABC", "ABC",
  64. "ABC", "ABC", "ABC", "ABC",
  65. "ABC", "ABC", "AsBx", "ABC",
  66. "ABC", "ABC", "ABC", "ABC",
  67. "ABC", "ABC", "ABC", "AsBx",
  68. "AsBx", "ABC", "ABC", "ABC",
  69. "ABx", "ABC",
  70. }
  71. local lua_opcode_names = {
  72. "MOVE", "LOADK", "LOADBOOL", "LOADNIL",
  73. "GETUPVAL", "GETGLOBAL", "GETTABLE", "SETGLOBAL",
  74. "SETUPVAL", "SETTABLE", "NEWTABLE", "SELF",
  75. "ADD", "SUB", "MUL", "DIV",
  76. "MOD", "POW", "UNM", "NOT",
  77. "LEN", "CONCAT", "JMP", "EQ",
  78. "LT", "LE", "TEST", "TESTSET",
  79. "CALL", "TAILCALL", "RETURN", "FORLOOP",
  80. "FORPREP", "TFORLOOP", "SETLIST", "CLOSE",
  81. "CLOSURE", "VARARG"
  82. };
  83. local function get_bits(input, n, n2)
  84. if n2 then
  85. local total = 0
  86. local digitn = 0
  87. for i = n, n2 do
  88. total = total + 2^digitn*get_bits(input, i)
  89. digitn = digitn + 1
  90. end
  91. return total
  92. else
  93. local pn = 2^(n-1)
  94. return (input % (pn + pn) >= pn) and 1 or 0
  95. end
  96. end
  97. local function decode_bytecode(bytecode)
  98. local index = 1
  99. local big_endian = false
  100. local int_size;
  101. local size_t;
  102. local get_int, get_size_t;
  103. local get_int8, get_int32, get_int64, get_float64, get_string;
  104. do
  105. function get_int8()
  106. local a = bytecode:byte(index, index);
  107. index = index + 1
  108. return a
  109. end
  110. function get_int32()
  111. local a, b, c, d = bytecode:byte(index, index + 3);
  112. index = index + 4;
  113. return d*16777216 + c*65536 + b*256 + a
  114. end
  115. function get_int64()
  116. local a = get_int32();
  117. local b = get_int32();
  118. return b*4294967296 + a;
  119. end
  120. function get_float64()
  121. local a = get_int32()
  122. local b = get_int32()
  123. return (-2*get_bits(b, 32)+1)*(2^(get_bits(b, 21, 31)-1023))*
  124. ((get_bits(b, 1, 20)*(2^32) + a)/(2^52)+1)
  125. end
  126. function get_string(len)
  127. local str;
  128. if len then
  129. str = bytecode:sub(index, index + len - 1);
  130. index = index + len;
  131. else
  132. len = get_size_t();
  133. if len == 0 then return; end
  134. str = bytecode:sub(index, index + len - 1);
  135. index = index + len;
  136. end
  137. return str;
  138. end
  139. end
  140. local function decode_chunk()
  141. local chunk;
  142. local instructions = {};
  143. local constants = {};
  144. local prototypes = {};
  145. local debug = {
  146. lines = {};
  147. };
  148. chunk = {
  149. instructions = instructions;
  150. constants = constants;
  151. prototypes = prototypes;
  152. debug = debug;
  153. };
  154. local num;
  155. chunk.name = get_string();
  156. chunk.first_line = get_int();
  157. chunk.last_line = get_int();
  158. if chunk.name then chunk.name = chunk.name:sub(1, -2); end
  159. chunk.upvalues = get_int8();
  160. chunk.arguments = get_int8();
  161. chunk.varg = get_int8();
  162. chunk.stack = get_int8();
  163. do
  164. num = get_int();
  165. for i = 1, num do
  166. local instruction = {
  167. };
  168. local data = get_int32();
  169. local opcode = get_bits(data, 1, 6);
  170. local type = lua_opcode_types[opcode + 1];
  171. instruction.opcode = opcode;
  172. instruction.type = type;
  173. instruction.A = get_bits(data, 7, 14);
  174. if type == "ABC" then
  175. instruction.B = get_bits(data, 24, 32);
  176. instruction.C = get_bits(data, 15, 23);
  177. elseif type == "ABx" then
  178. instruction.Bx = get_bits(data, 15, 32);
  179. elseif type == "AsBx" then
  180. instruction.sBx = get_bits(data, 15, 32) - 131071;
  181. end
  182. instructions[i] = instruction;
  183. end
  184. end
  185. do
  186. num = get_int();
  187. for i = 1, num do
  188. local constant = {
  189. };
  190. local type = get_int8();
  191. constant.type = type;
  192. if type == 1 then
  193. constant.data = (get_int8() ~= 0);
  194. elseif type == 3 then
  195. constant.data = get_float64();
  196. elseif type == 4 then
  197. constant.data = get_string():sub(1, -2);
  198. end
  199. constants[i-1] = constant;
  200. end
  201. end
  202. do
  203. num = get_int();
  204. for i = 1, num do
  205. prototypes[i-1] = decode_chunk();
  206. end
  207. end
  208. do
  209. local data = debug.lines
  210. num = get_int();
  211. for i = 1, num do
  212. data[i] = get_int32();
  213. end
  214. num = get_int();
  215. for i = 1, num do
  216. get_string():sub(1, -2);
  217. get_int32();
  218. get_int32();
  219. end
  220. num = get_int();
  221. for i = 1, num do
  222. get_string();
  223. end
  224. end
  225. return chunk;
  226. end
  227. do
  228. assert(get_string(4) == "\27Lua", "Lua bytecode expected.");
  229. assert(get_int8() == 0x51, "Only Lua 5.1 is supported.");
  230. get_int8();
  231. big_endian = (get_int8() == 0);
  232. int_size = get_int8();
  233. size_t = get_int8();
  234. if int_size == 4 then
  235. get_int = get_int32;
  236. elseif int_size == 8 then
  237. get_int = get_int64;
  238. else
  239. error("Unsupported bytecode target platform");
  240. end
  241. if size_t == 4 then
  242. get_size_t = get_int32;
  243. elseif size_t == 8 then
  244. get_size_t = get_int64;
  245. else
  246. error("Unsupported bytecode target platform");
  247. end
  248. assert(get_string(3) == "\4\8\0",
  249. "Unsupported bytecode target platform");
  250. end
  251. return decode_chunk();
  252. end
  253. local function handle_return(...)
  254. local c = select("#", ...)
  255. local t = {...}
  256. return c, t
  257. end
  258. local function create_wrapper(cache, upvalues)
  259. local instructions = cache.instructions;
  260. local constants = cache.constants;
  261. local prototypes = cache.prototypes;
  262.  
  263. local stack, top
  264. local environment
  265. local IP = 1;
  266. local vararg, vararg_size
  267.  
  268. local opcode_funcs = {
  269. [0] = function(instruction)
  270. stack[instruction.A] = stack[instruction.B];
  271. end,
  272. [1] = function(instruction)
  273. stack[instruction.A] = constants[instruction.Bx].data;
  274. end,
  275. [2] = function(instruction)
  276. stack[instruction.A] = instruction.B ~= 0
  277. if instruction.C ~= 0 then
  278. IP = IP + 1
  279. end
  280. end,
  281. [3] = function(instruction)
  282. local stack = stack
  283. for i = instruction.A, instruction.B do
  284. stack[i] = nil
  285. end
  286. end,
  287. [4] = function(instruction)
  288. stack[instruction.A] = upvalues[instruction.B]
  289. end,
  290. [5] = function(instruction)
  291. local key = constants[instruction.Bx].data;
  292. stack[instruction.A] = environment[key];
  293. end,
  294. [6] = function(instruction)
  295. local C = instruction.C
  296. local stack = stack
  297. C = C > 255 and constants[C-256].data or stack[C]
  298. stack[instruction.A] = stack[instruction.B][C];
  299. end,
  300. [7] = function(instruction)
  301. local key = constants[instruction.Bx].data;
  302. environment[key] = stack[instruction.A];
  303. end,
  304. [8] = function (instruction)
  305. upvalues[instruction.B] = stack[instruction.A]
  306. end,
  307. [9] = function (instruction)
  308. local B = instruction.B;
  309. local C = instruction.C;
  310. local stack, constants = stack, constants;
  311. B = B > 255 and constants[B-256].data or stack[B];
  312. C = C > 255 and constants[C-256].data or stack[C];
  313. stack[instruction.A][B] = C
  314. end,
  315. [10] = function (instruction)
  316. stack[instruction.A] = {}
  317. end,
  318. [11] = function (instruction)
  319. local A = instruction.A
  320. local B = instruction.B
  321. local C = instruction.C
  322. local stack = stack
  323. B = stack[B]
  324. C = C > 255 and constants[C-256].data or stack[C]
  325. stack[A+1] = B
  326. stack[A] = B[C]
  327. end,
  328. [12] = function(instruction)
  329. local B = instruction.B;
  330. local C = instruction.C;
  331. local stack, constants = stack, constants;
  332. B = B > 255 and constants[B-256].data or stack[B];
  333. C = C > 255 and constants[C-256].data or stack[C];
  334. stack[instruction.A] = B+C;
  335. end,
  336. [13] = function(instruction)
  337. local B = instruction.B;
  338. local C = instruction.C;
  339. local stack, constants = stack, constants;
  340. B = B > 255 and constants[B-256].data or stack[B];
  341. C = C > 255 and constants[C-256].data or stack[C];
  342. stack[instruction.A] = B - C;
  343. end,
  344. [14] = function(instruction)
  345. local B = instruction.B;
  346. local C = instruction.C;
  347. local stack, constants = stack, constants;
  348. B = B > 255 and constants[B-256].data or stack[B];
  349. C = C > 255 and constants[C-256].data or stack[C];
  350. stack[instruction.A] = B * C;
  351. end,
  352. [15] = function(instruction)
  353. local B = instruction.B;
  354. local C = instruction.C;
  355. local stack, constants = stack, constants;
  356. B = B > 255 and constants[B-256].data or stack[B];
  357. C = C > 255 and constants[C-256].data or stack[C];
  358. stack[instruction.A] = B / C;
  359. end,
  360. [16] = function(instruction)
  361. local B = instruction.B;
  362. local C = instruction.C;
  363. local stack, constants = stack, constants;
  364. B = B > 255 and constants[B-256].data or stack[B];
  365. C = C > 255 and constants[C-256].data or stack[C];
  366. stack[instruction.A] = B % C;
  367. end,
  368. [17] = function(instruction)
  369. local B = instruction.B;
  370. local C = instruction.C;
  371. local stack, constants = stack, constants;
  372. B = B > 255 and constants[B-256].data or stack[B];
  373. C = C > 255 and constants[C-256].data or stack[C];
  374. stack[instruction.A] = B ^ C;
  375. end,
  376. [18] = function(instruction)
  377. stack[instruction.A] = -stack[instruction.B]
  378. end,
  379. [19] = function(instruction)
  380. stack[instruction.A] = not stack[instruction.B]
  381. end,
  382. [20] = function(instruction)
  383. stack[instruction.A] = #stack[instruction.B]
  384. end,
  385. [21] = function(instruction)
  386. local B = instruction.B
  387. local result = stack[B]
  388. for i = B+1, instruction.C do
  389. result = result .. stack[i]
  390. end
  391. stack[instruction.A] = result
  392. end,
  393. [22] = function(instruction)
  394. IP = IP + instruction.sBx
  395. end,
  396. [23] = function(instruction)
  397. local A = instruction.A
  398. local B = instruction.B
  399. local C = instruction.C
  400. local stack, constants = stack, constants
  401. A = A ~= 0
  402. B = B > 255 and constants[B-256].data or stack[B]
  403. C = C > 255 and constants[C-256].data or stack[C]
  404. if (B == C) ~= A then
  405. IP = IP + 1
  406. end
  407. end,
  408. [24] = function(instruction)
  409. local A = instruction.A
  410. local B = instruction.B
  411. local C = instruction.C
  412. local stack, constants = stack, constants
  413. A = A ~= 0
  414. B = B > 255 and constants[B-256].data or stack[B]
  415. C = C > 255 and constants[C-256].data or stack[C]
  416. if (B < C) ~= A then
  417. IP = IP + 1
  418. end
  419. end,
  420. [25] = function(instruction)
  421. local A = instruction.A
  422. local B = instruction.B
  423. local C = instruction.C
  424. local stack, constants = stack, constants
  425. A = A ~= 0
  426. B = B > 255 and constants[B-256].data or stack[B]
  427. C = C > 255 and constants[C-256].data or stack[C]
  428. if (B <= C) ~= A then
  429. IP = IP + 1
  430. end
  431. end,
  432. [26] = function(instruction)
  433. if stack[instruction.A] == (instruction.C ~= 0) then
  434. IP = IP + 1
  435. end
  436. end,
  437. [27] = function(instruction)
  438. local stack = stack
  439. local B = stack[instruction.B]
  440. if B == (instruction.C ~= 0) then
  441. IP = IP + 1
  442. else
  443. stack[instruction.A] = B
  444. end
  445. end,
  446. [28] = function(instruction)
  447. local A = instruction.A;
  448. local B = instruction.B;
  449. local C = instruction.C;
  450. local stack = stack;
  451. local args, results;
  452. local limit, loop
  453. args = {};
  454. if B ~= 1 then
  455. if B ~= 0 then
  456. limit = A+B-1;
  457. else
  458. limit = top
  459. end
  460. loop = 0
  461. for i = A+1, limit do
  462. loop = loop + 1
  463. args[loop] = stack[i];
  464. end
  465. limit, results = handle_return(stack[A](unpack(args, 1, limit-A)))
  466. else
  467. limit, results = handle_return(stack[A]())
  468. end
  469. top = A - 1
  470. if C ~= 1 then
  471. if C ~= 0 then
  472. limit = A+C-2;
  473. else
  474. limit = limit+A
  475. end
  476. loop = 0;
  477. for i = A, limit do
  478. loop = loop + 1;
  479. stack[i] = results[loop];
  480. end
  481. end
  482. end,
  483. [29] = function (instruction)
  484. local A = instruction.A;
  485. local B = instruction.B;
  486. local C = instruction.C;
  487. local stack = stack;
  488. local args, results;
  489. local top, limit, loop = top
  490. args = {};
  491. if B ~= 1 then
  492. if B ~= 0 then
  493. limit = A+B-1;
  494. else
  495. limit = top
  496. end
  497. loop = 0
  498. for i = A+1, limit do
  499. loop = loop + 1
  500. args[#args+1] = stack[i];
  501. end
  502. results = {stack[A](unpack(args, 1, limit-A))};
  503. else
  504. results = {stack[A]()};
  505. end
  506. return true, results
  507. end,
  508. [30] = function(instruction)
  509. local A = instruction.A;
  510. local B = instruction.B;
  511. local stack = stack;
  512. local limit;
  513. local loop, output;
  514. if B == 1 then
  515. return true;
  516. end
  517. if B == 0 then
  518. limit = top
  519. else
  520. limit = A + B - 2;
  521. end
  522. output = {};
  523. local loop = 0
  524. for i = A, limit do
  525. loop = loop + 1
  526. output[loop] = stack[i];
  527. end
  528. return true, output;
  529. end,
  530. [31] = function(instruction)
  531. local A = instruction.A
  532. local stack = stack
  533. local step = stack[A+2]
  534. local index = stack[A] + step
  535. stack[A] = index
  536. if step > 0 then
  537. if index <= stack[A+1] then
  538. IP = IP + instruction.sBx
  539. stack[A+3] = index
  540. end
  541. else
  542. if index >= stack[A+1] then
  543. IP = IP + instruction.sBx
  544. stack[A+3] = index
  545. end
  546. end
  547. end,
  548. [32] = function(instruction)
  549. local A = instruction.A
  550. local stack = stack
  551. stack[A] = stack[A] - stack[A+2]
  552. IP = IP + instruction.sBx
  553. end,
  554. [33] = function(instruction)
  555. local A = instruction.A
  556. local B = instruction.B
  557. local C = instruction.C
  558. local stack = stack
  559. local offset = A+2
  560. local result = {stack[A](stack[A+1], stack[A+2])}
  561. for i = 1, C do
  562. stack[offset+i] = result[i]
  563. end
  564. if stack[A+3] ~= nil then
  565. stack[A+2] = stack[A+3]
  566. else
  567. IP = IP + 1
  568. end
  569. end,
  570. [34] = function(instruction)
  571. local A = instruction.A
  572. local B = instruction.B
  573. local C = instruction.C
  574. local stack = stack
  575. if C == 0 then
  576. error("NYI: extended SETLIST")
  577. else
  578. local offset = (C - 1) * 50
  579. local t = stack[A]
  580. if B == 0 then
  581. B = top
  582. end
  583. for i = 1, B do
  584. t[offset+i] = stack[A+i]
  585. end
  586. end
  587. end,
  588. [35] = function(instruction)
  589. io.stderr:write("NYI: CLOSE")
  590. io.stderr:flush()
  591. end,
  592. [36] = function(instruction)
  593. local proto = prototypes[instruction.Bx]
  594. local instructions = instructions
  595. local stack = stack
  596. local indices = {}
  597. local new_upvals = setmetatable({},
  598. {
  599. __index = function(t, k)
  600. local upval = indices[k]
  601. return upval.segment[upval.offset]
  602. end,
  603. __newindex = function(t, k, v)
  604. local upval = indices[k]
  605. upval.segment[upval.offset] = v
  606. end
  607. }
  608. )
  609. for i = 1, proto.upvalues do
  610. local movement = instructions[IP]
  611. if movement.opcode == 0 then
  612. indices[i-1] = {segment = stack, offset = movement.B}
  613. elseif instructions[IP].opcode == 4 then
  614. indices[i-1] = {segment = upvalues, offset = movement.B}
  615. end
  616. IP = IP + 1
  617. end
  618. local _, func = create_wrapper(proto, new_upvals)
  619. stack[instruction.A] = func
  620. end,
  621. [37] = function(instruction)
  622. local A = instruction.A
  623. local B = instruction.B
  624. local stack, vararg = stack, vararg
  625.  
  626. for i = A, A + (B > 0 and B - 1 or vararg_size) do
  627. stack[i] = vararg[i - A]
  628. end
  629. end,
  630. }
  631. local function loop()
  632. local instructions = instructions
  633. local instruction, a, b
  634. while true do
  635. instruction = instructions[IP];
  636. IP = IP + 1
  637. a, b = opcode_funcs[instruction.opcode](instruction);
  638. if a then
  639. return b;
  640. end
  641. end
  642. end
  643. local debugging = {
  644. get_stack = function()
  645. return stack;
  646. end;
  647. get_IP = function()
  648. return IP;
  649. end
  650. };
  651. local function func(...)
  652. local local_stack = {};
  653. local ghost_stack = {};
  654. top = -1
  655. stack = setmetatable(local_stack, {
  656. __index = ghost_stack;
  657. __newindex = function(t, k, v)
  658. if k > top and v then
  659. top = k
  660. end
  661. ghost_stack[k] = v
  662. end;
  663. })
  664. local args = {...};
  665. vararg = {}
  666. vararg_size = select("#", ...) - 1
  667. for i = 0, vararg_size do
  668. local_stack[i] = args[i+1];
  669. vararg[i] = args[i+1]
  670. end
  671. environment = getfenv();
  672. IP = 1;
  673. local thread = coroutine.create(loop)
  674. local a, b = coroutine.resume(thread)
  675. if a then
  676. if b then
  677. return unpack(b);
  678. end
  679. return;
  680. else
  681. local name = cache.name;
  682. local line = cache.debug.lines[IP];
  683. local err = b:gsub("(.-:)", "");
  684. local output = "";
  685. output = output .. (name and name .. ":" or "");
  686. output = output .. (line and line .. ":" or "");
  687. output = output .. b
  688. error(output, 0);
  689. end
  690. end
  691. return debugging, func;
  692. end
  693. local VM = {
  694. load_bytecode = function(bytecode)
  695. local cache = decode_bytecode(bytecode);
  696. local _, func = create_wrapper(cache);
  697. return func;
  698. end;
  699. utils = {
  700. decode_bytecode = decode_bytecode;
  701. create_wrapper = create_wrapper;
  702. debug_bytecode = function(bytecode)
  703. local cache = decode_bytecode(bytecode)
  704. return create_wrapper(cache);
  705. end;
  706. };
  707. }
  708. ]].."VM.load_bytecode(table.concat(data, [[]]))()")
  709.  
  710. print("\n-------------------- BOTTOM OF SCRIPT --------------------")
  711. print("\nYour script has been encrypted. The result is above.\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement