Post: [TUTORIAL] Building A Descent Port Scanner In Python!
05-15-2015, 05:25 AM #1
(adsbygoogle = window.adsbygoogle || []).push({}); Hello, tonight I decided to make a tutorial on building a functional port scanner with the Python programming language. Let's get started...

Q&A

Q: What is a port scanner and what does it do?
A: A port scanner is a software application designed to probe a server or host for open ports. This is often used by administrators to verify security policies of their networks and by attackers to identify running services on a host with the view to compromise it. A port scan or portscan can be defined as a process that sends client requests to a range of server port addresses on a host, with the goal of finding an active port. While not a nefarious process in and of itself, it is one used by hackers to probe target machine services with the aim of exploiting a known vulnerability of that service.

Q: What is Python?
A: Leave...now....


Python 2.7.6 (2.7.x)

Required Modules:
    import os
import sys
from socket import *


Color Functions (makes the script look more pro):
    W = '\033[0m'  # white (normal)
R = '\033[31m' # red
G = '\033[32m' # green
O = '\033[33m' # orange
B = '\033[34m' # blue
P = '\033[35m' # purple
C = '\033[36m' # cyan
GR = '\033[37m' # gray


Main Function:
    if __name__ == '__main__':

#used to clear terminal (Cmd Prompt) screen of previous uses
os.system('clear'Winky Winky
#os.system('cls'Winky Winky if you're on windows

#style or whatever
print 40 * '-'
print P + 'My Port Scanner ' + W + '//' + B + ' Made By XXXXX'
print W + 40 * '-'
target = raw_input('Target Domain/IP: 'Winky Winky
p_start=input("Start At Port: ")
p_stop=input("Stop At Port: ")

#resolves host name into ip
targetIP = gethostbyname(target)

print 'Starting scan on' + O, targetIP
print W + 40 * '-'


Scan Function:
    
for i in range(p_start, p_stop):
s = socket(AF_INET, SOCK_STREAM)

#connects to the ip of the resoved host name
result = s.connect_ex((targetIP, i))

#0 is the level code for 'true' or in this case 'open'
if(result == 0) :
print G + 'Port %d: OPEN' % (i,) + W

#else will occur if the result comes back with the level code 1 or 'false'
else:
print R + 'Port %d: CLOSED' % (i,) + W
s.close()


Doesn't really seem like a tutorial, more of showing you how it works, but still I hope this helps. If you get a traceback error then please notify me so I can review the code. Also if you have improvements of this code, then please share!
Last edited by Jettt ; 05-17-2015 at 09:05 AM.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo