原版
/*
autor:its0nme
功能:web目录扫描器
*/
package main
import (
"bufio"
"flag"
"fmt"
"net/http"
"os"
"time"
"github.com/gookit/color"
)
func main() {
start := time.Now()
banner()
//接收用户输入,-u参数,得到host
u := flag.String("u", "请输入url", "-u <url>")
flag.Parse()
// fmt.Printf("Scanning:"+"\033[1;33;40m%s\033[0m\r\n", *u)
//上面这种写法只能在Linux下,或power shell下显示颜色,在Windows的cmd下是不显示的
//所以需要gookit/color这个库
color.Yellow.Println("Scanning:" + *u + "\n")
time.Sleep(3 * time.Second)
//string类型的host传入,返回的切片类型的host+字典组合成完整url
url_slice := getdict(string(*u))
//url切片传入,发送url请求
// go httpget(url_slice)
httpget(url_slice)
//计算程序运行时间
end := time.Since(start)
fmt.Printf("程序运行花费时间:%s", end)
}
//banner
func banner() {
banner := `
gggggggggg gg gg gg ggg
ggg gg gg gg
ggg gg gg gggg
ggg gggggggggggg gggggggggggg gg gg
ggg gggggggg gg gg gg gg gg gg
ggg gggg gg gg gg gg gg gg
ggg gggg gg gg gg gg gg gg
gggggggggggggggg gggggggggggg gggggggggggg gg gg
默认使用程序根目录字典dir.txt
Useage: godir.exe -u https://www.baidu.com
`
color.Green.Println(banner)
}
//函数getdict的作用是把host和字典拼接起来,组成完整url。
//传入host,读取字典文件,返回string类型的url
func getdict(u string) []string {
//返回文件的指针
file, _ := os.Open("DIR.txt")
//按行读文件
scanner := bufio.NewScanner(file)
//参数u_dicc用于接收host+dicc的拼接结果
var u_dicc []string
for scanner.Scan() {
uu := u + "/" + scanner.Text()
u_dicc = append(u_dicc, uu)
}
return u_dicc
}
//http请求,传入完整url,无需返回
func httpget(h []string) {
//i参数用于计算进度条,u则是url
for i, u := range h {
req, err := http.Get(u)
//输入的url格式不对,则提示正确格式。
if err != nil {
fmt.Println(" 请求出错啦!")
return
//不同的响应码,定义不同的颜色显示,直观。
} else if req.StatusCode != 404 { //404不打印
if req.StatusCode == 200 {
//绿色
s200 := "200 " + u
color.Green.Println(s200)
fmt.Fprintf(os.Stdout, "Process:%d/%d\r\n", i, len(h))
} else if req.StatusCode == 403 {
//红色
s403 := "403 " + u
color.Red.Println(s403)
fmt.Fprintf(os.Stdout, "Process:%d/%d\r\n", i, len(h))
} else if req.StatusCode == 500 {
//深蓝色
s500 := "500 " + u
color.Blue.Println(s500)
fmt.Fprintf(os.Stdout, "Process:%d/%d\r\n", i, len(h))
} else if req.StatusCode == 400 {
//白色
s400 := "400 " + u
color.White.Println(s400)
fmt.Fprintf(os.Stdout, "Process:%d/%d\r\n", i, len(h))
} else if req.StatusCode == 302 {
//Tiffany蓝
s302 := "302 " + u
color.Cyan.Println(s302)
fmt.Fprintf(os.Stdout, "Process:%d/%d\r\n", i, len(h))
} else {
color.Black.Println(string(req.StatusCode) + " " + u)
fmt.Fprintf(os.Stdout, "Process:%d/%d\r\n", i, len(h))
}
}
}
}
优化
使用head方法来请求,不需要响应体,速度快很多。
修复遇到特殊字符导致中断(其实就是直接跳过)。
优化进度条。
/*
autor:于勒
功能:web目录扫描器
*/
package main
import (
"bufio"
"flag"
"fmt"
"net/http"
"os"
"time"
"github.com/gookit/color"
)
//banner
func banner() {
banner := `
gggggggggg gg gg gg ggg
ggg gg gg gg
ggg gg gg gggg
ggg gggggggggggg gggggggggggg gg gg
ggg gggggggg gg gg gg gg gg gg
ggg gggg gg gg gg gg gg gg
ggg gggg gg gg gg gg gg gg
gggggggggggggggg gggggggggggg gggggggggggg gg gg
默认使用程序根目录字典dir.txt
Useage: godir.exe -u https://www.baidu.com
`
color.Green.Println(banner)
}
//函数getdict的作用是把host和字典拼接起来,组成完整url。
//传入host,读取字典文件,返回string类型的url
func getdict(u string) []string {
//返回文件的指针
file, _ := os.Open("DIR.txt")
//按行读文件
scanner := bufio.NewScanner(file)
//参数u_dicc用于接收host+dicc的拼接结果
var u_dicc []string
for scanner.Scan() {
uu := u + "/" + scanner.Text()
u_dicc = append(u_dicc, uu)
}
return u_dicc
}
//http请求,传入完整url,无需返回
func httpget(h []string) {
//i参数用于计算进度条,u则是url
for i, u := range h {
req, err := http.Head(u)
// defer req.Body.Close()
fmt.Fprintf(os.Stdout, "Process: %d/%d\r", i, len(h))
//输入的url格式不对,则提示正确格式。
if err != nil {
fmt.Println("url请求出错: ", err)
continue
//不同的响应码,定义不同的颜色显示,直观。
} else if req.StatusCode != 404 { //404不打印
if req.StatusCode == 200 {
//绿色
s200 := "200-----" + u
color.Green.Println(s200)
} else if req.StatusCode == 403 {
//红色
s403 := "403-----" + u
color.Red.Println(s403)
} else if req.StatusCode == 500 {
//深蓝色
s500 := "500-----" + u
color.Blue.Println(s500)
} else if req.StatusCode == 400 {
//白色
s400 := "400-----" + u
color.White.Println(s400)
} else if req.StatusCode == 302 {
//Tiffany蓝
s302 := "302-----" + u
color.Cyan.Println(s302)
} else {
color.Black.Println(string(req.StatusCode) + "-----" + u)
}
}
}
}
func main() {
start := time.Now()
banner() //输出banner信息
//接收用户输入,-u参数,得到host
u := flag.String("u", "请输入url", "-u <url>")
flag.Parse()
// fmt.Printf("Scanning:"+"\033[1;33;40m%s\033[0m\r\n", *u)
//上面这种写法只能在Linux下,或power shell下显示颜色,在Windows的cmd下是不显示的
//所以需要gookit/color这个库
color.Yellow.Println("Scanning:" + *u + "\n")
time.Sleep(3 * time.Second)
//string类型的host传入,返回的切片类型的host+字典组合成完整url
url_slice := getdict(string(*u))
//url切片传入,发送url请求
// go httpget(url_slice)
httpget(url_slice)
//计算程序运行时间
end := time.Since(start)
fmt.Printf("程序运行花费时间:%s", end)
}