scanner.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "sync"
  25. "time"
  26. )
  27. var PORT_MAP = map[int]string{
  28. 22: "ssh", 23: "telnet", 53: "dns", 80: "http", 25: "smtp", 443: "https", 8080: "unknown", 8081: "unknown",
  29. //8082: "unknown", 8085: "unknown", 8090: "unknown", 8091: "unknown", 9010: "unknown", 9012: "unknown", 10000: "unknown", 1433: "microsoft_sql",
  30. 3306: "mysql", 3050: "firebird", 5432: "postgres", 27017: "mongo", 6379: "redis", 8005: "tomcat", 6443: "kubernetes", 853: "dns-tls", 143: "imap",
  31. 389: "ldap", 445: "smb", 543: "kerberos", 544: "kerberos", 749: "kerberos", 760: "kerberos",
  32. }
  33. /*
  34. Need to work with with a database schema in mind, and revolve functionality around that
  35. */
  36. type Host struct {
  37. Fqdn string // The FQDN of the address targeted as per the systems default resolver
  38. IpAddress string // the IPv4 address (no ipv6 support yet)
  39. PingResponse bool // boolean value representing if the host responded to ICMP
  40. ListeningPorts map[int]string // list of maps depicting a port number -> service name
  41. }
  42. /*
  43. Perform a concurrent TCP port dial on a host, either by domain name or IP.
  44. :param addr: the address of fqdn to scan
  45. :param portmap: a key/value pair of port numbers to service names to dial the host with
  46. */
  47. func PortWalk(addr string, portmap map[int]string) *Host {
  48. wg := &sync.WaitGroup{}
  49. out := []*PortScanResult{}
  50. for p, s := range portmap {
  51. wg.Add(1)
  52. go func(target string, p int, s string) {
  53. defer wg.Done()
  54. out = append(out, singlePortScan(target, p, s))
  55. }(addr, p, s)
  56. }
  57. wg.Wait()
  58. host := &Host{IpAddress: addr, ListeningPorts: map[int]string{}}
  59. for i := range out {
  60. if out[i].Listening {
  61. host.ListeningPorts[out[i].PortNumber] = out[i].Service
  62. }
  63. }
  64. return host
  65. }
  66. type PortScanResult struct {
  67. // This is used to represent the results of a port scan against one host
  68. PortNumber int `json:"port_number"` // The port number that was scanned
  69. Service string `json:"service"` // the name of the service that the port was identified/mapped to
  70. Protocol string `json:"protocol"` // The IP protocol (TCP/UDP)
  71. Listening bool `json:"listening"` // A boolean value that depicts if the service is listening or not
  72. }
  73. type PortScanDirective struct {
  74. // Struct for dependency injecting the dynamic port map used for scans
  75. Pairs map[int]string
  76. }
  77. /*
  78. Wrapper function to dependency inject the resource for a port -> service name mapping.
  79. May move to a database, or something.
  80. */
  81. func RetrieveScanDirectives() PortScanDirective {
  82. return PortScanDirective{Pairs: PORT_MAP}
  83. }
  84. /*
  85. Scans a single host on a single port
  86. :param addr: the address to dial
  87. :param port: the port number to dial
  88. :param svcs: the name of the service that the port is associate with
  89. */
  90. func singlePortScan(addr string, port int, svcs string) *PortScanResult {
  91. address := fmt.Sprintf("%v:%d", addr, port)
  92. conn, err := net.DialTimeout("tcp", address, 5*time.Second)
  93. if err != nil {
  94. return &PortScanResult{PortNumber: port, Protocol: "tcp", Service: svcs, Listening: false}
  95. }
  96. conn.Close()
  97. return &PortScanResult{PortNumber: port, Protocol: "tcp", Service: svcs, Listening: true}
  98. }