scapy学习icmp报文

本文介绍了ICMP协议的基本概念,包括其在网络层的作用及简单的数据结构。详细列举了ICMP报文类型及其含义,并通过Scapy示例展示了如何发送与接收ICMP报文。此外,还讨论了防止ping扫描的策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  icmp(Internet Control Message Protocol)报文真的是一个数据结构特别简单的报文,虽然它使用到了ip头部,协议位于网络层。说它数据结构简单主要是因为icmp报文最主要出于网络诊断的目的,比如检测某个主机或者路由器是否正确到达,不像tcp,udp或者更上层协议的报文会携带很多额外的信息。

1.icmp报文类型
  在icmp报文中最重要的只有type和code字段,其中code相当与是对type更进一步的状态解释。

TypeCodeStatusDescription
0 – Echo Reply0Echo reply (used to ping)
1 and 2unassignedReserved
3 – Destination Unreachable0Destination network unreachable
1Destination host unreachable
2Destination protocol unreachable
3Destination port unreachable
4Fragmentation required, and DF flag set
5Source route failed
6Destination network unknown
7Destination host unknown
8Source host isolated
9Network administratively prohibited
10Host administratively prohibited
11Network unreachable for ToS
12Host unreachable for ToS
13Communication administratively prohibited
14Host Precedence Violation
15Precedence cutoff in effect
4 – Source Quench0deprecatedSource quench (congestion control)
5 – Redirect Message0Redirect Datagram for the Network
1Redirect Datagram for the Host
2Redirect Datagram for the ToS & network
3Redirect Datagram for the ToS & host
6deprecatedAlternate Host Address
7unassignedReserved
8 – Echo Request0Echo request (used to ping)
9 – Router Advertisement0Router Advertisement
10 – Router Solicitation0Router discovery/selection/solicitation
11 – Time Exceeded0TTL expired in transit
1Fragment reassembly time exceeded
12 – Parameter Problem: Bad IP header0Pointer indicates the error
1Missing a required option
2Bad length
13 – Timestamp0Timestamp
14 – Timestamp Reply0Timestamp reply
15 – Information Request0deprecatedInformation Request
16 – Information Reply0deprecatedInformation Reply
17 – Address Mask Request0deprecatedAddress Mask Request
18 – Address Mask Reply0deprecatedAddress Mask Reply
19reservedReserved for security
20 through 29reservedReserved for robustness experiment
30 – Traceroute0deprecatedInformation Request
31deprecatedDatagram Conversion Error
32deprecatedMobile Host Redirect
33deprecatedWhere-Are-You (originally meant for IPv6)
34deprecatedHere-I-Am (originally meant for IPv6)
35deprecatedMobile Registration Request
36deprecatedMobile Registration Reply
37deprecatedDomain Name Request
38deprecatedDomain Name Reply
39deprecatedSKIP Algorithm Discovery Protocol, Simple Key-Management for Internet Protocol
40Photuris, Security failures
41experimentalICMP for experimental mobility protocols such as Seamoby
42 through 252unassignedReserved
253experimentalRFC3692-style Experiment 1
254experimentalRFC3692-style Experiment 2
255reservedReserved

 
2.scapy发送icmp包与woreshark抓包
  发送icmp包 sr1(IP(dst="192.168.1.102")/ICMP()/"zhou") 此时type为8
这里写图片描述
  “zhou”属于最后的padding字段,不要也罢。

  sr1同样会接收来自102主机的回复包

In [20]: result.display()
###[ IP ]### 
  version   = 4L
  ihl       = 5L
  tos       = 0x0
  len       = 32
  id        = 593
  flags     = 
  frag      = 0L
  ttl       = 64
  proto     = icmp
  chksum    = 0xf46b
  src       = 192.168.1.102
  dst       = 192.168.1.106
  \options   \
###[ ICMP ]### 
     type      = echo-reply
     code      = 0
     chksum    = 0x1622
     id        = 0x0
     seq       = 0x0
###[ Raw ]### 
        load      = 'zhou'

 
3.防ping策略
  我们通常使用的ping和traceroute命令都在悄悄的使用icmp报文去检测主机的存活状态。当然从网络安全的角度来说,检测主机是否存活是渗透的第一步,所以从win7开始就已经默认开启了防ping规则,在linux中如果要过滤icmp包只需要在/etc/rc.local文件中添加一句echo "echo 1 >/proc/sys/net/ipv4/icmp_echo_ignore_all" >>/etc/rc.d/rc.local 但是如果使用这种方法的话,连自己都不能ping别人了。所以高级一点的还是使用防火墙规则吧。 iptables -A INPUT -p icmp --icmp-type 8 -s 0/0 -j DROP.
  当然如果真要较真网络安全中检测对方存活主机,不仅仅有icmp协议,还可以通过syn,udp等手段,当然这是后话了,不属于本篇文章。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值