From 2be1683253fb4771b067ae4161379bdbeb35c6b7 Mon Sep 17 00:00:00 2001 From: Wilfried OLLIVIER Date: Mon, 17 Nov 2014 17:27:38 +0100 Subject: [PATCH] =?UTF-8?q?add=20solution=20for=20nbcontest,=20progra,=20n?= =?UTF-8?q?=C2=B02?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- newbiecontest/prog/calcul/calcul.lua | 116 +++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100755 newbiecontest/prog/calcul/calcul.lua diff --git a/newbiecontest/prog/calcul/calcul.lua b/newbiecontest/prog/calcul/calcul.lua new file mode 100755 index 0000000..24d11d5 --- /dev/null +++ b/newbiecontest/prog/calcul/calcul.lua @@ -0,0 +1,116 @@ +#!/usr/bin/env lua + +-- require --------------------------------------------------------------------- +local luasql = require "luasql.sqlite3" -- sqlite module +local http = require "socket.http" -- http module +local ltn12 = require("ltn12") -- ltn12 module used to convert a sink to a table + +-- functions ------------------------------------------------------------------- +-- Extract info about the cookie used for newbiecontest connection +function extract_cookie(db) + + -- cookie value + cursor = db:execute("SELECT value FROM moz_cookies WHERE host LIKE\ + '%newbiecontest.org%' AND name LIKE '%SMFCookie%'") + cookie_value = cursor:fetch(row) + cursor:close() + + -- cookie name + cursor = db:execute("SELECT name FROM moz_cookies WHERE host LIKE\ + '%newbiecontest.org%' AND name LIKE '%SMFCookie%'") + cookie_name = cursor:fetch(row) + cursor:close() + + return cookie_value, cookie_name + +end + +-- Extract number from a given url from newbiecontest +function extract_number(cookie_name, cookie_value, url) + + -- create the table + local t = {} + + -- add the SMF cookie to the header + local headers = { + ["Cookie"] = cookie_name .. "=" .. cookie_value; + } + + -- request + r = http.request{url = url, + headers = headers, + sink = ltn12.sink.table(t) -- information goes to the table t + } + + -- convert the answer to string + string_r = table.concat(t) + + -- extract the number + number = string.match(string_r, "%d+") + + return number + +end + +-- Do the sqrt(a) * b +function calcul(a, b) + + y = math.sqrt(a) * b + + return math.floor(y) + +end + +-- Give the response back to the server +function send_response(r, url) + + -- crate the table + local t = {} + + -- concatenate the url + url = url .. "?solution=" .. r + + -- add the SMF cookie to the header + local headers = { + ["Cookie"] = cookie_name .. "=" .. cookie_value; + } + + -- send the reply + r = http.request{url = url, + headers = headers, + sink = ltn12.sink.table(t) + } + + print(table.concat(t)) -- print the token + + return 0 + +end + +-- main ----------------------------------------------------------------------- +if table.maxn(arg) < 4 + then + print("Get. The. Fuck. Out") + print("[i] Usage : ./calcul.lua file_db url_1 url_2 url_3") + os.exit() +end + +-- sqlite connection +env = luasql.sqlite3() +conn = env:connect(arg[1]) + +cookie_value, cookie_name = extract_cookie(conn) + +a = extract_number(cookie_name, cookie_value, arg[2]) +b = extract_number(cookie_name, cookie_value, arg[3]) + +r = calcul(a, b) + +send_response(r, arg[4]) + +-- sqlite close +env:close() +conn:close() + +print("Done.") +