SNMP Command Injection Check: Step-by-Step
In this article
This guide shows you how to test whether a server's SNMP service is vulnerable to command injection. You will learn to identify the SNMP version in use, check for default community strings, and safely send crafted SNMP SET requests that attempt to execute system commands. Before you begin, ensure you have explicit authorization to test the target server; unauthorized scanning is illegal.
What You'll Need
- A server running SNMP (usually on UDP port 161) that you are authorized to test.
- Access to a command line on a machine that can reach the server (Linux, macOS, or Windows with WSL).
- SNMP command-line tools:
snmpget,snmpwalk,snmpset(usually part of the net-snmp package). - Basic familiarity with terminal commands and SNMP concepts (OIDs, community strings).
Step-by-Step
- Identify the SNMP version and community string. Try common community strings (public, private) with SNMPv1 or v2c. Use
snmpgetto read a standard OID:
snmpget -v1 -c public1.3.6.1.2.1.1.1.0
If that fails, try-v2c. If you get a response, note the version and community string. - Check if SNMP writes are allowed. Many servers only allow read-only access. To test write access, attempt to set a harmless OID (like sysLocation) to its current value. Use
snmpset:
snmpset -v2c -c1.3.6.1.2.1.1.6.0 s "test"
If you get an error like “noAccess” or “notWritable”, writes are not enabled, and command injection via SNMP is unlikely (though not impossible if there are other vulnerabilities). - Look for vendor-specific extension OIDs. Command injection usually occurs through custom MIBs or vendor extensions that allow executing commands. Common examples include certain Net-SNMP debug OIDs or vendor-specific enterprise OIDs. You can walk the entire MIB tree to find writable OIDs:
snmpwalk -v2c -c.1
Look for OIDs that appear to be command-execution related (e.g., names containing “cmd”, “exec”, “shell”). - Test command injection via SNMP SET. If you find a writable OID that appears to accept a string, try injecting a command. A common test is to set an OID to a string that includes a shell command substitution, such as:
snmpset -v2c -cs "; id;"
Or use backticks:snmpset ... s "`id`". After setting, check if the command output appears in the OID value when you read it back, or if the server behaves differently (e.g., creates a file). - Confirm the injection. If the command executes, you may see the output in the OID or in the server's logs. A safer test is to create a unique file in /tmp (if you have permissions) and check for its existence:
snmpset -v2c -cs "; touch /tmp/snmp_test;"
Then try to read that file via SNMP if possible, or check via other means (if you have SSH access). - Document your findings. Record the exact OID, community string, SNMP version, and the payload used. This information is critical for remediation.
Common Mistakes
- Testing without authorization. Always ensure you have written permission before scanning or testing any server.
- Using the wrong SNMP version. Some servers only respond to v1 or v2c; try both.
- Assuming write access. Most SNMP deployments are read-only; if you can't write, command injection via SNMP is not possible (unless there's a separate vulnerability).
- Overlooking vendor-specific OIDs. Standard MIBs rarely have command execution; the vulnerability is almost always in custom or vendor extensions.
- Not checking for output. Some commands execute but the output is not returned via SNMP; you may need to check for side effects (like file creation).
Tips
- Use
snmpwalkwith a timeout to avoid hanging on large MIB trees:snmpwalk -t 2 -r 0 ... - If you find a writable OID, test with a benign command first (like
echo test) before trying anything destructive. - Check the SNMP service version (via sysDescr OID) and search for known vulnerabilities for that version.
- Use a packet capture tool (like tcpdump) to see exactly what the server sends back after a SET request.
- If the server uses SNMPv3, you'll need valid credentials; try default usernames (like “initial”) but this is less common.
Troubleshooting
- No response to snmpget: Verify the server is listening on UDP 161 (use
nmap -sU -p 161). Check firewalls. Try different community strings and versions. - “No access” error on snmpset: The community string may be read-only. Try a different community (like “private”). If still failing, writes are likely disabled.
- Command doesn't execute: The OID may not be a command execution point. Look for other writable OIDs. Also, the server may sanitize input or use a different shell.
- Command executes but no output: The output may be sent to a different OID or logged. Try a side-effect test (create a file).
- SNMPwalk times out: The MIB tree may be huge. Use a more specific base OID, or increase the timeout with
-t.
Frequently Asked Questions
Is command injection via SNMP common?
It's not common in standard SNMP implementations, but it has been found in some vendor-specific extensions and poorly coded agents. It's more likely in older or custom devices.
What is the difference between SNMPv1, v2c, and v3?
SNMPv1 and v2c use community strings (plaintext passwords). SNMPv3 adds authentication and encryption. Command injection is more likely in v1/v2c because they lack security.
Can I test for command injection without knowing the community string?
No, you need valid credentials. You can try default community strings like “public” and “private”, but if they don't work, you cannot proceed without authorization.
What should I do if I find a vulnerability?
Report it to the system owner immediately. Do not exploit it further. Suggest disabling SNMP if not needed, or restrict access by IP and use SNMPv3 with strong credentials.
Can command injection be prevented?
Yes, by disabling SNMP writes, using SNMPv3, filtering access to port 161, and keeping the SNMP agent updated. Also, validate all input in custom MIB extensions.
Do I need to test both IPv4 and IPv6?
If the server has an IPv6 address, it's a good idea to test both, as firewalls may be configured differently for each.