|
| 1 | +//go:build linux |
| 2 | + |
| 3 | +package pb |
| 4 | + |
| 5 | +import ( |
| 6 | + "encoding/xml" |
| 7 | + "fmt" |
| 8 | + "net" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "os/exec" |
| 12 | + "strconv" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "github.com/pocketbase/pocketbase/apis" |
| 16 | + "github.com/pocketbase/pocketbase/core" |
| 17 | + "kernel.org/pub/linux/libs/security/libcap/cap" |
| 18 | +) |
| 19 | + |
| 20 | +func HandlerScan(e *core.RequestEvent) error { |
| 21 | + // check if nmap installed |
| 22 | + nmap, err := exec.LookPath("nmap") |
| 23 | + if err != nil { |
| 24 | + return apis.NewBadRequestError(err.Error(), nil) |
| 25 | + } |
| 26 | + |
| 27 | + // check if scan range is valid |
| 28 | + allPrivateSettings, err := e.App.FindAllRecords("settings_private") |
| 29 | + if err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + settingsPrivate := allPrivateSettings[0] |
| 33 | + scanRange := settingsPrivate.GetString("scan_range") |
| 34 | + _, ipNet, err := net.ParseCIDR(scanRange) |
| 35 | + if err != nil { |
| 36 | + return apis.NewBadRequestError(err.Error(), nil) |
| 37 | + } |
| 38 | + |
| 39 | + // run nmap |
| 40 | + timeout := os.Getenv("UPSNAP_SCAN_TIMEOUT") |
| 41 | + if timeout == "" { |
| 42 | + timeout = "500ms" |
| 43 | + } |
| 44 | + orig := cap.GetProc() |
| 45 | + defer orig.SetProc() // restore original caps on exit. |
| 46 | + |
| 47 | + c, err := orig.Dup() |
| 48 | + if err != nil { |
| 49 | + return fmt.Errorf("Failed to dup existing capabilities: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + if on, _ := c.GetFlag(cap.Permitted, cap.NET_RAW); !on { |
| 53 | + return fmt.Errorf("Privileged ping selected but NET_RAW capability not permitted") |
| 54 | + } |
| 55 | + |
| 56 | + if err := c.SetFlag(cap.Effective, true, cap.NET_RAW); err != nil { |
| 57 | + return fmt.Errorf("unable to set NET_RAW capability effective") |
| 58 | + } |
| 59 | + |
| 60 | + if err := c.SetFlag(cap.Inheritable, true, cap.NET_RAW); err != nil { |
| 61 | + return fmt.Errorf("unable to set NET_RAW capability inheritable") |
| 62 | + } |
| 63 | + |
| 64 | + if err := c.SetProc(); err != nil { |
| 65 | + return fmt.Errorf("unable to raise NET_RAW capability") |
| 66 | + } |
| 67 | + |
| 68 | + if err := cap.SetAmbient(true, cap.NET_RAW); err != nil { |
| 69 | + return fmt.Errorf("unable to set NET_RAW capability ambient") |
| 70 | + } |
| 71 | + |
| 72 | + cmd := exec.Command(nmap, "-sn", "-oX", "-", scanRange, "--host-timeout", timeout, "--privileged") |
| 73 | + cmdOutput, err := cmd.Output() |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + // unmarshal xml |
| 79 | + nmapOutput := Nmaprun{} |
| 80 | + if err := xml.Unmarshal(cmdOutput, &nmapOutput); err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + type Device struct { |
| 85 | + Name string `json:"name"` |
| 86 | + IP string `json:"ip"` |
| 87 | + MAC string `json:"mac"` |
| 88 | + MACVendor string `json:"mac_vendor"` |
| 89 | + } |
| 90 | + |
| 91 | + // extract info from struct into data |
| 92 | + type Response struct { |
| 93 | + Netmask string `json:"netmask"` |
| 94 | + Devices []Device `json:"devices"` |
| 95 | + } |
| 96 | + res := Response{} |
| 97 | + var nm []string |
| 98 | + for _, octet := range ipNet.Mask { |
| 99 | + nm = append(nm, strconv.Itoa(int(octet))) |
| 100 | + } |
| 101 | + res.Netmask = strings.Join(nm, ".") |
| 102 | + |
| 103 | + for _, host := range nmapOutput.Host { |
| 104 | + dev := Device{} |
| 105 | + for _, addr := range host.Address { |
| 106 | + if addr.Addrtype == "ipv4" { |
| 107 | + dev.IP = addr.Addr |
| 108 | + } else if addr.Addrtype == "mac" { |
| 109 | + dev.MAC = addr.Addr |
| 110 | + } |
| 111 | + if addr.Vendor != "" { |
| 112 | + dev.MACVendor = addr.Vendor |
| 113 | + } else { |
| 114 | + dev.MACVendor = "Unknown" |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if dev.IP == "" || dev.MAC == "" { |
| 119 | + continue |
| 120 | + } |
| 121 | + |
| 122 | + names, err := net.LookupAddr(dev.IP) |
| 123 | + if err != nil || len(names) == 0 { |
| 124 | + dev.Name = dev.MACVendor |
| 125 | + } else { |
| 126 | + dev.Name = strings.TrimSuffix(names[0], ".") |
| 127 | + } |
| 128 | + |
| 129 | + if dev.Name == "" && dev.MACVendor == "" { |
| 130 | + continue |
| 131 | + } |
| 132 | + |
| 133 | + res.Devices = append(res.Devices, dev) |
| 134 | + } |
| 135 | + |
| 136 | + return e.JSON(http.StatusOK, res) |
| 137 | +} |
0 commit comments