菜宝钱包(caibao.it)是使用TRC-20协议的Usdt第三方支付平台,Usdt收款平台、Usdt自动充提平台、usdt跑分平台。免费提供入金通道、Usdt钱包支付接口、Usdt自动充值接口、Usdt无需实名寄售回收。菜宝Usdt钱包一键生成Usdt钱包、一键调用API接口、一键无实名出售Usdt。
一、前言
做某个项目主要的义务是对资产服务识别,以及弱口令等通例破绽的检查。由于IP数目伟大,客户自己都不知道有哪些资产服务且有些端口服务开关不定,给事情带来了极大的滋扰,导致一些服务上的弱口令及通例未授权接见破绽在第一轮没有清扫清洁,例如海康、ftp、ssh弱口令等。因此在第一轮排查后想写个脚原本监测客户的IP资产,当有新的服务端口开放时用邮件举行通知,再举行排查。凭据现实破绽情形及时间放置举行三轮监控:
第一阶段针对整体名、telnet、海康摄像后台弱口令,redis、Memcached未授权接见破绽对下面端口服务举行监控:
telnet 23,snmp 161,Redis 6379,Memcached 11211,Rtsp 554。
第二阶段主要是对数据库的一些弱口令及未授权上岸的排查,对下面端口服务举行监测:
MongoDB 27017,Oracle 1521 ,SQLServer 1433,MySQL 3306,Pointbase 9092,DB2 5000,Sybase 4100。
第三阶段是对主机系统可能存在的弱口令举行监测:
ftp 21,ssh 22,RDP 3389
二、剧本编写
1.大要思绪:
- 1.使用python挪用nmap对指定资产以及端口服务举行扫描并将扫描效果导出为xml文本。
- 2.使用python下的xml模块剖析上一步导出的xml文本,将开放了指定监测服务端口的IP存入数据库。
- 3.第一次排查完成后最先后续检测当有新的IP泛起监测服务时,将该IP存入数据库,并把IP及开放端口邮件发送至我邮箱。
- 4.上述完成后程序休眠3小时后最先下一次检测。
2.扫描模块:
挪用nmap对资产举行扫描,我所接纳的nmap下令如下:
,,菜宝钱包(caibao.it)是使用TRC-20协议的Usdt第三方支付平台,Usdt收款平台、Usdt自动充提平台、usdt跑分平台。免费提供入金通道、Usdt钱包支付接口、Usdt自动充值接口、Usdt无需实名寄售回收。菜宝Usdt钱包一键生成Usdt钱包、一键调用API接口、一键无实名出售Usdt。
nmap -sS -p ports -Pn -iL filename -oX result.xml
扫描模块代码如下:
def scanner(filename,ports,i): strTime = time.strftime("%Y-%m-%d-%H%M%S", time.localtime(time.time())) print strTime + ' 资产最先第%d次端口扫描\n' % i os.system("nmap -sS -p "+ports+" -Pn -iL "+filename+" -oX "+str(i)+".xml") print time.strftime("%Y-%m-%d-%H%M%S", time.localtime(time.time())) + " 资产完成第%d次扫描,最先剖析" % i file='{x}.xml'.format(x=i) analysis(file,i)
3.剖析模块:
使用python的xml模块来对扫描模块的xml文本举行剖析,关于xml模块的教程可参考该网址剖析模块如下所示:
def analysis(file,i): tree = ET.parse(file) root = tree.getroot() list=[] for temp in root.findall('host'): ports = '' ip = temp.find('address').get('addr') try: xml_ports = temp.find('ports').findall('port') except: print ip + "没有检测出" continue for temp1 in xml_ports: if temp1.find('state').get('state') == 'open': port = temp1.get('portid') ports = ports + ' ' + port cursor.execute('select count(*) from host where ip=?',(ip,)) result=cursor.fetchall() if len(ports)>0 and str(result)=='[(0,)]': store(ip,ports) list.append((ip+':'+ports)) cursor.close() conn.commit() conn.close() if len(list)!=0 and i!=1: send_email(list) elif i!=1 and len(list)==0: print "本次没有新地址泛起" print "本次剖析完成!休息一小时刻继续事情。\n"
4.存储模块:
该剧本接纳的是python自带的sqlite3模块举行数据库的建立,由于SQLite自己是C写的,而且体积很小,实现简朴以是,经常被集成到种种应用程序中,甚至在iOS和Android的App中都可以集成。详细教程参见菜鸟教程建立数据、获取游标、执行sql语句:
conn=sqlite3.connect('host.db') cursor=conn.cursor() cursor.execute(r"insert into host (ip,ports) values ('{ip}', '{ports}')".format(ip=ip,ports=ports))
5.邮件发送模块:
接纳了python下的smtp模块举行了邮件的发送,详细教程参考该网址邮件模块代码如下:
def send_email(list): sender="你的163邮箱" receiver=[] str="" for ip in list: str=str+ip+"\n" text=MIMEText("the new open address and ports:\n"+str,'plain','utf-8') message=MIMEMultipart('mixed') message['From']='你的163邮箱' message['To']=';'.join(receiver) message['Subject']='new address!!!!!!' message.attach(text) smtp=smtplib.SMTP() smtp.connect('smtp.163.com') smtp.login('你的163邮箱','你的163邮箱密码') smtp.sendmail(sender,receiver,message.as_string()) smtp.quit() print "已发现新地址,邮件已发送完成\n"
所有代码
,coding:gbk import os import time import sys import argparse import smtplib import sqlite3 from email.mime.multipart import MIMEMultipart import xml.etree.ElementTree as ET from email.mime.text import MIMEText def scanner(filename,ports,i): strTime = time.strftime("%Y-%m-%d-%H%M%S", time.localtime(time.time())) print strTime + ' 资产最先第%d次端口扫描\n' % i os.system("nmap -sS -p "+ports+" -Pn -iL "+filename+" -oX "+str(i)+".xml") print time.strftime("%Y-%m-%d-%H%M%S", time.localtime(time.time())) + " 资产完成第%d次扫描,最先剖析" % i file='{x}.xml'.format(x=i) analysis(file,i) def analysis(file,i): tree = ET.parse(file) root = tree.getroot() list=[] for temp in root.findall('host'): ports = '' ip = temp.find('address').get('addr') try: xml_ports = temp.find('ports').findall('port') except: print ip + "没有检测出" continue for temp1 in xml_ports: if temp1.find('state').get('state') == 'open': port = temp1.get('portid') ports = ports + ' ' + port cursor.execute('select count(*) from host where ip=?',(ip,)) result=cursor.fetchall() if len(ports)>0 and str(result)=='[(0,)]': store(ip,ports) list.append((ip+':'+ports)) cursor.close() conn.commit() conn.close() if len(list)!=0 and i!=1: send_email(list) elif i!=1 and len(list)==0: print "本次没有新地址泛起" print "本次剖析完成!休息一小时刻继续事情。\n" def store(ip,ports): cursor.execute(r"insert into host (ip,ports) values ('{ip}', '{ports}')".format(ip=ip,ports=ports)) print "success" def send_email(list): sender="你的163邮箱" receiver=[] str="" for ip in list: str=str+ip+"\n" text=MIMEText("the new open address and ports:\n"+str,'plain','utf-8') message=MIMEMultipart('mixed') message['From']='你的163邮箱' message['To']=';'.join(receiver) message['Subject']='new address!!!!!!' message.attach(text) smtp=smtplib.SMTP() smtp.connect('smtp.163.com') smtp.login('你的163邮箱','你的163邮箱密码') smtp.sendmail(sender,receiver,message.as_string()) smtp.quit() print "已发现新地址,邮件已发送完成\n" if __name__=='__main__': i=1 prase = argparse.ArgumentParser(usage="python port_scanner.py --file=filename --ports=p1,p2,p3......",description="the script is for large assets monitoring") prase.add_argument('--file', type=str, help="IP_assets'filename to be scanned") prase.add_argument('--ports', type=str, help="ports which are needed to be scanned") if len(sys.argv)<2: print prase.usage+'\n input -h for help' ports = prase.parse_args().ports filename=prase.parse_args().file conn=sqlite3.connect('host.db') cursor=conn.cursor() cursor.execute('create table if not exists host (id INTEGER PRIMARY KEY AUTOINCREMENT,ip VARCHAR(16) UNIQUE NOT NULL,ports TEXT)') while 1: conn = sqlite3.connect('host.db') cursor = conn.cursor() scanner(filename,ports,i) time.sleep(10800) i=i+1
三、实现效果
当探测到开放服务时收到邮件截图: