add solution for nbcontest, programation, n°1
This commit is contained in:
commit
c67ea93d98
1 changed files with 63 additions and 0 deletions
63
newbiecontest/prog/renvoi/renvoi.py
Executable file
63
newbiecontest/prog/renvoi/renvoi.py
Executable file
|
@ -0,0 +1,63 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
# imports ######################################################################
|
||||
import sqlite3
|
||||
import sys
|
||||
import urllib.request
|
||||
import re
|
||||
|
||||
# functions ####################################################################
|
||||
def extract_cookies_to_jar(cookie_db):
|
||||
try:
|
||||
|
||||
# Connection
|
||||
conn = sqlite3.connect(cookie_db)
|
||||
|
||||
# Execute SELECT query
|
||||
cur = conn.cursor()
|
||||
r = cur.execute("SELECT name, value FROM moz_cookies WHERE host LIKE '%newbiecontest.org%' AND name LIKE '%SMFCookie%'")
|
||||
|
||||
cookie = r.fetchone()
|
||||
|
||||
return cookie
|
||||
|
||||
except sqlite3.Error as e:
|
||||
print("An error occurred:", e.args[0])
|
||||
|
||||
def send_resquest(url, cookie):
|
||||
opener = urllib.request.build_opener()
|
||||
opener.addheaders.append(('Cookie', cookie[0] + "=" + cookie[1]))
|
||||
|
||||
f = opener.open(url)
|
||||
|
||||
number = re.findall(r'\d+', f.read().decode('utf-8'))
|
||||
|
||||
return number
|
||||
|
||||
def send_number(url, number):
|
||||
opener = urllib.request.build_opener()
|
||||
opener.addheaders.append(('Cookie', cookie[0] + "=" + cookie[1]))
|
||||
|
||||
f = opener.open(url + "?solution=" + number[0])
|
||||
|
||||
print(f.read().decode('utf-8'))
|
||||
|
||||
# main #########################################################################
|
||||
if __name__ == '__main__':
|
||||
|
||||
# if arguments are missing
|
||||
if len(sys.argv) != 4:
|
||||
print("[i] Usage : cookie.db url")
|
||||
print("Get. The. Fuck. Out")
|
||||
|
||||
# extract related cookies from db
|
||||
cookie = extract_cookies_to_jar(sys.argv[1])
|
||||
|
||||
# send request, get the number
|
||||
number = send_resquest(sys.argv[2], cookie)
|
||||
|
||||
# send the number back
|
||||
send_number(sys.argv[3], number)
|
||||
|
||||
print("Done.")
|
||||
|
Loading…
Reference in a new issue