Add SNMP v2c probing and results endpoint.

Probe sysName/sysDescr/sysObjectID for alive hosts, persist SNMP outcomes, and expose scan SNMP data via REST.

Made-with: Cursor
This commit is contained in:
Andrey Lutsenko
2026-04-09 21:56:31 +10:00
parent e9452dcc0a
commit cca4477792
10 changed files with 234 additions and 3 deletions
+55
View File
@@ -358,3 +358,58 @@ limit $2`
}
return out
}
func (s *PostgresStore) SaveSNMPResult(scanID string, result SNMPResult) error {
query := `
insert into scan_snmp (scan_id, ip, success, sys_name, sys_descr, sys_object_id, error_text, checked_at)
values ($1, $2, $3, $4, $5, $6, $7, $8)`
_, err := s.db.ExecContext(
context.Background(),
query,
scanID,
result.IP,
result.Success,
result.SysName,
result.SysDescr,
result.SysObjectID,
result.Error,
result.CheckedAt,
)
return err
}
func (s *PostgresStore) ListSNMPResults(scanID string, limit int) []SNMPResult {
if limit <= 0 {
limit = 1000
}
query := `
select ip::text, success, coalesce(sys_name, ''), coalesce(sys_descr, ''), coalesce(sys_object_id, ''),
coalesce(error_text, ''), checked_at
from scan_snmp
where scan_id = $1
order by checked_at desc
limit $2`
rows, err := s.db.QueryContext(context.Background(), query, scanID, limit)
if err != nil {
return nil
}
defer rows.Close()
out := make([]SNMPResult, 0, limit)
for rows.Next() {
var sres SNMPResult
if err = rows.Scan(
&sres.IP,
&sres.Success,
&sres.SysName,
&sres.SysDescr,
&sres.SysObjectID,
&sres.Error,
&sres.CheckedAt,
); err != nil {
return nil
}
out = append(out, sres)
}
return out
}
+66 -2
View File
@@ -1,6 +1,7 @@
package scans
import (
"fmt"
"log"
"net"
"net/netip"
@@ -9,14 +10,22 @@ import (
"strconv"
"sync"
"time"
"github.com/gosnmp/gosnmp"
)
type Runner struct {
store Store
cfg RunnerConfig
}
func NewRunner(store Store) *Runner {
return &Runner{store: store}
type RunnerConfig struct {
SNMPEnabled bool
SNMPCommunity string
}
func NewRunner(store Store, cfg RunnerConfig) *Runner {
return &Runner{store: store, cfg: cfg}
}
func (r *Runner) Start(job ScanJob) {
@@ -87,6 +96,9 @@ func (r *Runner) run(job ScanJob) {
})
}
}
if isUp && r.cfg.SNMPEnabled {
_ = r.store.SaveSNMPResult(job.ID, probeSNMP(ip, r.cfg.SNMPCommunity, checkedAt, job.Options.PingTimeoutMS))
}
mu.Lock()
done++
@@ -125,6 +137,58 @@ func (r *Runner) run(job ScanJob) {
})
}
func probeSNMP(ip, community string, checkedAt time.Time, timeoutMS int) SNMPResult {
if timeoutMS <= 0 {
timeoutMS = 700
}
if community == "" {
community = "public"
}
client := &gosnmp.GoSNMP{
Target: ip,
Port: 161,
Version: gosnmp.Version2c,
Community: community,
Timeout: time.Duration(timeoutMS) * time.Millisecond,
Retries: 1,
}
if err := client.Connect(); err != nil {
return SNMPResult{IP: ip, Success: false, Error: err.Error(), CheckedAt: checkedAt}
}
defer client.Conn.Close()
oids := []string{
".1.3.6.1.2.1.1.5.0",
".1.3.6.1.2.1.1.1.0",
".1.3.6.1.2.1.1.2.0",
}
pkt, err := client.Get(oids)
if err != nil {
return SNMPResult{IP: ip, Success: false, Error: err.Error(), CheckedAt: checkedAt}
}
out := SNMPResult{IP: ip, Success: true, CheckedAt: checkedAt}
for _, vb := range pkt.Variables {
switch vb.Name {
case ".1.3.6.1.2.1.1.5.0":
out.SysName = snmpValueToString(vb.Value)
case ".1.3.6.1.2.1.1.1.0":
out.SysDescr = snmpValueToString(vb.Value)
case ".1.3.6.1.2.1.1.2.0":
out.SysObjectID = snmpValueToString(vb.Value)
}
}
return out
}
func snmpValueToString(v any) string {
if b, ok := v.([]byte); ok {
return string(b)
}
return fmt.Sprint(v)
}
func expandTargets(cidrs []string, excludeIPs []string) ([]string, error) {
excluded := make(map[string]struct{}, len(excludeIPs))
for _, ip := range excludeIPs {
+43
View File
@@ -63,6 +63,8 @@ type Store interface {
ListHostResults(scanID string, limit int) []HostResult
SaveOpenPortResult(scanID string, result OpenPortResult) error
ListOpenPortResults(scanID string, limit int) []OpenPortResult
SaveSNMPResult(scanID string, result SNMPResult) error
ListSNMPResults(scanID string, limit int) []SNMPResult
}
type HostResult struct {
@@ -78,11 +80,22 @@ type OpenPortResult struct {
CheckedAt time.Time `json:"checked_at"`
}
type SNMPResult struct {
IP string `json:"ip"`
Success bool `json:"success"`
SysName string `json:"sys_name"`
SysDescr string `json:"sys_descr"`
SysObjectID string `json:"sys_object_id"`
Error string `json:"error,omitempty"`
CheckedAt time.Time `json:"checked_at"`
}
type MemoryStore struct {
mu sync.RWMutex
jobs map[string]ScanJob
hosts map[string][]HostResult
ports map[string][]OpenPortResult
snmp map[string][]SNMPResult
}
func NewMemoryStore() *MemoryStore {
@@ -90,6 +103,7 @@ func NewMemoryStore() *MemoryStore {
jobs: make(map[string]ScanJob),
hosts: make(map[string][]HostResult),
ports: make(map[string][]OpenPortResult),
snmp: make(map[string][]SNMPResult),
}
}
@@ -240,6 +254,35 @@ func (s *MemoryStore) ListOpenPortResults(scanID string, limit int) []OpenPortRe
return out
}
func (s *MemoryStore) SaveSNMPResult(scanID string, result SNMPResult) error {
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.jobs[scanID]; !ok {
return errors.New("scan not found")
}
s.snmp[scanID] = append(s.snmp[scanID], result)
return nil
}
func (s *MemoryStore) ListSNMPResults(scanID string, limit int) []SNMPResult {
if limit <= 0 {
limit = 1000
}
s.mu.RLock()
defer s.mu.RUnlock()
items := s.snmp[scanID]
if len(items) <= limit {
out := make([]SNMPResult, len(items))
copy(out, items)
return out
}
out := make([]SNMPResult, limit)
copy(out, items[:limit])
return out
}
func applyDefaultOptions(opts *ScanOptions) {
if opts.PingTimeoutMS <= 0 {
opts.PingTimeoutMS = 700