#!:~/anaconda3/bin python3
# -*- encoding: utf-8 -*-

# python 3.6.3 Hammer Dos Script v0.1
# by Jay-Z
# Only for legal purpose 
# Copyright (c) 1995-2017 Beyondsoft (BeiJing) Co.Ltd.
# All Rights reserved


import sys
import getopt
import requests
import random
import re
from SELog import *
from RemoteExploits import *

class SearchExploit(FunLog):
    """
        @Auth: Jay-Z
        @Desc: you can search infomation related to security exploit which is always updated by "https://www.exploit-db.com/" site
        @Method: 
                spiderScheduler(), this method controls the whole spider process, search what kinds of exploit of doc by your descision.
    """


    def __init__(self):
        self.logger = FunLog().fun()
        # search begin page as default 1
        self.beginPage = 1
        # search end page as default 1
        self.endPage = 1
        # search content as default "security"
        self.content = "security"
        # sertch type as defualt "1"
        self.type = "1" 

        #root url, any url object is of this sub-url in this project
        self.rootURL = "https://www.exploit-db.com"
        #overwire User-Agent
        self.userAgent = [
                    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
                    "Mozilla/6.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)",
                    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)",
                    "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)"
                ]

        #Construct header
        self.headers = {
                "User-Agent" : random.choice(self.userAgent)    
            }
        
        self.initialParams()    
                
        self.spiderScheduler()

    def initialParams(self):
        """
            Recive the values of inputing for initializing parameters
        """
        try:
            
            opts, args = getopt.getopt(sys.argv[1:], "hb:e:c:t:", ["BeginPage=", "EndPage=", "Content=", "Type="])

        except getopt.GetoptError:
            print("SearchExploit.py -b <BeginPage> -e <EndPage> -c <Content>")
            print("or: SearchExploit.py --BeginPage=<BeginPage> --EndPage=<EndPage> --Content=<Content>")
            print("or: SearchExploit.py --version --help ?")
            sys.exit(2)
        
        #Check if exist invalid element
        if len(args) > 0:
            print("SearchExploit.py -b <BeginPage> -e <EndPage> -c <Content> -t <Type>")
            print("or: SearchExploit.py --BeginPage=<BeginPage> --EndPage=<EndPage> --Content=<Content> --Type=<Type>")
            print("or: SearchExploit.py --version --help ?")
            sys.exit(1)
        else:
            for opt, arg in opts:
                if opt in ("-h", "--help"):
                    print(
"""
SearchExploit.py 
-b or --BeginPage <BeginPage> : Starting page as your spider, like, -b 1, -b 2, -b 3...
-e or --EndPage <EndPage> : Ending page as your spider, like -e 1, -e 2, -e 3...
-c or --Content <Content> : Security Information you'd love as your spider,like, -c SSL, -c bluetooth, -c DDos...
-t or --Type <Type> + Num: 
    1, Remote Exploits: This exploit category includes exploits for remote services or 
       applications, including client side exploits.
    
    2, Web Application Exploits: This exploit category includes exploits for web applications.
    
    3, Local & Privilege Escalation Exploits: This exploit category includes local exploits or 
       privilege escalation exploits.

    4, Denial of Service & Proof of Concept Exploits: This exploit category includes proof of concept 
       code or code that results in a denial of service or application crash.

    5, Exploit Shellcode Archive: This category includes archived shellcode.

    6, Archived Security Papers: Archived security papers and articles in various languages.
    like, -t 1, -t 2, -t 3...
"""
)
                    print("or: SearchExploit.py --BeginPage=<BeginPage> --EndPage=<EndPage> --Content=<Content> --Type=<Type>")
                    print("or: SearchExploit.py --version --help ?")
                    sys.exit(1)
                elif opt in ("-b", "--BeginPage"):
                    self.beginPage = arg
                elif opt in ("-e", "--EndPage"):
                    self.endPage = arg
                elif opt in ("-c", "--Content"):
                    self.content = arg
                elif opt in ("-t", "--Type"):
                    self.type = arg
    
    
    def spiderScheduler(self):
        """
            spider scheduler
        """
        self.logger.info("Initializing the parameters...")
         
        #initalize root page
        rootHTMLPage = requests.get(url=self.rootURL, headers=self.headers).text

        if "1" == self.type:
            #Get type url remoteExploits
            remoteExpURL = re.findall('<h2.*><a href="(.*?)">Remote\s+Exploits', rootHTMLPage, re.S)[0]
            #contruct type of security remote exploits url
            remoteTypeURL = self.rootURL + remoteExpURL
            #call remoteExploit() and deal with the content page
            RemoteExploits().startWork(self.beginPage, self.endPage, self.content, remoteTypeURL, self.headers)

        elif "2" == self.type:
            self.webAppURL = re.findall('<h2.*><a href="(.*)">Web', rootHTMLPage, re.S)[0]
        elif "3" == self.type:
            self.localPeeURL = re.findall('<h2.*><a href="(.*)">Local &', rootHTMLPage, re.S)[0]
            print(self.localPeeURL)
        elif "4" == self.type:
            self.dosURL = re.findall('<h2.*><a href="(.*)">Denial of', rootHTMLPage, re.S)[0]
            print(self.dosURL)
        elif "5" == self.type:
            self.exploitURL = re.findall('<h2.*><a href="(.*)">Exploit', rootHTMLPage, re.S)[0]
            print(self.exploitURL)
        elif "6" == self.type:
            self.ArchivedURL = re.findall('<h2.*><a href="(.*)">Archived Security', rootHTMLPage, re.S)[0]
            print(self.ArchivedURL)
            

if __name__ == "__main__":
    SearchExploit()
