# SNMP
Date: 2023-06-06


#### **SNMP**

全称为Simple Network Management Protocol（简单网络管理协议），是一种广泛使用的网络管理协议，允许管理员监视和管理网络设备，如路由器、交换机、服务器和打印机。它是一种应用层协议，促进网络设备与集中的网络管理系统之间的管理信息交换。

#### **组成**

SNMP 管理的网络由三个关键组件组成：

- Managed devices
- Agent – software which runs on managed devices
- Network management station (NMS) – software which runs on the manage

![](https://garlicspace.com/wp-content/uploads/2023/05/SNMP_communication_principles_diagram.png)

from wiki

 

#### **OID & MIB**

- OID：对象标识符
- MIB：管理信息库

 

#### **安装验证**

node2

```
sudo apt-get install snmpd
```

编辑文件/etc/snmp/snmpd.conf

```
# agentaddress: The IP address and port number that the agent will listen on.
#   By default the agent listens to any and all traffic from any
#   interface on the default SNMP port (161).  This allows you to
#   specify which address, interface, transport type and port(s) that you
#   want the agent to listen on.  Multiple definitions of this token
#   are concatenated together (using ':'s).
#   arguments: [transport:]port[@interface/address],...

agentaddress  127.0.0.1,[::1],192.168.2.76

```

 

node1

```
sudo apt-install snmp
```

```
garlic@node-1:~$ snmpwalk -Os -c public -v 1 192.168.2.76
iso.3.6.1.2.1.1.1.0 = STRING: "Linux node-2 6.1.0-1006-oem #6-Ubuntu SMP PREEMPT                                                            _DYNAMIC Tue Jan 24 18:24:09 UTC 2023 x86_64"
iso.3.6.1.2.1.1.2.0 = OID: iso.3.6.1.4.1.8072.3.2.10
iso.3.6.1.2.1.1.3.0 = Timeticks: (2150) 0:00:21.50
iso.3.6.1.2.1.1.4.0 = STRING: "Me <me@example.org>"
iso.3.6.1.2.1.1.5.0 = STRING: "node-2"
iso.3.6.1.2.1.1.6.0 = STRING: "Sitting on the Dock of the Bay"
iso.3.6.1.2.1.1.7.0 = INTEGER: 72
iso.3.6.1.2.1.1.8.0 = Timeticks: (0) 0:00:00.00
iso.3.6.1.2.1.1.9.1.2.1 = OID: iso.3.6.1.6.3.10.3.1.1
iso.3.6.1.2.1.1.9.1.2.2 = OID: iso.3.6.1.6.3.11.3.1.1
iso.3.6.1.2.1.1.9.1.2.3 = OID: iso.3.6.1.6.3.15.2.1.1
iso.3.6.1.2.1.1.9.1.2.4 = OID: iso.3.6.1.6.3.1
iso.3.6.1.2.1.1.9.1.2.5 = OID: iso.3.6.1.6.3.16.2.2.1
iso.3.6.1.2.1.1.9.1.2.6 = OID: iso.3.6.1.2.1.49
iso.3.6.1.2.1.1.9.1.2.7 = OID: iso.3.6.1.2.1.50
iso.3.6.1.2.1.1.9.1.2.8 = OID: iso.3.6.1.2.1.4
iso.3.6.1.2.1.1.9.1.2.9 = OID: iso.3.6.1.6.3.13.3.1.3
iso.3.6.1.2.1.1.9.1.2.10 = OID: iso.3.6.1.2.1.92
```

 

由于开始没有配置地址，导致使用snmpwalk出现超时情况， 抓包看到

```
19    5.459061    192.168.2.75    192.168.2.76    SNMP    82  get-next-request 1.3.6.1.2.1
20  5.459169    192.168.2.76    192.168.2.75    ICMP    110 Destination unreachable (Port unreachable)
```

```
Frame 20: 110 bytes on wire (880 bits), 110 bytes captured (880 bits)
Ethernet II, Src: VMware_db:6c:a0 (00:0c:29:db:6c:a0), Dst: VMware_0c:b9:7a (00:0c:29:0c:b9:7a)
Internet Protocol Version 4, Src: 192.168.2.76, Dst: 192.168.2.75
Internet Control Message Protocol
    Type: 3 (Destination unreachable)
    Code: 3 (Port unreachable)
    Checksum: 0x8326 [correct]
    [Checksum Status: Good]
    Unused: 00000000
    Internet Protocol Version 4, Src: 192.168.2.75, Dst: 192.168.2.76
    User Datagram Protocol, Src Port: 44331, Dst Port: 161
Simple Network Management Protocol
    version: version-1 (0)
    community: public
    data: get-next-request (1)
        get-next-request
            request-id: 1100403277
            error-status: noError (0)
            error-index: 0
            variable-bindings: 1 item
                1.3.6.1.2.1: Value (Null)
                    Object Name: 1.3.6.1.2.1 (iso.3.6.1.2.1)
                    Value (Null)

```

网上找到这样解释, 如果udp包发送到一个没有监听端口主机，此消息将 将 ip 标头和有效负载的第一部分作为 icmp 有效载荷。

```
If an udp message is sent to a host that does not listen on the
specific udp port, an "icmp destination port unreachable" message
is sent back to the src-ip of the udp message. This message will
have the first part of the ip header and payload as the icmp
payload.

```

from https://wireshark-users.wireshark.narkive.com/Kw2nD9cR/filter-for-snmp-doesn-t-work-also-shows-icmp

验证端口也可以使用nmap

```
garlic@node-1:~$ sudo nmap -sU -p 161 192.168.2.76
Starting Nmap 7.80 ( https://nmap.org ) at 2023-06-04 11:02 UTC
Nmap scan report for 192.168.2.76 (192.168.2.76)
Host is up (0.00061s latency).

PORT    STATE SERVICE
161/udp open  snmp
MAC Address: 00:0C:29:DB:6C:A0 (VMware)

Nmap done: 1 IP address (1 host up) scanned in 0.39 seconds

```

 

node1

snmpwalk -Os -c public -v 1 192.168.91.28

node2

sudo tcpdump -i ens33 -w /tmp/snmp.pcap

```
11    2.497082    192.168.91.116  192.168.91.28   SNMP    82  get-next-request 1.3.6.1.2.1
12  2.497526    192.168.91.28   192.168.91.116  SNMP    179 get-response 1.3.6.1.2.1.1.1.0
13  2.498095    192.168.91.116  192.168.91.28   SNMP    85  get-next-request 1.3.6.1.2.1.1.1.0
16  2.498359    192.168.91.28   192.168.91.116  SNMP    95  get-response 1.3.6.1.2.1.1.2.0
```

 

#### **协议**

SNMP 运行在应用层。 所有 SNMP 消息都通过用户数据报协议 (UDP) 传输。 SNMP 代理在 UDP 端口 161 上接收请求。管理器（Manager）可以将请求从任何可用的源端口发送到代理中的端口 161。 代理（Agent）响应被发送回管理器上的源端口。 管理器（Manager）在端口 162 上接收通知（Traps 和 InformRequests）。代理（Agent）可以从任何可用端口生成通知。 当与**传输层安全性或数据报传输层安全**性(TLS)一起使用时，请求在端口 10161 上接收，通知发送到端口 10162。

> 接收请求 161/10161  接收通知（trap） 162/10162

All SNMP PDUs are constructed as follows:

| IP header | UDP header | version | community | PDU-type | request-id | error-status | error-index | variable bindings |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |

PDU-type：

- GetRequest
- SetRequest
- GetNextRequest：  MIB 中字典顺序的下一个参数
- GetBulkRequest
- Response
- Trap
- InformRequest

> Getbulk 和 inform 两个是snmpv2 版本新增，
> 
> trap与InformRequest前几个不同，他们工作模式是主动推送

 

SNMP有三种版本：SNMPv1，SNMPv2c和SNMPv3。

- SNMPv1：SNMP的第一个版本，它提供了一种监控和管理计算机网络的系统方法，它基于团体名认证，安全性较差，且返回报文的错误码也较少。它在RFC 1155和RFC 1157中定义。
- SNMPv2c：第二个版本SNMPv2c引入了GetBulk和Inform操作，支持更多的标准错误码信息，支持更多的数据类型。它在RFC 1901，RFC 1905和RFC 1906中定义。
- SNMPv3：鉴于SNMPv2c在安全性方面没有得到改善，IETF颁布了SNMPv3版本，提供了基于USM（User Security Module）的认证加密和基于VACM（View-based Access Control Model）的访问控制，是迄今为止最安全的版本。SNMPv3在RFC 1905，RFC 1906，RFC 2571，RFC 2572，RFC 2574和RFC 2575中定义。

 

#### **参考及引用**

https://www.comparitech.com/net-admin/snmpwalk-examples-windows-linux/

https://unix.stackexchange.com/questions/261360/icmp-port-unreachable-error-even-if-port-is-open

https://wiki.wireshark.org/SNMP

https://info.support.huawei.com/info-finder/encyclopedia/zh/SNMP.html

图片from[**陳丁光**](https://www.facebook.com/groups/474930279315210/user/100010173011046/?__cft__[0]=AZUvvZhUDothfW7d6FYSLSv0rp6IACurl76Do5aaskmI6Mwfcl7JD4m2BOVQ9mLxa0UnmrrpLe5omhy-2sIPgudVJ52l_jnXYsR2WxABQoRAG85_rk-MySR42vS9Rdo0VwiuWgaboZHmTEVezuOzmp96X2Onfop9PTPUYtT4OEM0Sa3Vl0Hv7r5MeYf1lnYCF78&__tn__=-UC%2CP-R)

