From 16869adc198584fd208439ec1855175ac92a17c8 Mon Sep 17 00:00:00 2001 From: Jueab Shaikh Date: Tue, 10 Oct 2023 00:23:54 +0530 Subject: [PATCH 1/2] added a Fully working Network Scanner --- EthicalHackingScripts/Network Scanner/main.py | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 EthicalHackingScripts/Network Scanner/main.py diff --git a/EthicalHackingScripts/Network Scanner/main.py b/EthicalHackingScripts/Network Scanner/main.py new file mode 100644 index 000000000..6c2996e73 --- /dev/null +++ b/EthicalHackingScripts/Network Scanner/main.py @@ -0,0 +1,108 @@ +import argparse +from scapy.all import * + + +def arp_scan(ip): + """ + Performs a network scan by sending ARP requests to an IP address or a range of IP addresses. + + Args: + ip (str): An IP address or IP address range to scan. For example: + - 192.168.1.1 to scan a single IP address + - 192.168.1.1/24 to scan a range of IP addresses. + + Returns: + A list of dictionaries mapping IP addresses to MAC addresses. For example: + [ + {'IP': '192.168.2.1', 'MAC': 'c4:93:d9:8b:3e:5a'} + ] + """ + request = Ether(dst="ff:ff:ff:ff:ff:ff") / ARP(pdst=ip) + + ans, unans = srp(request, timeout=2, retry=1) + result = [] + + for sent, received in ans: + result.append({'IP': received.psrc, 'MAC': received.hwsrc}) + + return result + + +def tcp_scan(ip, ports): + """ + Performs a TCP scan by sending SYN packets to . + + Args: + ip (str): An IP address or hostname to target. + ports (list or tuple of int): A list or tuple of ports to scan. + + Returns: + A list of ports that are open. + """ + try: + syn = IP(dst=ip) / TCP(dport=ports, flags="S") + except socket.gaierror: + raise ValueError('Hostname {} could not be resolved.'.format(ip)) + + ans, unans = sr(syn, timeout=2, retry=1) + result = [] + + for sent, received in ans: + if received[TCP].flags == "SA": + result.append(received[TCP].sport) + + return result + + +def main(): + parser = argparse.ArgumentParser() + subparsers = parser.add_subparsers( + dest="command", help="Command to perform.", required=True + ) + + arp_subparser = subparsers.add_parser( + 'ARP', help='Perform a network scan using ARP requests.' + ) + arp_subparser.add_argument( + 'IP', help='An IP address (e.g. 192.168.1.1) or address range (e.g. 192.168.1.1/24) to scan.' + ) + + tcp_subparser = subparsers.add_parser( + 'TCP', help='Perform a TCP scan using SYN packets.' + ) + tcp_subparser.add_argument('IP', help='An IP address or hostname to target.') + tcp_subparser.add_argument( + 'ports', nargs='+', type=int, + help='Ports to scan, delimited by spaces. When --range is specified, scan a range of ports. Otherwise, scan individual ports.' + ) + tcp_subparser.add_argument( + '--range', action='store_true', + help='Specify a range of ports. When this option is specified, should be given as .' + ) + + args = parser.parse_args() + + if args.command == 'ARP': + result = arp_scan(args.IP) + + for mapping in result: + print('{} ==> {}'.format(mapping['IP'], mapping['MAC'])) + + elif args.command == 'TCP': + if args.range: + ports = tuple(args.ports) + else: + ports = args.ports + + try: + result = tcp_scan(args.IP, ports) + except ValueError as error: + print(error) + exit(1) + + for port in result: + print('Port {} is open.'.format(port)) + + +if __name__ == '__main__': + main() \ No newline at end of file From 99f43cc14cb9663a9adb9791c62420114e249a87 Mon Sep 17 00:00:00 2001 From: Jueab Shaikh Date: Tue, 10 Oct 2023 00:28:09 +0530 Subject: [PATCH 2/2] added a Fully working Network Scanner 2 --- .../{Network Scanner => Network_scanner2}/main.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename EthicalHackingScripts/{Network Scanner => Network_scanner2}/main.py (100%) diff --git a/EthicalHackingScripts/Network Scanner/main.py b/EthicalHackingScripts/Network_scanner2/main.py similarity index 100% rename from EthicalHackingScripts/Network Scanner/main.py rename to EthicalHackingScripts/Network_scanner2/main.py