scanner.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. GNU GENERAL PUBLIC LICENSE
  3. Version 3, 29 June 2007
  4. kyoketsu, a Client-To-Client Network Enumeration System
  5. Copyright (C) 2024 Russell Hrubesky, ChiralWorks Software LLC
  6. Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
  7. Everyone is permitted to copy and distribute verbatim copies
  8. of this license document, but changing it is not allowed.
  9. This program is free software: you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation, either version 3 of the License,
  12. or (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. See the GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package kyoketsu
  21. import (
  22. "fmt"
  23. "net"
  24. "strconv"
  25. "strings"
  26. "sync"
  27. "time"
  28. )
  29. /*
  30. Need to work with with a database schema in mind, and revolve functionality around that
  31. */
  32. type Host struct {
  33. Fqdn string // The FQDN of the address targeted as per the systems default resolver
  34. IpAddress string // the IPv4 address (no ipv6 support yet)
  35. PingResponse bool // boolean value representing if the host responded to ICMP
  36. ListeningPorts map[int]string // list of maps depicting a port number -> service name
  37. PortString string
  38. Id int64
  39. }
  40. /*
  41. Perform a concurrent TCP port dial on a host, either by domain name or IP.
  42. :param addr: the address of fqdn to scan
  43. :param portmap: a key/value pair of port numbers to service names to dial the host with
  44. */
  45. func PortWalk(addr string, portmap map[int]string) Host {
  46. wg := &sync.WaitGroup{}
  47. mu := &sync.Mutex{}
  48. out := map[int]string{}
  49. for p, s := range portmap {
  50. wg.Add(1)
  51. go func(target string, p int, s string, mu *sync.Mutex) {
  52. defer wg.Done()
  53. scanout := singlePortScan(target, p, s)
  54. if scanout.Listening {
  55. mu.Lock()
  56. out[scanout.PortNumber] = scanout.Service
  57. mu.Unlock()
  58. }
  59. }(addr, p, s, mu)
  60. }
  61. wg.Wait()
  62. var dnames string
  63. var portstring string
  64. dns, _ := net.LookupAddr(addr)
  65. dnames = strings.Join(dns, ", ")
  66. for key, _ := range out {
  67. portstring = portstring + "," + strconv.Itoa(key)
  68. }
  69. return Host{
  70. IpAddress: addr,
  71. Fqdn: dnames,
  72. PingResponse: false,
  73. ListeningPorts: out,
  74. PortString: portstring,
  75. }
  76. }
  77. type PortScanResult struct {
  78. // This is used to represent the results of a port scan against one host
  79. PortNumber int `json:"port_number"` // The port number that was scanned
  80. Service string `json:"service"` // the name of the service that the port was identified/mapped to
  81. Protocol string `json:"protocol"` // The IP protocol (TCP/UDP)
  82. Listening bool `json:"listening"` // A boolean value that depicts if the service is listening or not
  83. }
  84. /*
  85. Wrapper function to dependency inject the resource for a port -> service name mapping.
  86. May move to a database, or something.
  87. */
  88. func RetrieveScanDirectives() map[int]string {
  89. var portmap = map[int]string{
  90. 22: "ssh", 23: "telnet", 53: "dns", 80: "http", 25: "smtp", 443: "https", 8080: "unknown", 8081: "unknown",
  91. 8082: "unknown", 8085: "unknown", 8090: "unknown", 8091: "unknown", 9010: "unknown", 9012: "unknown", 10000: "unknown", 1433: "microsoft_sql",
  92. 3306: "mysql", 3050: "firebird", 5432: "postgres", 27017: "mongo", 6379: "redis", 8005: "tomcat", 6443: "kubernetes", 853: "dns-tls", 143: "imap",
  93. 389: "ldap", 445: "smb", 543: "kerberos", 544: "kerberos", 749: "kerberos", 760: "kerberos",
  94. }
  95. return portmap
  96. }
  97. /*
  98. Scans a single host on a single port
  99. :param addr: the address to dial
  100. :param port: the port number to dial
  101. :param svcs: the name of the service that the port is associate with
  102. */
  103. func singlePortScan(addr string, port int, svcs string) PortScanResult {
  104. address := fmt.Sprintf("%v:%d", addr, port)
  105. _, err := net.DialTimeout("tcp", address, 2*time.Second)
  106. if err != nil {
  107. return PortScanResult{PortNumber: port, Protocol: "tcp", Service: svcs, Listening: false}
  108. }
  109. return PortScanResult{PortNumber: port, Protocol: "tcp", Service: svcs, Listening: true}
  110. }
  111. /*
  112. Perform a port scan sweep across an entire subnet
  113. :param ip: the IPv4 address WITH CIDR notation
  114. :param portmap: the mapping of ports to scan with (port number mapped to protocol name)
  115. */
  116. func NetSweep(ips []net.IP, portmap map[int]string, scanned chan Host) {
  117. wg := &sync.WaitGroup{}
  118. for i := range ips {
  119. wg.Add(1)
  120. go func(target string, wg *sync.WaitGroup) {
  121. defer wg.Done()
  122. scanned <- PortWalk(target, portmap)
  123. }(ips[i].String(), wg)
  124. }
  125. wg.Wait()
  126. }