Python实现TCP探测目标服务路由轨迹的原理与方法详解

yipeiwu_com6年前Python基础

本文实例讲述了Python实现TCP探测目标服务路由轨迹的原理与方法。分享给大家供大家参考,具体如下:

一 点睛

在此次实践中,通过scapy的traceroute()方法实现探测机到目标服务器的路由轨迹,整个过程的原理见下图,首先通过探测机以SYN方式进行TCP服务扫描,同时启动tcpdump进行抓包,捕获扫描过程经过的所有路由点,再通过graph()方法进行路由IP轨迹绘制,中间调用ASN映射查询IP地理信息并生成svg流程文档,最后使用ImageMagick工 具将svg格式转换成png,流程结束。

二 代码

# -*- coding: utf-8 -*-
import os,sys,time,subprocess
import warnings,logging
#屏蔽scapy 无用告警信息
warnings.filterwarnings("ignore", category=DeprecationWarning)
#屏蔽模块IPv6 多余告警
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import traceroute
#接受输入的域名或IP
domains = raw_input('Please input one or more IP/domain: ')
target = domains.split(' ')
dport = [80]  #扫描的端口列表
if len(target) >= 1 and target[0]!='':
  res,unans = traceroute(target,dport=dport,retry=-2)  #启动路由跟踪
  res.graph(target="> test.svg", ASres=None, type="svg")  #生成svg矢量图形
  time.sleep(1)
  #svg转png格式
  subprocess.Popen("/usr/bin/convert test.svg test.png", shell=True)
else:
  print "IP/domain number of errors,exit"

三 结果

四 参考

https://github.com/secdev/scapy/issues/1480

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

对python中的six.moves模块的下载函数urlretrieve详解

对python中的six.moves模块的下载函数urlretrieve详解

实验环境:windows 7,anaconda 3(python 3.5),tensorflow(gpu/cpu) 函数介绍:所用函数为six.moves下的urllib中的函数,调用如...

Python序列化基础知识(json/pickle)

     我们把对象(变量)从内存中变成可存储的过程称之为序列化,比如XML,在Python中叫pickling,在其他语言中也被称之为seria...

Python写入数据到MP3文件中的方法

本文实例讲述了Python写入数据到MP3文件中的方法。分享给大家供大家参考。具体分析如下: 通过Mp3的Id3V1数据段的数据来修正Mp3文件的正确名字,但是,有时候这个数据断中的数据...

python os.fork() 循环输出方法

先看下面这段代码: import os def main(): for i in range(0, 2): os.fork() print 'Hello'...

python计算程序开始到程序结束的运行时间和程序运行的CPU时间

执行时间 方法1复制代码 代码如下:import datetimestarttime = datetime.datetime.now()#long runningendtime = da...