scanner.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. "log"
  24. "net"
  25. "strings"
  26. "sync"
  27. "syscall"
  28. "time"
  29. )
  30. /*
  31. Need to work with with a database schema in mind, and revolve functionality around that
  32. */
  33. type Host struct {
  34. Fqdn string // The FQDN of the address targeted as per the systems default resolver
  35. IpAddress string // the IPv4 address (no ipv6 support yet)
  36. PingResponse bool // boolean value representing if the host responded to ICMP
  37. ListeningPorts []int // list of maps depicting a port number -> service name
  38. PortString string
  39. Id int64
  40. }
  41. /*
  42. Perform a concurrent TCP port dial on a host, either by domain name or IP.
  43. :param addr: the address of fqdn to scan
  44. :param ports a list of port numbers to dial the host with
  45. */
  46. func PortWalk(addr string, ports []int) []int {
  47. out := []int{}
  48. for i := range ports {
  49. p := singlePortScan(addr, ports[i])
  50. if p != 0 {
  51. out = append(out, p)
  52. }
  53. }
  54. return out
  55. }
  56. type PortScanResult struct {
  57. // This is used to represent the results of a port scan against one host
  58. PortNumber int `json:"port_number"` // The port number that was scanned
  59. Service string `json:"service"` // the name of the service that the port was identified/mapped to
  60. Protocol string `json:"protocol"` // The IP protocol (TCP/UDP)
  61. Listening bool `json:"listening"` // A boolean value that depicts if the service is listening or not
  62. }
  63. /*
  64. Wrapper function to dependency inject the resource for a port -> service name mapping.
  65. May move to a database, or something.
  66. */
  67. func RetrieveScanDirectives() []int {
  68. var portmap = []int{22, 443, 8080, 4379, 445, 53, 153, 27017}
  69. /*map[int]string{
  70. 22: "ssh", 23: "telnet", 53: "dns", 80: "http", 25: "smtp", 443: "https", 8080: "unknown", 8081: "unknown",
  71. 8082: "unknown", 8085: "unknown", 8090: "unknown", 8091: "unknown", 9010: "unknown", 9012: "unknown", 10000: "unknown", 1433: "microsoft_sql",
  72. 3306: "mysql", 3050: "firebird", 5432: "postgres", 27017: "mongo", 6379: "redis", 8005: "tomcat", 6443: "kubernetes", 853: "dns-tls", 143: "imap",
  73. 389: "ldap", 445: "smb", 543: "kerberos", 544: "kerberos", 749: "kerberos", 760: "kerberos",
  74. } */
  75. return portmap
  76. }
  77. /*
  78. Scans a single host on a single port
  79. :param addr: the address to dial
  80. :param port: the port number to dial
  81. :param svcs: the name of the service that the port is associate with
  82. */
  83. func singlePortScan(addr string, port int) int {
  84. conn, err := net.DialTimeout("tcp", fmt.Sprintf("%v:%d", addr, port), 2*time.Second)
  85. if err != nil {
  86. return 0
  87. // return PortScanResult{PortNumber: port, Protocol: "tcp", Listening: false}
  88. }
  89. conn.Close()
  90. return port
  91. //return PortScanResult{PortNumber: port, Protocol: "tcp", Listening: true}
  92. }
  93. /*
  94. Perform a port scan sweep across an entire subnet
  95. :param ip: the IPv4 address WITH CIDR notation
  96. :param portmap: the mapping of ports to scan with (port number mapped to protocol name)
  97. */
  98. func NetSweep(ips []net.IP, ports []int, scanned chan Host) {
  99. wg := &sync.WaitGroup{}
  100. for i := range ips {
  101. wg.Add(1)
  102. go func(target string, portnum []int, wgrp *sync.WaitGroup) {
  103. defer wgrp.Done()
  104. portscanned := PortWalk(target, portnum)
  105. scanned <- Host{
  106. Fqdn: getFqdn(target),
  107. IpAddress: target,
  108. ListeningPorts: portscanned,
  109. PortString: strings.Trim(strings.Join(strings.Fields(fmt.Sprint(portscanned)), ","), "[]"),
  110. }
  111. }(ips[i].String(), ports, wg)
  112. }
  113. wg.Wait()
  114. close(scanned)
  115. }
  116. func getFqdn(ip string) string {
  117. names, err := net.LookupAddr(ip)
  118. if err != nil {
  119. return "not found with default resolver"
  120. }
  121. return strings.Join(names, ", ")
  122. }
  123. /*
  124. Create a new TCP dialer to share in a goroutine
  125. */
  126. func NewDialer() net.Dialer {
  127. return net.Dialer{}
  128. }
  129. /*
  130. Create a new low level networking interface socket
  131. :param intf: the name of the interface to bind the socket to
  132. */
  133. func NewTCPSock(interfaceName string) *syscall.SockaddrLinklayer {
  134. sock, err := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_RAW, syscall.ETH_P_IP)
  135. if err != nil {
  136. log.Fatal(err, " Could not create raw AF_PACKET socket.\n")
  137. }
  138. defer syscall.Close(sock)
  139. intf, err := net.InterfaceByName(interfaceName)
  140. if err != nil {
  141. log.Fatal(err, " Couldnt locate that interface. Are you sure you mean to pass ", interfaceName, " ?")
  142. }
  143. return &syscall.SockaddrLinklayer{
  144. Protocol: htons(syscall.ETH_P_IP),
  145. Ifindex: intf.Index,
  146. }
  147. }
  148. // htons converts a uint16 from host- to network byte order.
  149. func htons(i uint16) uint16 {
  150. return (i<<8)&0xff00 | i>>8
  151. }