scanner.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package scan
  2. import (
  3. "fmt"
  4. "net"
  5. "net/netip"
  6. "sync"
  7. "time"
  8. )
  9. var PORT_MAP = map[int]string{
  10. 22: "ssh", 23: "telnet", 53: "dns", 80: "http", 25: "smtp", 443: "https", 8080: "unknown", 8081: "unknown",
  11. 8082: "unknown", 8085: "unknown", 8090: "unknown", 8091: "unknown", 9010: "unknown", 9012: "unknown", 10000: "unknown",
  12. }
  13. type TcpScanHost struct {
  14. Host string `json:"host"`
  15. Ipv4Address netip.Addr `json:"ipv4_address"`
  16. PortsScanned []PortScanResult `json:"ports_scanned"`
  17. }
  18. type PortScanResult struct {
  19. PortNumber int `json:"port_number"`
  20. Service string `json:"service"`
  21. Protocol string `json:"protocol"`
  22. Listening bool `json:"listening"`
  23. }
  24. type PortScanDirective struct {
  25. Pairs map[int]string
  26. }
  27. func RetrieveScanDirectives() PortScanDirective {
  28. return PortScanDirective{Pairs: PORT_MAP}
  29. }
  30. /*
  31. Scans a single host on a single port
  32. :param addr: the address to dial
  33. :param port: the port number to dial
  34. */
  35. func singlePortScan(addr string, port int, svcs string) *PortScanResult {
  36. //defer wg.Done()
  37. address := fmt.Sprintf("%v:%d", addr, port)
  38. conn, err := net.DialTimeout("tcp", address, 1*time.Second)
  39. if err != nil {
  40. return &PortScanResult{PortNumber: port, Protocol: "tcp", Service: svcs, Listening: false}
  41. }
  42. conn.Close()
  43. return &PortScanResult{PortNumber: port, Protocol: "tcp", Service: svcs, Listening: true}
  44. }
  45. /*
  46. Perform a TCP scan on the pointers host
  47. :param posts: a list of ports to scan against a host
  48. */
  49. func (t *TcpScanHost) ScanPortRange(ports []int) []*PortScanResult {
  50. wg := &sync.WaitGroup{}
  51. out := make(chan *PortScanResult)
  52. var res []*PortScanResult
  53. wgOuter := &sync.WaitGroup{}
  54. wgOuter.Add(1)
  55. go func() {
  56. defer wgOuter.Done()
  57. ports := RetrieveScanDirectives()
  58. for p, s := range ports.Pairs {
  59. wg.Add(1)
  60. port := p
  61. svcs := s
  62. go func() {
  63. out <- singlePortScan(t.Ipv4Address.String(), port, svcs)
  64. wg.Done()
  65. }()
  66. }
  67. wg.Wait()
  68. close(out)
  69. }()
  70. for result := range out {
  71. if !result.Listening {
  72. continue
  73. }
  74. res = append(res, result)
  75. }
  76. wgOuter.Wait()
  77. return res
  78. }
  79. /*
  80. Evaluate whether the host has an entry in the ARP table
  81. */
  82. func (h *TcpScanHost) IsOnline() bool {
  83. return false
  84. }