基于python-nmap的扫描代码

本次代码只利于人员进行分析,小学期作业,被迫工作。

 

 

 

  1 import tkinter
  2 from tkinter import *
  3 import time
  4 import nmap
  5 import psutil
  6 LOG_LINE_NUM = 0
  7 import psutil
  8 import datetime
  9 import time
 10 import socket
 11 import uuid
 12 #import geoip2.database
 13 import dns.resolver
 14 class MY_GUI():
 15     def __init__(self, init_window_name):
 16         self.init_window_name = init_window_name
 17 
 18     def set_int_DS(self):
 19         DS= Tk()
 20         DS.geometry('400x400')
 21         DS.title('域名')
 22         DS_data_Text = Text(DS, width=150, height=49)  # 处理结果展示
 23         DS_data_Text.grid(row=1, column=12, rowspan=15, columnspan=10)
 24         res = dns.resolver.query('testfire.net', 'NS')
 25         DS_data_Text.insert(END,'本次解析域名为testfire.net')
 26         for item in res.response.answer:
 27             DS_data_Text.insert(END,item)
 28         DS.mainloop()
 29 
 30     def set_init_zisao(self):
 31         show= Tk()  # 实例化出一个父窗口
 32         show.geometry('1400x700')
 33         # 设置根窗口默认属性
 34         show.title('页面')
 35         #page=tkinter.Frame(show)
 36         result_data_Text = Text(show, width=700, height=49)  # 处理结果展示
 37         result_data_Text.grid(row=1, column=12, rowspan=15, columnspan=10)
 38         #result_data_Text.insert(END, 'a_' + str(i))
 39         now_time = time.strftime('%Y-%m-%d-%H:%M:%S'+'\n', time.localtime(time.time()))
 40         result_data_Text.insert(END,now_time)
 41         # 系统启动时间
 42         result_data_Text.insert(END,"系统启动时间: %s" % datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"+'\n'))
 43         # 系统用户
 44         users_count = len(psutil.users())
 45         users_list = ",".join([u.name for u in psutil.users()])
 46         result_data_Text.insert(END,"当前有%s个用户,分别是 %s" % (users_count, users_list)+'\n')
 47         #result_data_Text.insert(END,'-----------------------------cpu信息---------------------------------------')
 48         # 查看cpu物理个数的信息
 49         result_data_Text.insert(END,"物理CPU个数: %s" % psutil.cpu_count(logical=False)+'\n')
 50         result_data_Text.insert(END,"逻辑CPU个数: %s" % psutil.cpu_count(logical=True)+'\n')
 51         result_data_Text.insert(END,"CPU核心总数: %s" % psutil.cpu_count()+'\n')
 52         # CPU的使用率
 53         cpu = (str(psutil.cpu_percent(1))) + '%'
 54         result_data_Text.insert(END,"cup使用率: %s" % cpu+'\n')
 55         #result_data_Text.insert(END,'-----------------------------mem信息---------------------------------------')
 56         # 查看内存信息,剩余内存.free  总共.total
 57         # round()函数方法为返回浮点数x的四舍五入值。
 58         free = str(round(psutil.virtual_memory().free / (1024.0 * 1024.0 * 1024.0), 2))
 59         total = str(round(psutil.virtual_memory().total / (1024.0 * 1024.0 * 1024.0), 2))
 60         memory = int(psutil.virtual_memory().total - psutil.virtual_memory().free) / float(
 61             psutil.virtual_memory().total)
 62         swap_free = str(round(psutil.swap_memory().free / (1024.0 * 1024.0 * 1024.0), 2))
 63         swap_total = str(round(psutil.swap_memory().total / (1024.0 * 1024.0 * 1024.0), 2))
 64         swap_memory = int(psutil.swap_memory().total - psutil.swap_memory().free) / float(psutil.swap_memory().total)
 65         result_data_Text.insert(END,"物理内存: %s G" % total+'\n')
 66         result_data_Text.insert(END,"剩余物理内存: %s G" % free+'\n')
 67         result_data_Text.insert(END,"物理内存使用率: %s %%" % int(memory * 100)+'\n')
 68         result_data_Text.insert(END,"交换内存: %s G" % swap_total+'\n')
 69         result_data_Text.insert(END,"剩余交换内存: %s G" % swap_free+'\n')
 70         result_data_Text.insert(END,"交换内存使用率: %s %%" % int(swap_memory * 100)+'\n')
 71         #result_data_Text.insert(END,'-----------------------------网络信息---------------------------------------')
 72         # 网卡,可以得到网卡属性,连接数,当前流量等信息
 73         hostname = socket.gethostname()  # 获取本机主机名
 74         ip = socket.gethostbyname(hostname)  # 获取本机ip地址
 75         mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
 76         mac = ":".join([mac[e:e + 2] for e in range(0, 11, 2)])
 77         net = psutil.net_io_counters()
 78         bytes_sent = '{0:.2f} Mb'.format(net.bytes_recv / 1024 / 1024)
 79         bytes_rcvd = '{0:.2f} Mb'.format(net.bytes_sent / 1024 / 1024)
 80         result_data_Text.insert(END,"主机名称为:"+'\n', hostname)
 81         result_data_Text.insert(END,"IP地址为:"+'\n', ip)
 82         result_data_Text.insert(END,"MAC地址:"+'\n', mac)
 83         result_data_Text.insert(END,"网卡接收流量 %s 网卡发送流量 %s" % (bytes_rcvd, bytes_sent)+'\n')
 84 
 85         #result_data_Text.insert(END,'-----------------------------磁盘信息---------------------------------------')
 86         io = psutil.disk_partitions()
 87         # print("系统磁盘信息:" + str(io))
 88         o = psutil.disk_usage("/")
 89         ioo = psutil.disk_io_counters()
 90         result_data_Text.insert(END,ioo)
 91         result_data_Text.insert(END,"盘总容量:" + str(int(o.total / (1024.0 * 1024.0 * 1024.0))) + "G"+'\n')
 92         result_data_Text.insert(END,"已用容量:" + str(int(o.used / (1024.0 * 1024.0 * 1024.0))) + "G"+'\n')
 93         result_data_Text.insert(END,"可用容量:" + str(int(o.free / (1024.0 * 1024.0 * 1024.0))) + "G"+'\n')
 94 
 95         #result_data_Text.insert(END,'-----------------------------进程信息-------------------------------------')
 96         # 查看系统全部进程
 97         for pnum in psutil.pids():
 98             p = psutil.Process(pnum)
 99             result_data_Text.insert(END,"进程名 %-20s  内存利用率 %-18s 进程状态 %-10s 创建时间 %-10s " \
100                   % (p.name(), p.memory_percent(), p.status(), p.create_time())+'\n')
101         #page.pack()
102         show.mainloop()
103 
104     def set_init_saomiao(self):
105         nm = nmap.PortScanner()
106         if not self.init_data_Text.get  is NONE:
107             ret = nm.scan(self.init_data_Text.get(1.0, END).strip().replace("\n", ""), '20-1000')  # 220.181.38.148/24
108             # self.result_data_Text.insert(1.0, ret)
109             for host in nm.all_hosts():  # 返回被扫描的主机列表给host
110                 self.result_data_Text.insert(END,'Host : %s (%s)' % (host, nm[host].hostname())+'\n')  # nm[host].hostname()获取目标主机的主机名
111                 self.result_data_Text.insert(END,'State : %s' % nm[host].state()+'\n')  # nm[host].state()获取主机的状态  |up|down|unknow|skipped|
112                 for proto in nm[host].all_protocols():  # nm[host].all_protocols获取执行的协议['tcp','udp']
113                     #print('-----------------------------------------------------')
114                     self.result_data_Text.insert(END,'protocol : %s' % proto+'\n')  # 输出执行的协议
115                     lport = nm[host][proto].keys()  # 获取目标主机所开放的端口赋值给lport
116                     # lport.sort()
117                     #print('')
118                     for port in lport:  # 将lport赋值给port并遍历
119                         self.result_data_Text.insert(END,'port : %s\tstate : %s' % (port, nm[host][proto][port]['state']+'\n'))  # 输出扫描结果
120             self.result_data_Text.insert(END, ret)
121             self.write_log_to_Text("INFO:str_trans_to_md5 success")
122 
123     # 设置窗口
124     def set_init_window(self):
125         self.init_window_name.title("扫描器")  # 窗口名
126         # self.init_window_name.geometry('320x160+10+10')                         #290 160为窗口大小,+10 +10 定义窗口弹出时的默认展示位置
127         self.init_window_name.geometry('1300x800')
128         # self.init_window_name["bg"] = "pink"                                    #窗口背景色,其他背景色见:blog.csdn.net/chl0000/article/details/7657887
129         # self.init_window_name.attributes("-alpha",0.9)                          #虚化,值越小虚化程度越高
130         # 标签
131         self.init_data_label = Label(self.init_window_name, text="请输入IP")
132         self.init_data_label.grid(row=0, column=0)
133         self.result_data_label = Label(self.init_window_name, text="扫描结果")
134         self.result_data_label.grid(row=0, column=12)
135         self.log_label = Label(self.init_window_name, text="日志")
136         self.log_label.grid(row=12, column=0)
137         # 文本框
138         self.init_data_Text = Text(self.init_window_name, width=67, height=15)  # 原始数据录入框
139         self.init_data_Text.grid(row=1, column=0, rowspan=10, columnspan=10)
140         self.result_data_Text = Text(self.init_window_name, width=70, height=49)  # 处理结果展示
141         self.result_data_Text.grid(row=1, column=12, rowspan=15, columnspan=10)
142         self.log_data_Text = Text(self.init_window_name, width=66, height=9)  # 日志框
143         self.log_data_Text.grid(row=13, column=0, columnspan=10)
144         # 按钮
145         self.str_trans_to_md5_button = Button(self.init_window_name, text="扫描开始", bg="lightblue", width=10,command=self.set_init_saomiao)  # 调用内部方法  加()为直接调用
146         self.str_trans_to_md5_button.grid(row=1, column=11)
147         self.str_trans_to_md1_button = Button(self.init_window_name, text="自扫", bg="lightblue", width=10,command=self.set_init_zisao)  # 调用内部方法  加()为直接调用
148         self.str_trans_to_md1_button.grid(row=10, column=11)
149         self.str_trans_to_md2_button = Button(self.init_window_name, text="域名", bg="lightblue", width=10,command=self.set_int_DS)  # 调用内部方法  加()为直接调用
150         self.str_trans_to_md2_button.grid(row=5, column=11)
151 
152     # 获取当前时间
153     def get_current_time(self):
154         current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
155         return current_time
156 
157     # 日志动态打印
158     def write_log_to_Text(self, logmsg):
159         global LOG_LINE_NUM
160         current_time = self.get_current_time()
161         logmsg_in = str(current_time) + " " + str(logmsg) + "\n"  # 换行
162         if LOG_LINE_NUM <= 7:
163             self.log_data_Text.insert(END, logmsg_in)
164             LOG_LINE_NUM = LOG_LINE_NUM + 1
165         else:
166             self.log_data_Text.delete(1.0, 2.0)
167             self.log_data_Text.insert(END, logmsg_in)
168 
169 
170 def gui_start():
171     init_window = Tk()  # 实例化出一个父窗口
172     ZMJ_PORTAL = MY_GUI(init_window)
173     # 设置根窗口默认属性
174     ZMJ_PORTAL.set_init_window()
175 
176     init_window.mainloop()  # 父窗口进入事件循环,可以理解为保持窗口运行,否则界面不展示
177 
178 
179 gui_start()

 

 自扫

 

 

 

 有助思考,以及后面的参考。

posted @ 2022-07-11 22:36  一刀灭bug  Views(212)  Comments(0Edit  收藏  举报