我正在阅读暴力python一书,并使用github中的一些示例在python 3中编写代码。对于大多数端口,一切都进行得很好,但是在端口扫描程序上,我正在运行此重复的错误模块“套接字”没有属性“ SOCKET_STREAM”。我尝试搜索此示例,但是我看到的大多数示例都不相关。
尽管我不认为这是操作系统问题,但我曾尝试在Windows和Linux上对此进行编码。
#!/bin/bash/python 3.7
import argparse
import binascii
import socket
import threading
screen_lock = threading.Semaphore(value=1)
def conn_scan(target_host, target_port):
conn_skt = socket.socket(socket.AF_INET, socket.SOCKET_STREAM)
error = conn_skt.connect_ex(target_host, target_port)
screen_lock.aquire()
if not error:
print("[*] %d tcp open, " % target_port)
else:
print("[*] %d tcp closed." % target_port)
screen_lock.release()
conn_skt.close()
def ban_scan(target_host, target_port):
try:
conn_skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn_skt.connect(target_host, target_port)
conn_skt.send(b'ViolentPython3\r\n')
conn_skt.recv(100)
screen_lock.aquire()
print("[+] %d/tcp open " % target_port)
except Exception:
print("[-] %d/tcp closed " % target_port)
finally:
screen_lock.release()
conn_skt.close()
def initiate_scan(target_host, target_ports):
try:
target_IP = socket.gethostbyname(target_host)
except Exception:
print("[-] Cannot resolve '%s': Unknown host" % target_host)
return
try:
target_name = socket.gethostbyaddr(target_IP)
print("\n[+] Scan results for: " + target_name[0])
except Exception:
print("\n[+] Scan results for : " + target_IP)
socket.setdefaulttimeout(1)
for target_port in target_ports:
print("Scanning port " + target_port)
t = threading.Thread(target=conn_scan, args=(target_host, int(target_port)))
t.start()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-H', '--host', help='specify target host')
parser.add_argument('-P', '--port', help='specify target ports separated by comma')
args = parser.parse_args()
target_host = args.host
target_ports = str(args.port).split(',')
if(target_host is None) | (target_ports[0] is None):
parser.print_help()
exit(0)
initiate_scan(target_host, target_ports)
if __name__ == '__main__':
main()
应显示端口扫描的输出