简体   繁体   English

scapy :: traceroute设置源IP地址

[英]scapy::traceroute setting source ip address

I have a setup in which my source server is connected to a network using an anycast address, as a result I need to use source ip every time I ping or traceroute any destination in the network I am connected to. 我有一个设置,其中我的源服务器使用任播地址连接到网络,因此,每次ping或traceroute我连接到的网络中的任何目的地时,都需要使用源ip。

I am currently experimenting with scapy and using sr methods but traceroute in scapy has some powerful features that I need to use. 我目前正在实验scapy并使用sr方法,但是scapy中的traceroute具有一些我需要使用的强大功能。 The traceroute in scapy does not take any source addresss as the sr methods do. scapy中的traceroute不像sr方法那样使用任何源地址。

Is there a way around this? 有没有解决的办法? Or are there any wrappers on top of scapy::traceroute that allow me to do that? 还是在scapy :: traceroute之上有任何包装允许我这样做?

Inspecting Scapy 's traceroute function's code reveals that it's pretty straightforward while most of its powerful features lie with the TracerouteResult class it instantiates with the result received from a simple sr invocation, as can be seen by the current implementation: 查看Scapytraceroute函数的代码表明,它非常简单,而其大多数强大的功能都位于TracerouteResult类中,该类使用从一个简单的sr调用接收到的结果实例化,如当前实现所示:

@conf.commands.register
def traceroute(target, dport=80, minttl=1, maxttl=30, sport=RandShort(), l4 = None, filter=None, timeout=2, verbose=None, **kargs):
    """Instant TCP traceroute
traceroute(target, [maxttl=30,] [dport=80,] [sport=80,] [verbose=conf.verb]) -> None
"""
    if verbose is None:
        verbose = conf.verb
    if filter is None:
        # we only consider ICMP error packets and TCP packets with at
        # least the ACK flag set *and* either the SYN or the RST flag
        # set
        filter="(icmp and (icmp[0]=3 or icmp[0]=4 or icmp[0]=5 or icmp[0]=11 or icmp[0]=12)) or (tcp and (tcp[13] & 0x16 > 0x10))"
    if l4 is None:
        a,b = sr(IP(dst=target, id=RandShort(), ttl=(minttl,maxttl))/TCP(seq=RandInt(),sport=sport, dport=dport),
                 timeout=timeout, filter=filter, verbose=verbose, **kargs)
    else:
        # this should always work
        filter="ip"
        a,b = sr(IP(dst=target, id=RandShort(), ttl=(minttl,maxttl))/l4,
                 timeout=timeout, filter=filter, verbose=verbose, **kargs)

    a = TracerouteResult(a.res)
    if verbose:
        a.show()
    return a,b

Therefore, you may invoke sr in a similar manner to that performed here, but with a source address as well, and pass on the result to the TracerouteResult class in order to make use of its powerful features. 因此,您可以以与此处执行的方式类似的方式调用sr ,但也使用源地址,并将结果传递给TracerouteResult类,以利用其强大的功能。

NOTE: an equivalent approach can be applied to Scapy 's traceroute6 function's code for IPv6 . 注意:可以将等效方法应用于ScapyIPv6traceroute6函数的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM