From 692ded373d6bf3882c2b98dbdc306eea62caaa76 Mon Sep 17 00:00:00 2001 From: Nir Date: Wed, 2 Oct 2019 19:56:42 +0300 Subject: [PATCH] Add Port Scanner Utility --- Port_Scanner/README.md | 8 ++++++++ Port_Scanner/port_scanner.py | 40 ++++++++++++++++++++++++++++++++++++ README.md | 1 + 3 files changed, 49 insertions(+) create mode 100644 Port_Scanner/README.md create mode 100644 Port_Scanner/port_scanner.py diff --git a/Port_Scanner/README.md b/Port_Scanner/README.md new file mode 100644 index 0000000..027a646 --- /dev/null +++ b/Port_Scanner/README.md @@ -0,0 +1,8 @@ +# Port Scanner +A simple tool that asynchronously scans a server's ports with python. It's pretty quick as it processes ports in batches of 256. + +## Required libraries +None, the only libraries that are used are built-ins. + +## Usage +Use "port_scanner.py", and enter the IP that you want to scan when prompted. \ No newline at end of file diff --git a/Port_Scanner/port_scanner.py b/Port_Scanner/port_scanner.py new file mode 100644 index 0000000..a6a828d --- /dev/null +++ b/Port_Scanner/port_scanner.py @@ -0,0 +1,40 @@ +import asyncio +from random import SystemRandom + +def run(tasks, *, loop=None): + """Run Asynchronous Tasks""" + if loop is None: + loop = asyncio.get_event_loop() + # waiting for all tasks + return loop.run_until_complete(asyncio.wait(tasks)) + +async def scanner(ip, port, loop=None): + fut = asyncio.open_connection(ip, port, loop=loop) + + try: + reader, writer = await asyncio.wait_for(fut, timeout=0.5) # This is where it is blocking? + print("{}:{} Connected".format(ip, port)) + except asyncio.TimeoutError: + pass + # handle connection refused and bunch of others + except Exception as exc: + print('Error {}:{} {}'.format(ip, port, exc)) + +def scan(ips, ports, randomize=False): + """Scan the ports""" + loop = asyncio.get_event_loop() + if randomize: + rdev = SystemRandom() + ips = rdev.shuffle(ips) + ports = rdev.shuffle(ports) + + # let's pass list of task, not only one + run([scanner(ip, port) for port in ports for ip in ips]) + + +ips = [input("IP to scan: ")] +STEP = 256 +for r in range(STEP+1, 65536, STEP): + # print(r) + ports = [str(r) for r in list(range(r-STEP, r))] + scan(ips, ports) \ No newline at end of file diff --git a/README.md b/README.md index 43dffa3..1d66c4c 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ So far, the following projects have been integrated to this repo: - [Colored B&W Image Converter](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Color_to_BW_Converter) by [Nitish Srivastava](https://github.com/nitish-iiitd) - [Gmail Mailing Script](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/mailing) by [mayank-kapur](https://github.com/kapurm17) - [Take Screenshot](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Take_screenshot) by [Moad Mohammed Elhebri](https://github.com/moadmmh) +- [Port Scanner](https://github.com/hastagAB/Awesome-Python-Scripts/tree/master/Port_Scanner) by [Plutoberth](https://github.com/Plutoberth) ## Contribuition Guidelines : - Make a Seperate Folder for the Script.