1 Star 0 Fork 46

ITI0/qytang_Python

forked from 张鹏/qytang_Python 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
2016.03.04 scapy 端口扫描 2.48 KB
一键复制 编辑 原始数据 按行查看 历史
现任明教教主-乾颐堂 提交于 2016-03-04 10:25 . new file
======================syn_scan_one_port=====================================
#!/usr/bin/python3.4
# -*- coding=utf-8 -*-
import sys
from io import StringIO
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
import re
from scapy.all import *
def syn_scan_one_port(hostname,port):
f_handler = StringIO()
__console__=sys.stdout
sys.stdout = f_handler
result_raw = sr(IP(dst=hostname)/TCP(sport=RandShort(),dport=[port],flags="S"), verbose = False)
result_raw[0][0][1].display()
sys.stdout=__console__
scan_raw = f_handler.getvalue()
flag_result = re.findall('.*\s+(flags)\s*=\s*(.*).*', scan_raw)
for flag in flag_result:
if flag[1] == 'SA':
#print('Host: ' + hostname + ' Port:' + str(port) + ' is Open!!!')
os._exit(3)
if __name__ == '__main__':
syn_scan_one_port('202.100.1.200',139)
======================scapy_syn_scan=====================================
#!/usr/bin/python3.4
# -*- coding=utf-8 -*-
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
import re
import time
import multiprocessing
from syn_scan_one_port import syn_scan_one_port
from scapy.all import *
def scapy_syn_scan(host,port_low,port_high):
processes = []
ports = []
port_l = int(port_low)
port_h = int(port_high) + 1
for port in range(port_l,port_h):
syn_one = multiprocessing.Process(target=syn_scan_one_port, args=(host, port))
#print('Started ' + str(port) + ' Scan!!!')
syn_one.start()
processes.append(syn_one)
ports.append(port)
#print(processes)
#print(ports)
port_no = 0
time.sleep(3)
for process in processes:
if process.exitcode == 3:
print('Host: ' + host + ' Port:' + str(ports[port_no]) + ' is Open!!!')
else:
process.terminate()
port_no = port_no + 1
if __name__ == '__main__':
host = input('请你输入扫描主机的IP地址: ')
port_low = input('请你输入扫描端口的最低端口号: ')
port_high = input('请你输入扫描端口的最高端口号: ')
scapy_syn_scan(host,port_low,port_high)
#scapy_syn_scan('202.100.1.200',135,140)
=====================扫描效果===================================================
[root@Fedora python]# ./scapy_syn_scan.py
请你输入扫描主机的IP地址: 202.100.1.200
请你输入扫描端口的最低端口号: 100
请你输入扫描端口的最高端口号: 500
Host: 202.100.1.200 Port:135 is Open!!!
Host: 202.100.1.200 Port:139 is Open!!!
Host: 202.100.1.200 Port:445 is Open!!!
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/iti0/qytang_Python.git
git@gitee.com:iti0/qytang_Python.git
iti0
qytang_Python
qytang_Python
master

搜索帮助