Skip to content

Commit

Permalink
Initial Commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
ab6459 committed Aug 2, 2022
0 parents commit 65af5cb
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea/
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Student Instructions

This project is a tool for scanning open (or closed) ports for a given IP address. In its current state, it is
incomplete, and you are tasked with aiding in the completion of the project.

In order for you to complete this project, you will need to learn some new things along the way. Most notably, you will
need to know the following:

- Basic Python
- Virtual Environments
- PyDoc
- PyTest
- Basic Linux Command Line Input (CLI)

## Instructions

For a detailed overview on what is expected to be undertaken for this project, please follow the instructions at the
following URL:

- [Project: Port Scanner](https://github.coventry.ac.uk/pages/CUEH/4061CEM/labs/projects/port_scanner/)
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
wheel
colored
pytest
pdoc3
30 changes: 30 additions & 0 deletions src/scanner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!python
"""Simple program for scanning ports on a given host and acting on results"""

from sockets import test_port
import colored

if __name__ == "__main__":
target = "172.17.0.2"

for p in range(1, 100):

message = colored.fg("red") + "Closed"

result = test_port(target, p)

if not result is None:

message = colored.fg("green") + "Open"

if len(result) > 0:
message += colored.fg("yellow")
message += " - Data received"

for i in result:
message += "\n" + i

message += "\n"

message += colored.attr('reset')
print(f"{p}: {message}")
56 changes: 56 additions & 0 deletions src/sockets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import socket
import select
import time


def check_status(sock):
""" Return the number of bytes ready to be read from a socket

Args:
sock (socket): the socket to test for data

Returns:
int: the number of bytes ready to be read.
"""
ready_to_read, ready_to_write, in_error = select.select([sock, ], [sock, ], [], 5)
return len(ready_to_read)


def test_port(host: str, portnum: int, poke=None):
"""Given a host (IP or name) and a port number, connect if possible and return any data transmitted.

Args:
host (string): the host to scan. Can be an IP address or hostname
portnum (int): the port number, between 0 and 65535
poke (string): if given, the string to send to the server upon connection.

Returns:
list of strings or None: the data returned by the connection, or None if the connection failed. If a list is returned, it represents the sequence of responses. The first element is the reponse recieved immediately, the second is the response after sending any given data.
"""
response = []

try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((host, portnum))
if result == 0: # 0 means no error
time.sleep(0.1) # Give the server time to send
if check_status(sock) > 0:
rcv = sock.recv(1024)
response.append(rcv.decode("utf-8", "ignore"))
if poke != None:
sock.sendall(str.encode(poke))
time.sleep(1) # Give the server time to send
ready_to_read, ready_to_write, in_error = select.select([sock, ], [sock, ], [], 5)
if check_status(sock) > 0:
rcv = sock.recv(1024)
response.append(rcv.decode("utf-8", "ignore"))

sock.shutdown(socket.SHUT_RDWR)
sock.close()

return response
else:
return None
except socket.error:
return None if response is [] else response
return None
33 changes: 33 additions & 0 deletions src/web_scan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!python
"""A simple scanner for HTTP servers"""

from sockets import test_port
import colored

if __name__ == "__main__":
target = "172.17.0.2"

# Ckeck a range of ports
maxPort = 85
minPort = 75

# How many characters to display from the response
maxResponse = 500
for p in range(minPort, maxPort):
message = "Closed"

result = test_port(target, p, poke="GET /index.html HTTP/1.0\r\n\r\n")
if not result is None:
message = "Open"
if len(result) > 0:
message += f": (response follows)\n"
response = ""
for i in result:
response += colored.fg("green") + i + "\n"
if len(response) > maxResponse:
# Trim it down if it's too long
response = response[:maxResponse]
response += colored.attr("reset")
message += response

print(f"{p}: {message}")

0 comments on commit 65af5cb

Please sign in to comment.