Advertisement
RedouaneRed1

script.lua

Jan 2nd, 2019
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.98 KB | None | 0 0
  1. -- This is a challenge, the goal is to read arbitary files on the machine running this script
  2.  
  3. local dangerous_functions = { 'io.open', 'io.popen', 'io.input', 'io.lines', 'io.write', 'io.output', 'dofile', 'loadfile', 'loadstring', 'load', 'require', 'module', 'os.execute'};
  4.  
  5. -- remove every dangerous function from the global environment :D
  6. for k,v in pairs(dangerous_functions) do
  7.     _G[v] = nil;
  8. end
  9.  
  10. account = { };
  11.  
  12. function account.greet()
  13.     print"Greetings";
  14. end
  15.  
  16. local function print_menu()
  17.     print'\n\nAccount Management Service';
  18.     print'You are free to do whatever you want with your object';
  19.     print'[0] display current account\n[1] add a key-value pair to the account\n[2] display attribute / call method on the current account';
  20. end
  21.  
  22. local function inspect_account()
  23.     print'Account properties:'
  24.     for k,v in pairs(account) do
  25.         print('\t' .. k .. ' : ' .. type(v));
  26.     end
  27. end
  28.  
  29. local function parse_value(value)
  30.     if value:find'^%d+$' then
  31.         return tonumber(value); -- its a number
  32.     elseif value:find"^'[^']+'$" or value:find'^"[^"]+"$' then
  33.         return value:sub(2, -2); -- its a string, what is between the quotes
  34.     else
  35.         return _G[value]; -- return the value of the global variable that has the name value
  36.     end
  37. end
  38.  
  39. local function add_entry_to_account()
  40.     print'Enter the name of the key you want to add to your account';
  41.     key_name = io.read'l';
  42.     print'Enter the value you want to associate with that key';
  43.     print'It can be an integer, a string, or the name of a global variable';
  44.     value = parse_value(io.read'l');
  45.     account[key_name] = value;
  46. end
  47.  
  48. local function access_entry_from_account()
  49.     inspect_account();
  50.     print'Enter the name of the attribute that you want to read, or the method that you want to call';
  51.     local key_name = io.read'l';
  52.     if type(account[key_name]) == 'function' then
  53.         local arguments = { };
  54.         -- read the arguments from the user, they can be strings, numbers, or references to global variables!
  55.         while true do
  56.             print'Please specify the arguments, enter an empty line to stop entering arguments';
  57.             local argum = io.read'l';
  58.             if argum == '' then break;
  59.             end
  60.             arguments[#arguments+1] = parse_value(argum);
  61.         end
  62.         account[key_name](table.unpack(arguments)); -- call the function and pass it the given arguments, no dangerous functions can be called Haha!
  63.     else
  64.         if type(account[key_name]) == 'string' or type(account[key_name]) == 'number' then
  65.             print('The value associated with the key ' .. key_name .. ' is ' .. account[key_name]);
  66.         else
  67.             -- the value is not string and not number, just print its type
  68.             print('The value associated with the key ' .. key_name .. ' is a ' .. type(account[key_name]) .. ' object');
  69.         end
  70.     end
  71. end
  72.  
  73. while true do
  74.     print_menu();
  75.     local choice = tonumber(io.read'l');
  76.     if choice == 0 then
  77.         inspect_account();
  78.     elseif choice == 1 then
  79.         add_entry_to_account();
  80.     elseif choice == 2 then
  81.         access_entry_from_account();
  82.     else
  83.         print'Unexpected input, enter a number between 0 and 2';
  84.     end
  85. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement