2011-07-15, 21:32
  #1
Medlem
Tja. Har precis greppat python och gjorde mitt frsta exe program idag. Och nu undrar jag om ngon har en kod fr en simpel portscanner som ngon vill dela med sig av. Nr jag frskt med olika stt s har den anntingen inte startat scanningen eller bara avslutat nr den startat. S om ngon vill ge mig en fungerande kos s skulle det vara underbart!
Citera
2011-07-17, 17:44
  #2
Medlem
Trevligt med nnu en Pythonare!

Kodade en simpel, trdad portscanner i utbildningssyfte. ven om den r tmligen lngsam s rekommenderar jag dig att inte gra ngot dumt.

Kort info om programmet:
Scannar av portar mellan start_port och end_port genom att anvnda sig av trdar. P s stt kan den kolla flera portar samtidigt. Nr processen r klar kommer den att skriva ut bde i terminalen samt skapa en fil test.

Ls och lr!
Kod:
import sys
import socket
import threading
import time

PORTSTATUS_OPEN = 1
PORTSTATUS_CLOSED = 0

class PortScan(threading.Thread):
	def __init__(self, host, port):
		threading.Thread.__init__(self)
		
		self.host = host
		self.port = port

		self.port_status = PORTSTATUS_CLOSED

	def run(self):

		# We have to use reliable data transfer (RDT) which exists in SOCK_STREAM (TCP)
		# so we can be notified if connection failed.
		s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	
		# The socket has 10 seconds to figure out if port is open or not.
		# Port is open if a connection could be established.
		s.settimeout(10)
		try:
			t = s.connect((self.host, self.port))
		except:
			pass
		else:
			self.port_status = PORTSTATUS_OPEN
			s.close()

host = "www.aftonbladet.se"
start_port  = 25
end_port = 5500
threads = []
max_thread_count = 300

for port in xrange(start_port, end_port + 1):

	# Never have more than 300 threads active at same time...
	while threading.activeCount() > max_thread_count:
		
		sys.stdout.write("Too many threads active. Waiting for threads to terminate... ")
	
		sys.stdout.flush()
		for t in threads:
			t.join()

		sys.stdout.write("[done]\n")
		sys.stdout.flush()
		
	
	portscan_thread = PortScan(host, port)
	threads.append(portscan_thread)

	try: 
	
		# thread.start() calls the run() method in PortScan instance
		portscan_thread.start()
	except:
		# Should not happen.... If this error occurs try lowering max_thread_count
		print "Unexpected thread error: start(). [port ", port, "]"
	
	# For every 1000th port we have processed, print the port number 
	# in order to overview progress
	if port % 1000 == 0:
		print port

# Make sure all threads have been terminated 
# (complete with checking port status)
for t in threads:
	t.join()

# Open a file in which we will write all open ports
# Let's also make the terminal happy by printing them
output = open("test", "w")
output.write("Host: " + str(host) + "\n")
for t in threads:
	if t.port_status == PORTSTATUS_OPEN:
		print "Port", t.port , " is open"
		output.write(str(t.port) + "\n")
output.close()
Citera
2011-07-17, 19:49
  #3
Medlem
Citat:
Ursprungligen postat av Patron_
Trevligt med nnu en Pythonare!

Kodade en simpel, trdad portscanner i utbildningssyfte. ven om den r tmligen lngsam s rekommenderar jag dig att inte gra ngot dumt.

Kort info om programmet:
Scannar av portar mellan start_port och end_port genom att anvnda sig av trdar. P s stt kan den kolla flera portar samtidigt. Nr processen r klar kommer den att skriva ut bde i terminalen samt skapa en fil test.

Ls och lr!
Kod:
import sys
import socket
import threading
import time

PORTSTATUS_OPEN = 1
PORTSTATUS_CLOSED = 0

class PortScan(threading.Thread):
	def __init__(self, host, port):
		threading.Thread.__init__(self)
		
		self.host = host
		self.port = port

		self.port_status = PORTSTATUS_CLOSED

	def run(self):

		# We have to use reliable data transfer (RDT) which exists in SOCK_STREAM (TCP)
		# so we can be notified if connection failed.
		s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	
		# The socket has 10 seconds to figure out if port is open or not.
		# Port is open if a connection could be established.
		s.settimeout(10)
		try:
			t = s.connect((self.host, self.port))
		except:
			pass
		else:
			self.port_status = PORTSTATUS_OPEN
			s.close()

host = "www.aftonbladet.se"
start_port  = 25
end_port = 5500
threads = []
max_thread_count = 300

for port in xrange(start_port, end_port + 1):

	# Never have more than 300 threads active at same time...
	while threading.activeCount() > max_thread_count:
		
		sys.stdout.write("Too many threads active. Waiting for threads to terminate... ")
	
		sys.stdout.flush()
		for t in threads:
			t.join()

		sys.stdout.write("[done]\n")
		sys.stdout.flush()
		
	
	portscan_thread = PortScan(host, port)
	threads.append(portscan_thread)

	try: 
	
		# thread.start() calls the run() method in PortScan instance
		portscan_thread.start()
	except:
		# Should not happen.... If this error occurs try lowering max_thread_count
		print "Unexpected thread error: start(). [port ", port, "]"
	
	# For every 1000th port we have processed, print the port number 
	# in order to overview progress
	if port % 1000 == 0:
		print port

# Make sure all threads have been terminated 
# (complete with checking port status)
for t in threads:
	t.join()

# Open a file in which we will write all open ports
# Let's also make the terminal happy by printing them
output = open("test", "w")
output.write("Host: " + str(host) + "\n")
for t in threads:
	if t.port_status == PORTSTATUS_OPEN:
		print "Port", t.port , " is open"
		output.write(str(t.port) + "\n")
output.close()


Underbart! Python r ett vldigt underskattat sprk. Synd att det inte python forumet p fb r mer aktivt!
Citera

Skapa ett konto eller logga in för att kommentera

Du måste vara medlem för att kunna kommentera

Skapa ett konto

Det är enkelt att registrera ett nytt konto

Bli medlem

Logga in

Har du redan ett konto? Logga in här

Logga in