Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "flag"
  5. "fmt"
  6. "github.com/bronzdoc/gops/lib/util"
  7. "github.com/gosuri/uilive"
  8. "github.com/gosuri/uitable"
  9. "net"
  10. )
  11.  
  12. type Info struct {
  13. port int
  14. protocol string
  15. desc string
  16. }
  17.  
  18. func getProtocol(tcp, udp *bool) string {
  19. var protocol string
  20.  
  21. if *tcp && *udp {
  22. protocol = "all"
  23. } else if *tcp {
  24. protocol = "tcp"
  25. } else if *udp {
  26. protocol = "udp"
  27. } else {
  28. protocol = "all"
  29. }
  30. return protocol
  31. }
  32.  
  33. func scanTCP(host string, port int) int {
  34. conn, err := net.Dial("tcp", host)
  35. if err != nil {
  36. return -1
  37. }
  38. defer conn.Close()
  39. return port
  40. }
  41.  
  42. func scanUDP(host string, port int) int {
  43. serverAddr, err := net.ResolveUDPAddr("udp", host)
  44. if err != nil {
  45. util.LogError(err)
  46. return -1
  47. }
  48.  
  49. localAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
  50. if err != nil {
  51. util.LogError(err)
  52. return -1
  53. }
  54.  
  55. conn, err := net.DialUDP("udp", localAddr, serverAddr)
  56. if err != nil {
  57. util.LogError(err)
  58. return -1
  59. }
  60. defer conn.Close()
  61.  
  62. // Write 3 times to the udp socket and check
  63. // if there's any kind of error
  64. error_count := 0
  65. for i := 0; i <= 3; i++ {
  66. buf := []byte("0")
  67. _, err := conn.Write(buf)
  68. if err != nil {
  69. error_count++
  70. }
  71. }
  72.  
  73. if error_count > 0 {
  74. return -1
  75. }
  76. return port
  77. }
  78.  
  79. func displayScanInfo(host string, port int, protocol string, channel chan Info) {
  80. udpPortScanned := -1
  81. tcpPortScanned := -1
  82. var protocolDesc string
  83.  
  84. if protocol == "tcp" {
  85. tcpPortScanned = scanTCP(host, port)
  86. } else if protocol == "udp" {
  87. udpPortScanned = scanUDP(host, port)
  88. } else {
  89. tcpPortScanned = scanTCP(host, port)
  90. udpPortScanned = scanUDP(host, port)
  91. }
  92.  
  93. if tcpPortScanned != -1 || udpPortScanned != -1 {
  94. if tcpPortScanned == udpPortScanned {
  95. protocolDesc = "tcp/udp"
  96. } else if tcpPortScanned != -1 {
  97. protocolDesc = "tcp"
  98. } else if udpPortScanned != -1 {
  99. protocolDesc = "udp"
  100. }
  101. info := Info{
  102. port: port,
  103. protocol: protocolDesc,
  104. desc: func(port int) string { // Build protocol description
  105. desc := "(?)"
  106. if val, ok := util.CommonPorts[port]; ok {
  107. desc = val
  108. }
  109. return desc
  110. }(port),
  111. }
  112. // Send data to channel
  113. channel <- info
  114. }
  115. }
  116.  
  117. func gops() {
  118.  
  119. dataChan := make(chan Info)
  120.  
  121. protocol := getProtocol(&tcp, &udp)
  122.  
  123. table := uitable.New()
  124. table.MaxColWidth = 100
  125. table.AddRow("PORT", "PROTOCOL", "DESCRIPTION")
  126.  
  127. status := uilive.New()
  128. status.Start()
  129.  
  130. display := uilive.New()
  131. display.Start()
  132.  
  133. // Scan ports
  134. if port > 0 {
  135. host := fmt.Sprintf("%s:%d", host, port)
  136. fmt.Fprintf(status, "gops scanning port %d\n", port)
  137. go displayScanInfo(host, port, protocol, dataChan)
  138. for {
  139. go func() {
  140. info := <-dataChan
  141. table.AddRow(info.port, info.protocol, info.desc)
  142. }()
  143. }
  144. status.Flush()
  145. } else {
  146.  
  147. go func() {
  148. for port := start; port <= end; port++ {
  149. //fmt.Fprintf(status, "gops scanning...(%d%%)\n", int((float32(port)/float32(end))*100))
  150. //status.Flush()
  151. host := fmt.Sprintf("%s:%d", host, port)
  152. displayScanInfo(host, port, protocol, dataChan)
  153. }
  154. close(dataChan)
  155. }()
  156.  
  157. for info := range dataChan {
  158. table.AddRow(info.port, info.protocol, info.desc)
  159. fmt.Fprintf(display, "%s", table)
  160. }
  161. }
  162.  
  163. //fmt.Fprintf(status, "gops finished scanning (100%%)\n")
  164. //status.Stop()
  165.  
  166. fmt.Fprintf(display, "%s", table)
  167. display.Stop()
  168. }
  169.  
  170. var (
  171. host string
  172. tcp bool
  173. udp bool
  174. start int
  175. end int
  176. port int
  177. )
  178.  
  179. func main() {
  180. flag.StringVar(&host, "host", "127.0.0.1", "Specify host")
  181. flag.BoolVar(&tcp, "tcp", false, "Show only tcp ports open")
  182. flag.BoolVar(&udp, "udp", false, "Show only udp ports open")
  183. flag.IntVar(&start, "start", 0, "Port to start the scan")
  184. flag.IntVar(&end, "end", 65535, "Port to end the scan")
  185. flag.IntVar(&port, "port", 0, "Check if port is open")
  186.  
  187. flag.Parse()
  188. gops()
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement