scanner.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package kyoketsu
  2. import (
  3. "fmt"
  4. "log"
  5. "net"
  6. "os"
  7. "sync"
  8. "time"
  9. "github.com/go-ping/ping"
  10. "golang.org/x/net/icmp"
  11. "golang.org/x/net/ipv4"
  12. )
  13. var PORT_MAP = map[int]string{
  14. 22: "ssh", 23: "telnet", 53: "dns", 80: "http", 25: "smtp", 443: "https", 8080: "unknown", 8081: "unknown",
  15. //8082: "unknown", 8085: "unknown", 8090: "unknown", 8091: "unknown", 9010: "unknown", 9012: "unknown", 10000: "unknown", 1433: "microsoft_sql",
  16. 3306: "mysql", 3050: "firebird", 5432: "postgres", 27017: "mongo", 6379: "redis", 8005: "tomcat", 6443: "kubernetes", 853: "dns-tls", 143: "imap",
  17. 389: "ldap", 445: "smb", 543: "kerberos", 544: "kerberos", 749: "kerberos", 760: "kerberos",
  18. }
  19. /*
  20. Need to work with with a database schema in mind, and revolve functionality around that
  21. */
  22. type Host struct {
  23. Fqdn string // The FQDN of the address targeted as per the systems default resolver
  24. IpAddress string // the IPv4 address (no ipv6 support yet)
  25. PingResponse bool // boolean value representing if the host responded to ICMP
  26. ListeningPorts []map[int]string // list of maps depicting a port number -> service name
  27. }
  28. /*
  29. Perform a concurrent TCP port dial on a host, either by domain name or IP.
  30. :param addr: the address of fqdn to scan
  31. :param portmap: a key/value pair of port numbers to service names to dial the host with
  32. */
  33. func PortWalk(addr string, portmap map[int]string) *Host {
  34. wg := &sync.WaitGroup{}
  35. out := []*PortScanResult{}
  36. ports := RetrieveScanDirectives()
  37. for p, s := range ports.Pairs {
  38. wg.Add(1)
  39. port := p
  40. svcs := s
  41. go func() {
  42. defer wg.Done()
  43. out = append(out, singlePortScan(addr, port, svcs))
  44. }()
  45. }
  46. wg.Wait()
  47. host := &Host{IpAddress: addr, ListeningPorts: []map[int]string{}}
  48. for i := range out {
  49. if out[i].Listening {
  50. host.ListeningPorts = append(host.ListeningPorts, map[int]string{
  51. out[i].PortNumber: out[i].Service,
  52. })
  53. }
  54. }
  55. return host
  56. }
  57. type PortScanResult struct {
  58. // This is used to represent the results of a port scan against one host
  59. PortNumber int `json:"port_number"` // The port number that was scanned
  60. Service string `json:"service"` // the name of the service that the port was identified/mapped to
  61. Protocol string `json:"protocol"` // The IP protocol (TCP/UDP)
  62. Listening bool `json:"listening"` // A boolean value that depicts if the service is listening or not
  63. }
  64. type PortScanDirective struct {
  65. // Struct for dependency injecting the dynamic port map used for scans
  66. Pairs map[int]string
  67. }
  68. /*
  69. Wrapper function to dependency inject the resource for a port -> service name mapping.
  70. May move to a database, or something.
  71. */
  72. func RetrieveScanDirectives() PortScanDirective {
  73. return PortScanDirective{Pairs: PORT_MAP}
  74. }
  75. /*
  76. Scans a single host on a single port
  77. :param addr: the address to dial
  78. :param port: the port number to dial
  79. :param svcs: the name of the service that the port is associate with
  80. */
  81. func singlePortScan(addr string, port int, svcs string) *PortScanResult {
  82. address := fmt.Sprintf("%v:%d", addr, port)
  83. conn, err := net.DialTimeout("tcp", address, 5*time.Second)
  84. if err != nil {
  85. return &PortScanResult{PortNumber: port, Protocol: "tcp", Service: svcs, Listening: false}
  86. }
  87. conn.Close()
  88. return &PortScanResult{PortNumber: port, Protocol: "tcp", Service: svcs, Listening: true}
  89. }
  90. /*
  91. This function makes use of an external dependency, may or may not keep. It still needs to be implemented
  92. :param addr: the ip address to send an ICMP request to
  93. :param pinger: a pointer to a ping.Pinger struct to reuse
  94. */
  95. func PingTarget(addr string, pinger *ping.Pinger) bool {
  96. err := pinger.SetAddr(addr)
  97. if err != nil {
  98. log.Println(err)
  99. return false
  100. }
  101. err = pinger.Run()
  102. if err != nil {
  103. log.Println(err)
  104. return false
  105. }
  106. stats := pinger.Statistics()
  107. if stats.PacketsRecv > 0 {
  108. return true
  109. }
  110. return false
  111. }
  112. /*
  113. Listen for ICMP responses on a specific address
  114. :param listening: the address of your server
  115. */
  116. func IcmpListen(listening string) *icmp.PacketConn {
  117. icmpSrv, err := icmp.ListenPacket("udp4", listening)
  118. icmpSrv.SetDeadline(time.Now().Add(5 * time.Second))
  119. if err != nil {
  120. log.Println(err)
  121. }
  122. return icmpSrv
  123. }
  124. /*
  125. Send an ICMP request to an address and listen for a response (UNFINISHED/NON-FUNCTIONAL)
  126. :param icmpSrv: a pointer to an ICMP PacketConn struct, for reading and sending ICMP requests.
  127. :param addr: the address to send the request to
  128. */
  129. func icmpAsk(icmpSrv *icmp.PacketConn, addr string) bool {
  130. icmpReq := icmp.Message{
  131. Type: ipv4.ICMPTypeEcho, Code: 0,
  132. Body: &icmp.Echo{
  133. ID: os.Getpid() & 0xffff, Seq: 1,
  134. Data: []byte("DIALING FROM KYOKETSU"),
  135. },
  136. }
  137. icmpB, err := icmpReq.Marshal(nil)
  138. if err != nil {
  139. log.Println(err)
  140. return false
  141. }
  142. if _, err := icmpSrv.WriteTo(icmpB, &net.UDPAddr{IP: net.ParseIP(addr)}); err != nil {
  143. log.Println(err)
  144. return false
  145. }
  146. respB := make([]byte, 1500)
  147. n, peer, err := icmpSrv.ReadFrom(respB)
  148. if err != nil {
  149. log.Println(err)
  150. return false
  151. }
  152. rm, err := icmp.ParseMessage(ipv4.ICMPTypeEcho.Protocol(), respB[:n])
  153. if err != nil {
  154. log.Println(err)
  155. return false
  156. }
  157. switch rm.Type {
  158. case ipv4.ICMPTypeEchoReply:
  159. echo, ok := rm.Body.(*icmp.Echo)
  160. if !ok {
  161. return false
  162. }
  163. if peer.(*net.UDPAddr).IP.String() == addr && echo.Seq == 1 {
  164. return true
  165. }
  166. default:
  167. return false
  168. }
  169. return false
  170. }