add solution for nbcontest, progra, n°7

This commit is contained in:
Wilfried OLLIVIER 2014-11-24 13:23:42 +01:00
parent 8323c13b24
commit d672a4f41b
1 changed files with 191 additions and 0 deletions

View File

@ -0,0 +1,191 @@
#!/usr/bin/env python
# imports ######################################################################
import sqlite3
import sys
import urllib.request
import re
# functions ####################################################################
# extract cookie to jar
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()
conn.close()
return cookie
except sqlite3.Error as e:
print("An error occurred:", e.args[0])
# send request using the cookie
def send_resquest(url, cookie):
opener = urllib.request.build_opener()
# add cookie to http header
opener.addheaders.append(('Cookie', cookie[0] + "=" + cookie[1]))
f = opener.open(url)
return f
# extract line containing the configs
def extract_config_line(f):
for line in f:
if re.findall(r'a un processeur', line.decode("utf-8")):
return line.decode("utf-8")
return None
# extract memory prices
def extract_memory_prices(f):
# create the hashtable
memory_hashtable = {}
# for each line
for line in f:
# find ram name
ram_name = re.findall(r'\d+ Mo [DS]DRAM', line.decode("utf-8"))
# save ram_name
if ram_name:
ram_name_cur = ram_name[0]
continue
# save ram_price
ram_price = re.findall(r'<td>(\d+)', line.decode("utf-8"))
# if ram_price is not None
if ram_price:
# add entry to memory_hashtable
memory_hashtable[ram_name_cur] = ram_price
# If all the prices are in the hashtable, just return it
if re.findall(r'Trop chère ;\)', line.decode("utf-8")):
return memory_hashtable
return memory_hash_table;
# extract cpu price
def extract_cpu_prices(f):
# create the cpu_hashtable
cpu_hashtable = {}
# for earch line
for line in f:
# find cpu name
cpu_name = re.findall(r'\d+.\d+ [MG]Hz', line.decode("utf-8"))
# save cpu name to cpu_name_cur
if cpu_name:
cpu_name_cur = cpu_name[0]
continue
# find price
cpu_price = re.findall(r'<td>(\d+)', line.decode("utf-8"))
# if cpu_price is not None
if cpu_price:
# add an entry to the hashtable
cpu_hashtable[cpu_name_cur] = cpu_price
# return the hashtable
return cpu_hashtable
# calculate config prices
def config_price_calc(line, memory_hashtable, cpu_hashtable):
# crete the hashtable containing the configs
configs = {}
i = 0
# extract the config for each people
config_array = re.findall(r'(\w+) a un processeur de (\d+.\d+ [MG]Hz) et dispose de (\d+ Mo [DS]DRAM)', line)
# for all the configs, determine the prices
for item in config_array:
try:
config_price = int(memory_hashtable[item[2]][0]) +\
int(cpu_hashtable[item[1]][0])
configs[i] = [item[0], config_price]
i += 1
except Exception as e:
raise e
# return the hashtable
return configs
# find the most expensive config
def find_most_expensive_config(configs):
i = 0
# for all the configs
for index in configs:
# init
if i == 0:
name = configs[index][0]
price = configs[index][1]
id = i
i += 1
# if a new max if found
if price < configs[index][1]:
# change the max value, to the current value in the loop
name = configs[index][0]
price = configs[index][1]
id = i
return name, price
def send_response(url, cookie, name, price):
opener = urllib.request.build_opener()
opener.addheaders.append(('Cookie', cookie[0] + "=" + cookie[1]))
f = opener.open(url + "?prenom=" + name + "&prix=" + str(price))
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_in url_out")
print("Get. The. Fuck. Out")
# extract related cookies from db
cookie = extract_cookies_to_jar(sys.argv[1])
# send request, get the challenge page
f = send_resquest(sys.argv[2], cookie)
line = extract_config_line(f)
if line == None:
os.exit(0)
memory_hashtable = extract_memory_prices(f)
cpu_hashtable = extract_cpu_prices(f)
configs = config_price_calc(line, memory_hashtable, cpu_hashtable)
name, price = find_most_expensive_config(configs)
send_response(sys.argv[3], cookie, name, price)
print("Done.")