2023年7月15日发(作者:)
python⽹络扫描编程_在Python中,编程实现端⼝扫描程序概览本⽂将展⽰如何使⽤Python语⾔编写 简单易⽤的端⼝扫描程序.使⽤Python实现端⼝扫描的⽅式有很多,这⾥我们使⽤Python内置的模块Socket.套接字SocketPython中的套接字socket模块提供对BSD套接字接⼝的访问。它包括⽤于处理实际数据通道的套接字类,以及⽤于⽹络相关任务的函数,例如将服务器名称转换为地址和格式化要通过⽹络发送的数据。 Source套接字⼴泛⽤于Internet,因为它们⽀持您的计算机进⾏的任何类型的⽹络通信。INET 套接字⾄少占使⽤中套接字的99%。您使⽤的Web浏览器打开⼀个套接字并连接到Web服务器。任何⽹络通信都需要通过套接字。有关套接字模块的更多信息,请参阅套接字 官⽅⽂档.套接字功能在我们开始使⽤⽰例程序之前,让我们先看⼀些我们将要使⽤的套接字功能。sock = (socket_family, socket_type)Syntax for creating a socketsock = (_INET, _STREAM)Creates a stream socketAF_INETSocket Family (here Address Family version 4 or IPv4)SOCK_STREAMSocket type TCP connectionsSOCK_DGRAMSocket type UDP connectionsgethostbyname("host")Translate a host name to IPv4 address tbyname_ex("host")Translate a host name to IPv4 address format, extended n("8.8.8.8")Get the fqdn (fully qualified domain name)tname()Returns the hostname of ption handling使⽤Python套接字创建程序如何在Python中创建⼀个简单的端⼝扫描程序这个⼩端⼝扫描程序将尝试连接您为特定主机定义的每个端⼝。我们必须做的第⼀件事是导⼊套接字库和我们需要的其他库。打开⽂本编辑器,复制粘贴下⾯的代码。将⽂件另存为:"" 然后退出编辑器。#!/usr/bin/env pythonimport socketimport subprocessimport sysfrom datetime import datetime# Clear the ('clear', shell=True)# Ask for inputremoteServer = raw_input("Enter a remote host to scan: ")remoteServerIP = tbyname(remoteServer)# Print a nice banner with information on which host we are about to scanprint "-" * 60print "Please wait, scanning remote host", remoteServerIPprint "-" * 60# Check what time the scan startedt1 = ()# Using the range function to specify ports (here it will scans all ports between 1 and 1024)# We also put in some error handling for catching errorstry:for port in range(1,1025):sock = (_INET, _STREAM)result = t_ex((remoteServerIP, port))if result == 0:print "Port {}: Open".format(port)()except KeyboardInterrupt:print "You pressed Ctrl+C"()except or:print 'Hostname could not be resolved. Exiting'()except :print "Couldn't connect to server"()# Checking the time againt2 = ()# Calculates the difference of time, to see how long it took to run the scripttotal = t2 - t1# Printing the information to screenprint 'Scanning Completed in: ', total样本输出Let's run the program and see how an output can look like$ python r a remote host to scan: _host_------------------------------------------------------------Please wait, scanning remote host ------------------------------------------------------------Port 21: OpenPort 22: OpenPort 23: OpenPort 80: OpenPort 110: OpenPort 111: OpenPort 143: OpenPort 443: OpenPort 465: OpenPort 587: OpenPort 993: OpenPort 995: OpenScanning Completed in: 0:06:34.705170声明此程序适⽤于个⼈测试⾃⼰的设备以确定是否安全性较差,如果将其⽤于任何其他⽤途,作者将不承担任何责任。
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689407865a243179.html
评论列表(0条)