current position:Home>The second stop of Python Programming - dictionary based directory scanning tool

The second stop of Python Programming - dictionary based directory scanning tool

2022-05-15 06:04:10Wandering mage 12

Catalog

Preface

One 、 Dictionary based directory resource cracking tool

1. Tool principle

2. Tool thinking

Two 、 Tool initialization

1.Banner Information functions

2. Use method information function

3、 ... and 、 Command line tool parameters get

1. Module introduction

2. Parameters get

3. encapsulation start function ( Parameters get the content )

Four 、 Dictionary file read

1.python Dictionary file read

2. Multithreading thinking

3. Scan function

5、 ... and 、 Multithreaded access

1.python Multithreading

6、 ... and 、 The complete code is as follows


Preface

         Beginners write tools !

         Learning only , Not for any other purpose !

One 、 Dictionary based directory resource cracking tool

1. Tool principle

         Similar to directory scanning tool ;

2. Tool thinking

as follows :

        1) Command line tool parameter acquisition

        2) Dictionary reading

        3) Multithreaded access

        4) The status code judges the output result

        5) Result analysis

Two 、 Tool initialization

1.Banner Information functions

        '''''''''

        def banner()

         Used to introduce tools and names

        '''''''''

# Banner Information functions 
def banner():
    print('*'*50)
    print('*'*2+' '*16+'DirBrute v1.0'+' '*17+'*'*2)
    print('*' * 50)

2. Use method information function

'''''''''

1)def usage():

2) Usage method

url:-u

thread:-t

dictionary:-d

'''''''''

# Usage Information 
def usage():
    print("This is the tool's usage")
    print('python  Dictionary based directory resource cracking tool .py -u url -t thread -d dictionary.txt')

3、 ... and 、 Command line tool parameters get

1. Module introduction

'''''''''

1)sys:sys.argv[n] obtain python Command execution data

2)getopt:python The built-in module for parsing command line parameters

'''''''''

2. Parameters get

'''''''''

opts,args = getopt.getopt(sys.argv[1:],'u:t:d:')

'''''''''

opts,args = getopt.getopt(sys.argv[1:],'u:t:d:')
print(opts)     # [('-u', 'www.baidu.com'), ('-t', '5'), ('-d', '.\\pass.txt')]
print(args)     # []

3. encapsulation start function ( Parameters get the content )

def start():
    if(len(sys.argv) == 7):
        # This is ture length
        opts, args = getopt.getopt(sys.argv[1:], 'u:t:d:')
        for k,v in opts:
            if k == '-u':
                url = v
            elif k == '-t':
                thread = v
            elif k == '-d':
                dic = v
        # print('url:'+url)
        # print('thread:'+thread)
        # print('dictionary:'+dic)
        multi_scan(url,thread,dic)
    else:
        print('Error Argument')
        sys.exit()

Four 、 Dictionary file read

1.python Dictionary file read

'''''''''

1)with open(filename,mode) as f

2)f.readlines()

'''''''''

with open(dic,'r') as f:
        dic_list = f.readlines()
        # print(dic_list)
        # print(len(dic_list))      # 16

2. Multithreading thinking

'''''''''

1) A thread reads a fixed number of dictionary files

2) Make a list of dictionaries used by multithreading ( Storage is in the form of a list )

'''''''''

#  Second parts , Determine the number of rows read 
        # len(dic_list)/thread
        if len(dic_list) % int(thread) == 0:
            thread_read_line_num = len(dic_list) / int(thread)
        else:
            thread_read_line_num = math.ceil(len(dic_list) / int(thread))     # math.ceil Rounding up , The result is 4; If you don't add this math.ceil, The result is 3.2, Not reality ;
        # print(thread_read_line_num)
        #  The third step is to make a list of dictionaries read by the thread  [[t1],[t2],[t3],[t4]]
        i = 0
        temp_list = [ ]
        for line in dic_list:
            i += 1
            if i % thread_read_line_num == 0:
                temp_list.append(line.strip())
                result_list.append(temp_list)
                temp_list = []
            else:
                temp_list.append(line.strip())

        # print(result_list)

3. Scan function

def scan(url,dic):
    #  Realize the scanning function 
    for line in dic:
        r = requests.get(url+'/'+line)
        if r.status_code == 200:
            print(r.url+' : '+str(r.status_code))

5、 ... and 、 Multithreaded access

'''''''''

1.python Multithreading

threading.Thread(target=,args=())

start()

#  Thread scanning 
for i in result_list:
   thread_list.append(threading.Thread(target=scan,args=(url,i)))
for t in thread_list:
   t.start()

6、 ... and 、 The complete code is as follows

# coding=utf8
# @time:2022/5/7 9:47
# Author  Haoyu 

#  Need to use python modular 
import sys,getopt,math,threading,requests

# ---------------------------------------------------------------
# python  Dictionary based directory resource cracking tool .py -u url -t thread -d dictionary.txt
# ---------------------------------------------------------------
# Banner Information functions 
def banner():
    print('*'*50)
    print('*'*2+' '*16+'DirBrute v1.0'+' '*17+'*'*2)
    print('*' * 50)


# Usage Information 
def usage():
    print("This is the tool's usage")
    print('python  Dictionary based directory resource cracking tool .py -u url -t thread -d dictionary.txt')


# opts,args = getopt.getopt(sys.argv[1:],'u:t:d:')
# print(opts)     # [('-u', 'www.baidu.com'), ('-t', '5'), ('-d', '.\\pass.txt')]
# print(args)     # []

#  encapsulation start function ( Parameters get the content )
def start():
    if(len(sys.argv) == 7):
        # This is ture length
        opts, args = getopt.getopt(sys.argv[1:], 'u:t:d:')
        for k,v in opts:
            if k == '-u':
                url = v
            elif k == '-t':
                thread = v
            elif k == '-d':
                dic = v
        # print('url:'+url)
        # print('thread:'+thread)
        # print('dictionary:'+dic)
        multi_scan(url,thread,dic)
    else:
        print('Error Argument')
        sys.exit()



#  Multithreading 
def multi_scan(url,thread,dic):
    result_list = []
    thread_list = []
    #  The first step is to read the dictionary file 
    with open(dic,'r') as f:
        dic_list = f.readlines()
        # print(dic_list)
        # print(len(dic_list))      # 16
        #  Second parts , Determine the number of rows read 
        # len(dic_list)/thread
        if len(dic_list) % int(thread) == 0:
            thread_read_line_num = len(dic_list) / int(thread)
        else:
            thread_read_line_num = math.ceil(len(dic_list) / int(thread))     # math.ceil Rounding up , The result is 4; If you don't add this math.ceil, The result is 3.2, Not reality ;
        # print(thread_read_line_num)
        #  The third step is to make a list of dictionaries read by the thread  [[t1],[t2],[t3],[t4]]
        i = 0
        temp_list = [ ]
        for line in dic_list:
            i += 1
            if i % thread_read_line_num == 0:
                temp_list.append(line.strip())
                result_list.append(temp_list)
                temp_list = []
            else:
                temp_list.append(line.strip())

        # print(result_list)
    #  Thread scanning 
    for i in result_list:
        thread_list.append(threading.Thread(target=scan,args=(url,i)))
    for t in thread_list:
        t.start()

def scan(url,dic):
    #  Realize the scanning function 
    for line in dic:
        r = requests.get(url+'/'+line)
        if r.status_code == 200:
            print(r.url+' : '+str(r.status_code))



banner()
usage()
start()

Effect display :

More secure sharing , Please pay attention to 【 Security info】 WeChat official account !

copyright notice
author[Wandering mage 12],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/131/202205110607423918.html

Random recommended