• XSS.stack #1 – первый литературный журнал от юзеров форума

[Script] Python port scanner using multi-threading.

Guest

Премиум
Пользователь
Регистрация
31.01.2023
Сообщения
250
Решения
1
Реакции
232
Гарант сделки
1
Python:
import socket
import threading

def scan(target, port):
    sockets = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sockets.settimeout(5)
    scan = sockets.connect_ex((target, port))
    if scan == 0:
        print(f"Port > {port} open")
    sockets.close()

def ports(target):
    threads = []
    for port in range(1, 65535):
        t = threading.Thread(target=scan, args=(target, port))
        threads.append(t)
        t.start()
    for t in threads:
        t.join()

target = input("Enter the target address: ")
ports(target)
 
Пожалуйста, обратите внимание, что пользователь заблокирован
this look like single ip port scanner, i think this script not support ip range like 192.168.1.1-192.168.1.255
and for port in range(1 to 65535) whats the point of extra ports?? not work good crash or stuck
 
this look like single ip port scanner, i think this script not support ip range like 192.168.1.1-192.168.1.255
and for port in range(1 to 65535) whats the point of extra ports?? not work good crash or stuck
Correct feel free to implement it i just wrote the script messing around.
 
Пожалуйста, обратите внимание, что пользователь заблокирован
Correct feel free to implement it i just wrote the script messing around.
oh sorry bro, if you code this , then i am sorry
i appreciate your work.
 
Пожалуйста, обратите внимание, что пользователь заблокирован
Here is a more accurate one in terms of scanning. i wrote after i hallucinated.​
Python:
import socket
import threading
import ipaddress

# function to check if an IP address is reachable
def is_reachable(ip):
    reachable = False
    try:
        # ping the IP address
        response = os.system("ping -c 1 " + ip)
        if response == 0:
            reachable = True
    except:
        pass
    return reachable

# function to scan a range of ports for a given IP address
def scan_ports(ip, ports, results):
    for port in ports:
        try:
            # create a socket object
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            # set a timeout for the connection attempt
            s.settimeout(0.5)
            # try to connect to the port
            result = s.connect_ex((ip, port))
            # if the connection attempt is successful, add the open port to the results list
            if result == 0:
                banner = s.recv(1024)
                results.append((ip, port, banner))
            # close the socket
            s.close()
        except:
            pass

# function to perform a brute force scan of a range of IP addresses and ports
def brute_force_scan():
    # prompt the user to input the IP range to scan
    while True:
        try:
            ip_start = input("Enter the starting IP address to scan (e.g. 192.168.0.1): ")
            ipaddress.IPv4Address(ip_start)
            ip_end = input("Enter the ending IP address to scan (e.g. 192.168.0.255): ")
            ipaddress.IPv4Address(ip_end)
            ip_range = (ip_start, ip_end)
            break
        except ValueError:
            print("Invalid IP address format. Please try again.")

    # prompt the user to input the port range to scan
    while True:
        try:
            port_start = int(input("Enter the starting port to scan: "))
            port_end = int(input("Enter the ending port to scan: "))
            if port_start <= port_end and 1 <= port_start <= 65535 and 1 <= port_end <= 65535:
                port_range = (port_start, port_end)
                break
            else:
                print("Invalid port range. Please try again.")
        exceptValueError:
            print("Invalid port number. Please try again.")

    # create a list to store the results of the scan
    results = []

    # loop through each IP address in the range
    for ip in ipaddress.summarize_address_range(ip_range[0], ip_range[1]):
        # convert the IP address to a string
        ip_str = str(ip)
        # check if the IP address is reachable
        if is_reachable(ip_str):
            # create a list to store the threads
            threads = []
            # loop through each port in the range
            for port in range(port_range[0], port_range[1]+1):
                # create a new thread to scan the port
                t = threading.Thread(target=scan_ports, args=(ip_str, [port], results))
                # add the thread to the list
                threads append(t)
                # start the thread
                t.start()
            # wait for all threads to finish
            for t in threads:
                t.join()
    # write the results to a file
    with open('results.txt', 'w') as f:
        for result in results:
            f.write(f"{result[0]}:{result[1]} ({result[2].decode('utf-8', 'ignore').strip()})\n")
            print(f"Open port found on {result[0]}:{result[1]} ({result[2].decode('utf-8', 'ignore').strip()})")[/ code]
 
Python:
import socket
import threading

def scan(target, port):
    sockets = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sockets.settimeout(5)
    scan = sockets.connect_ex((target, port))
    if scan == 0:
        print(f"Port > {port} open")
    sockets.close()

def ports(target):
    threads = []
    for port in range(1, 65535):
        t = threading.Thread(target=scan, args=(target, port))
        threads.append(t)
        t.start()
    for t in threads:
        t.join()

target = input("Enter the target address: ")
ports(target)
Слишком упрощённая версия, это больше пример для новичков чем полезный к изучению код.
Сделай получение банеров служб хотя бы, что бы как то разнообразить этот сканер.
 
Пожалуйста, обратите внимание, что пользователь заблокирован
An overly simplified version, this is more of an example for beginners than useful code to learn.
Get service banners at least to diversify this scanner.
you can update this script, ask chatgpt lol :cool: ;)
 
you can update this script, ask chatgpt lol :cool: ;)
chatgpt is not needed here, this is the basics of the python language, if you do not know elementary things, then even chatgpt will not help to make a normal scanner.
 


Напишите ответ...
  • Вставить:
Прикрепить файлы
Верх