Python port scan system implementation method

  • 2020-04-02 14:20:59
  • OfStack

This article illustrates the implementation of a python port scan system. Share with you for your reference. The specific implementation method is as follows:

The main functions of the program are as follows:

1. Obtain all external network IP segments from its own API interface;
2. Scan all IP segments with Nmap and generate XML scan report with -ox;
3. Read the XML file with the method of xml.etree.elementtree module, and write the IP, open port and corresponding service into the Mysql database.
The function is very simple, did not meet the big boss on the demand, so this small project is so heroic to die! ~~~ has not considered the abnormal termination of the program, scan the server abnormal shutdown.
Paste code:

#coding:utf-8 
import sys,os,time,subprocess
import MySQLdb
import re,urllib2
import ConfigParser
from IPy import IP
import xml.etree.ElementTree as ET
 
nowtime = time.strftime('%Y-%m-%d',time.localtime(time.time()))
configpath=r'c:portscanconfig.ini'
# The incoming api Interface main path , Iterate to get all of them ip The list, IPy Module format into 127.0.0.1/24 The format of the
def getiplist(ipinf):
serverarea=['tj101','tj103','dh','dx']
iplist=[]
for area in serverarea:
ipapi=urllib2.urlopen(ipinf+area).read()
for ip in ipapi.split('n'):
# Determine if the ip The list is not empty, convert to ip/ The gateway format , Reformat into ip/24 The format of the
if ip:
ip=ip.replace('_','/')
ip=(IP(ip))
iplist.append(str(ip))
ipscan(iplist,nmapathx)
 
# pass ip Address file and nmap The path
def ipscan(iplist,nmapath):
# Ancient deduplication, right ip In the file ip Address to be deduplicated
newiplist=[]
scaniplist=[]
for ip in iplist:
if ip not in newiplist:
newiplist.append(ip)
# Iterate over all ip Segment, batch scan, generation xml Report format
for ip in newiplist:
filename=nowtime+ip.split('/')[0]+'.xml'
filepath=r"c:portscanscanres"
nmapcmd=nmapath+' -PT '+ip.strip('rn')+' -oX '+filepath+filename
os.system(nmapcmd)
scaniplist.append(ip)
writeinmysql(scaniplist)
 
# Warehouse module is a great aunt to write good to me I just modified it, mainly xml.etree.ElementTree The module.
def writeinmysql(scaniplist):
filepath=r"c:portscanscanres"
for ip in scaniplist:
xmlfile=filepath+''+ip+'.xml'
# The indentation ha I changed it temporarily when I posted the article, so I'm too lazy to indent it
root=ET.parse(xmlfile).getroot()
allhost=root.findall('host')
conn=MySQLdb.connect(host='10.5.10.57',user='nxadmin',passwd='nxadmin.com',port=3306,db='scandb',charset='utf8')
cur= conn.cursor()
for host in allhost:
address = host.find('address')
# First determine whether the port is open Of, if is carries on the warehousing again
for port in host.find('ports').findall('port'):
if port.find('state').attrib['state']=="open":
ip=address.attrib['addr']
portval=port.attrib['portid']
state=port.find('state').attrib['state']
sql = "INSERT INTO portscan (ip,port,state) VALUES(%s,%s,%s)"
params=[ip,portval,state]
cur.execute(sql,params)
conn.commit()
cur.close()
conn.close()
if __name__=="__main__":
# Read the configuration file to be scanned IP apiurl and nmap Installation file path
config=ConfigParser.ConfigParser()
config.readfp(open(configpath,'rb'))
nmapathx=config.get('nmap','nmapath')
ipinf=config.get('ip','ipinf')
getiplist(ipinf)

Configuration file c:portscanconfig.ini is mainly the main API interface url, nmap installation path.

Interested friends can further refine the functionality of the example. I hope this article has helped you with your Python programming.


Related articles: