原版

  1. /*
  2. autor:its0nme
  3. 功能:web目录扫描器
  4. */
  5. package main
  6. import (
  7. "bufio"
  8. "flag"
  9. "fmt"
  10. "net/http"
  11. "os"
  12. "time"
  13. "github.com/gookit/color"
  14. )
  15. func main() {
  16. start := time.Now()
  17. banner()
  18. //接收用户输入,-u参数,得到host
  19. u := flag.String("u", "请输入url", "-u <url>")
  20. flag.Parse()
  21. // fmt.Printf("Scanning:"+"\033[1;33;40m%s\033[0m\r\n", *u)
  22. //上面这种写法只能在Linux下,或power shell下显示颜色,在Windows的cmd下是不显示的
  23. //所以需要gookit/color这个库
  24. color.Yellow.Println("Scanning:" + *u + "\n")
  25. time.Sleep(3 * time.Second)
  26. //string类型的host传入,返回的切片类型的host+字典组合成完整url
  27. url_slice := getdict(string(*u))
  28. //url切片传入,发送url请求
  29. // go httpget(url_slice)
  30. httpget(url_slice)
  31. //计算程序运行时间
  32. end := time.Since(start)
  33. fmt.Printf("程序运行花费时间:%s", end)
  34. }
  35. //banner
  36. func banner() {
  37. banner := `
  38. gggggggggg gg gg gg ggg
  39. ggg gg gg gg
  40. ggg gg gg gggg
  41. ggg gggggggggggg gggggggggggg gg gg
  42. ggg gggggggg gg gg gg gg gg gg
  43. ggg gggg gg gg gg gg gg gg
  44. ggg gggg gg gg gg gg gg gg
  45. gggggggggggggggg gggggggggggg gggggggggggg gg gg
  46. 默认使用程序根目录字典dir.txt
  47. Useage: godir.exe -u https://www.baidu.com
  48. `
  49. color.Green.Println(banner)
  50. }
  51. //函数getdict的作用是把host和字典拼接起来,组成完整url。
  52. //传入host,读取字典文件,返回string类型的url
  53. func getdict(u string) []string {
  54. //返回文件的指针
  55. file, _ := os.Open("DIR.txt")
  56. //按行读文件
  57. scanner := bufio.NewScanner(file)
  58. //参数u_dicc用于接收host+dicc的拼接结果
  59. var u_dicc []string
  60. for scanner.Scan() {
  61. uu := u + "/" + scanner.Text()
  62. u_dicc = append(u_dicc, uu)
  63. }
  64. return u_dicc
  65. }
  66. //http请求,传入完整url,无需返回
  67. func httpget(h []string) {
  68. //i参数用于计算进度条,u则是url
  69. for i, u := range h {
  70. req, err := http.Get(u)
  71. //输入的url格式不对,则提示正确格式。
  72. if err != nil {
  73. fmt.Println(" 请求出错啦!")
  74. return
  75. //不同的响应码,定义不同的颜色显示,直观。
  76. } else if req.StatusCode != 404 { //404不打印
  77. if req.StatusCode == 200 {
  78. //绿色
  79. s200 := "200 " + u
  80. color.Green.Println(s200)
  81. fmt.Fprintf(os.Stdout, "Process:%d/%d\r\n", i, len(h))
  82. } else if req.StatusCode == 403 {
  83. //红色
  84. s403 := "403 " + u
  85. color.Red.Println(s403)
  86. fmt.Fprintf(os.Stdout, "Process:%d/%d\r\n", i, len(h))
  87. } else if req.StatusCode == 500 {
  88. //深蓝色
  89. s500 := "500 " + u
  90. color.Blue.Println(s500)
  91. fmt.Fprintf(os.Stdout, "Process:%d/%d\r\n", i, len(h))
  92. } else if req.StatusCode == 400 {
  93. //白色
  94. s400 := "400 " + u
  95. color.White.Println(s400)
  96. fmt.Fprintf(os.Stdout, "Process:%d/%d\r\n", i, len(h))
  97. } else if req.StatusCode == 302 {
  98. //Tiffany蓝
  99. s302 := "302 " + u
  100. color.Cyan.Println(s302)
  101. fmt.Fprintf(os.Stdout, "Process:%d/%d\r\n", i, len(h))
  102. } else {
  103. color.Black.Println(string(req.StatusCode) + " " + u)
  104. fmt.Fprintf(os.Stdout, "Process:%d/%d\r\n", i, len(h))
  105. }
  106. }
  107. }
  108. }

image.png

优化

使用head方法来请求,不需要响应体,速度快很多。
修复遇到特殊字符导致中断(其实就是直接跳过)。
优化进度条。

  1. /*
  2. autor:于勒
  3. 功能:web目录扫描器
  4. */
  5. package main
  6. import (
  7. "bufio"
  8. "flag"
  9. "fmt"
  10. "net/http"
  11. "os"
  12. "time"
  13. "github.com/gookit/color"
  14. )
  15. //banner
  16. func banner() {
  17. banner := `
  18. gggggggggg gg gg gg ggg
  19. ggg gg gg gg
  20. ggg gg gg gggg
  21. ggg gggggggggggg gggggggggggg gg gg
  22. ggg gggggggg gg gg gg gg gg gg
  23. ggg gggg gg gg gg gg gg gg
  24. ggg gggg gg gg gg gg gg gg
  25. gggggggggggggggg gggggggggggg gggggggggggg gg gg
  26. 默认使用程序根目录字典dir.txt
  27. Useage: godir.exe -u https://www.baidu.com
  28. `
  29. color.Green.Println(banner)
  30. }
  31. //函数getdict的作用是把host和字典拼接起来,组成完整url。
  32. //传入host,读取字典文件,返回string类型的url
  33. func getdict(u string) []string {
  34. //返回文件的指针
  35. file, _ := os.Open("DIR.txt")
  36. //按行读文件
  37. scanner := bufio.NewScanner(file)
  38. //参数u_dicc用于接收host+dicc的拼接结果
  39. var u_dicc []string
  40. for scanner.Scan() {
  41. uu := u + "/" + scanner.Text()
  42. u_dicc = append(u_dicc, uu)
  43. }
  44. return u_dicc
  45. }
  46. //http请求,传入完整url,无需返回
  47. func httpget(h []string) {
  48. //i参数用于计算进度条,u则是url
  49. for i, u := range h {
  50. req, err := http.Head(u)
  51. // defer req.Body.Close()
  52. fmt.Fprintf(os.Stdout, "Process: %d/%d\r", i, len(h))
  53. //输入的url格式不对,则提示正确格式。
  54. if err != nil {
  55. fmt.Println("url请求出错: ", err)
  56. continue
  57. //不同的响应码,定义不同的颜色显示,直观。
  58. } else if req.StatusCode != 404 { //404不打印
  59. if req.StatusCode == 200 {
  60. //绿色
  61. s200 := "200-----" + u
  62. color.Green.Println(s200)
  63. } else if req.StatusCode == 403 {
  64. //红色
  65. s403 := "403-----" + u
  66. color.Red.Println(s403)
  67. } else if req.StatusCode == 500 {
  68. //深蓝色
  69. s500 := "500-----" + u
  70. color.Blue.Println(s500)
  71. } else if req.StatusCode == 400 {
  72. //白色
  73. s400 := "400-----" + u
  74. color.White.Println(s400)
  75. } else if req.StatusCode == 302 {
  76. //Tiffany蓝
  77. s302 := "302-----" + u
  78. color.Cyan.Println(s302)
  79. } else {
  80. color.Black.Println(string(req.StatusCode) + "-----" + u)
  81. }
  82. }
  83. }
  84. }
  85. func main() {
  86. start := time.Now()
  87. banner() //输出banner信息
  88. //接收用户输入,-u参数,得到host
  89. u := flag.String("u", "请输入url", "-u <url>")
  90. flag.Parse()
  91. // fmt.Printf("Scanning:"+"\033[1;33;40m%s\033[0m\r\n", *u)
  92. //上面这种写法只能在Linux下,或power shell下显示颜色,在Windows的cmd下是不显示的
  93. //所以需要gookit/color这个库
  94. color.Yellow.Println("Scanning:" + *u + "\n")
  95. time.Sleep(3 * time.Second)
  96. //string类型的host传入,返回的切片类型的host+字典组合成完整url
  97. url_slice := getdict(string(*u))
  98. //url切片传入,发送url请求
  99. // go httpget(url_slice)
  100. httpget(url_slice)
  101. //计算程序运行时间
  102. end := time.Since(start)
  103. fmt.Printf("程序运行花费时间:%s", end)
  104. }

image.png