scanner.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. for p, s := range portmap {
  37. wg.Add(1)
  38. go func(target string, p int, s string) {
  39. defer wg.Done()
  40. out = append(out, singlePortScan(target, p, s))
  41. }(addr, p, s)
  42. }
  43. wg.Wait()
  44. host := &Host{IpAddress: addr, ListeningPorts: map[int]string{}}
  45. for i := range out {
  46. if out[i].Listening {
  47. host.ListeningPorts[out[i].PortNumber] = out[i].Service
  48. }
  49. }
  50. return host
  51. }
  52. type PortScanResult struct {
  53. // This is used to represent the results of a port scan against one host
  54. PortNumber int `json:"port_number"` // The port number that was scanned
  55. Service string `json:"service"` // the name of the service that the port was identified/mapped to
  56. Protocol string `json:"protocol"` // The IP protocol (TCP/UDP)
  57. Listening bool `json:"listening"` // A boolean value that depicts if the service is listening or not
  58. }
  59. type PortScanDirective struct {
  60. // Struct for dependency injecting the dynamic port map used for scans
  61. Pairs map[int]string
  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() PortScanDirective {
  68. return PortScanDirective{Pairs: PORT_MAP}
  69. }
  70. /*
  71. Scans a single host on a single port
  72. :param addr: the address to dial
  73. :param port: the port number to dial
  74. :param svcs: the name of the service that the port is associate with
  75. */
  76. func singlePortScan(addr string, port int, svcs string) *PortScanResult {
  77. address := fmt.Sprintf("%v:%d", addr, port)
  78. conn, err := net.DialTimeout("tcp", address, 5*time.Second)
  79. if err != nil {
  80. return &PortScanResult{PortNumber: port, Protocol: "tcp", Service: svcs, Listening: false}
  81. }
  82. conn.Close()
  83. return &PortScanResult{PortNumber: port, Protocol: "tcp", Service: svcs, Listening: true}
  84. }
  85. /*
  86. This function makes use of an external dependency, may or may not keep. It still needs to be implemented
  87. :param addr: the ip address to send an ICMP request to
  88. :param pinger: a pointer to a ping.Pinger struct to reuse
  89. */
  90. func PingTarget(addr string, pinger *ping.Pinger) bool {
  91. err := pinger.SetAddr(addr)
  92. if err != nil {
  93. log.Println(err)
  94. return false
  95. }
  96. err = pinger.Run()
  97. if err != nil {
  98. log.Println(err)
  99. return false
  100. }
  101. stats := pinger.Statistics()
  102. if stats.PacketsRecv > 0 {
  103. return true
  104. }
  105. return false
  106. }
  107. /*
  108. Listen for ICMP responses on a specific address
  109. :param listening: the address of your server
  110. */
  111. func IcmpListen(listening string) *icmp.PacketConn {
  112. icmpSrv, err := icmp.ListenPacket("udp4", listening)
  113. icmpSrv.SetDeadline(time.Now().Add(5 * time.Second))
  114. if err != nil {
  115. log.Println(err)
  116. }
  117. return icmpSrv
  118. }
  119. /*
  120. Send an ICMP request to an address and listen for a response (UNFINISHED/NON-FUNCTIONAL)
  121. :param icmpSrv: a pointer to an ICMP PacketConn struct, for reading and sending ICMP requests.
  122. :param addr: the address to send the request to
  123. */
  124. func icmpAsk(icmpSrv *icmp.PacketConn, addr string) bool {
  125. icmpReq := icmp.Message{
  126. Type: ipv4.ICMPTypeEcho, Code: 0,
  127. Body: &icmp.Echo{
  128. ID: os.Getpid() & 0xffff, Seq: 1,
  129. Data: []byte("DIALING FROM KYOKETSU"),
  130. },
  131. }
  132. icmpB, err := icmpReq.Marshal(nil)
  133. if err != nil {
  134. log.Println(err)
  135. return false
  136. }
  137. if _, err := icmpSrv.WriteTo(icmpB, &net.UDPAddr{IP: net.ParseIP(addr)}); err != nil {
  138. log.Println(err)
  139. return false
  140. }
  141. respB := make([]byte, 1500)
  142. n, peer, err := icmpSrv.ReadFrom(respB)
  143. if err != nil {
  144. log.Println(err)
  145. return false
  146. }
  147. rm, err := icmp.ParseMessage(ipv4.ICMPTypeEcho.Protocol(), respB[:n])
  148. if err != nil {
  149. log.Println(err)
  150. return false
  151. }
  152. switch rm.Type {
  153. case ipv4.ICMPTypeEchoReply:
  154. echo, ok := rm.Body.(*icmp.Echo)
  155. if !ok {
  156. return false
  157. }
  158. if peer.(*net.UDPAddr).IP.String() == addr && echo.Seq == 1 {
  159. return true
  160. }
  161. default:
  162. return false
  163. }
  164. return false
  165. }