scanner.go 5.3 KB

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