【问题标题】:Lambda use with ScapyLambda 与 Scapy 一起使用
【发布时间】:2014-07-30 15:54:36
【问题描述】:

我是 Python 新手,在使用 Scapy 进行 ICMP ping 扫描时遇到了 lambda 函数的使用。我已经阅读了一些教程,但我很难理解这个概念以正确利用我的代码。

我试图弄清楚 lambda 参数如何准确引用 Scapy 给出的输出;主要是 's' 和 'r' 的功能以及它们如何使用 '%IP.src%' 直接从 IP 字段中提取数据?

def icmp_ping(target_ip):
"Function for a classic ICMP Ping sweep."
    ans,unans=sr(IP(dst=target_ip)/ICMP(), timeout=3)
    ans.summary(lambda (s,r): r.sprintf("%IP.src% is alive") )
    ans.summary(lambda (s,r): r.sprintf("%IP.ttl% is the TTL value") )
    print(ans)

它给出了输出:

Received 3 packets, got 1 answers, remaining 10 packets
192.168.0.100 is alive
64 is the TTL value
[(<IP  frag=0 proto=icmp dst=192.168.0.100 |<ICMP  |>>, <IP  version=4L ihl=5L tos=0x0 len=28 id=30394 flags= frag=0L ttl=64 proto=icmp chksum=0x820d src=192.168.0.100 dst=192.168.0.101 options=[] |<ICMP  type=echo-reply code=0 chksum=0xffff id=0x0 seq=0x0 |<Padding  load='\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' |>>>)]

希望这个问题有意义!

【问题讨论】:

  • 似乎我已经设法弄清楚了一些。 lambda 功能等同于code for s,r in ans: code 其中 s = 发送和 r= 接收。然后使用 sprintf 拉出需要的数据。

标签: python lambda network-programming scapy


【解决方案1】:

ans 具有包含发送和接收数据包的元组。 Lambda 采用这个元组。您正在获取接收数据包的 IP 标头并写入源地址。在python3中你必须写如下

 ans.summary(lambda p: p[1].sprintf("%IP.src% is alive"))

在 python 3 中,参数以元组的形式出现,你必须通过它的索引来接收数据包

【讨论】:

    猜你喜欢
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 2016-07-08
    • 2015-11-10
    • 2015-12-08
    相关资源
    最近更新 更多