fscan/Plugins/WebTitle.go

258 lines
6.1 KiB
Go
Raw Normal View History

2020-12-29 17:17:10 +08:00
package Plugins
import (
2021-05-12 10:57:12 +08:00
"compress/gzip"
2021-09-10 22:43:50 +08:00
"crypto/tls"
2020-12-29 17:17:10 +08:00
"fmt"
2024-12-18 21:56:08 +08:00
"github.com/shadow1ng/fscan/Config"
2021-05-12 10:57:12 +08:00
"io"
2020-12-29 17:17:10 +08:00
"net/http"
"net/url"
2020-12-29 17:17:10 +08:00
"regexp"
"strings"
2021-09-10 22:43:50 +08:00
"time"
2022-02-17 14:37:06 +08:00
"unicode/utf8"
2023-06-05 19:02:55 +03:00
2024-12-18 22:00:18 +08:00
"github.com/shadow1ng/fscan/Common"
2023-06-05 19:02:55 +03:00
"github.com/shadow1ng/fscan/WebScan"
"github.com/shadow1ng/fscan/WebScan/lib"
"golang.org/x/text/encoding/simplifiedchinese"
)
2024-12-18 21:56:08 +08:00
func WebTitle(info *Config.HostInfo) error {
2024-12-18 22:00:18 +08:00
if Common.Scantype == "webpoc" {
2023-11-13 11:27:34 +08:00
WebScan.WebScan(info)
return nil
}
2023-11-13 11:27:34 +08:00
err, CheckData := GOWebTitle(info)
2022-02-17 14:37:06 +08:00
info.Infostr = WebScan.InfoCheck(info.Url, &CheckData)
2024-10-25 16:41:19 +08:00
//不扫描打印机,避免打纸
for _, v := range info.Infostr {
if v == "打印机" {
return nil
}
}
2024-12-18 22:00:18 +08:00
if !Common.NoPoc && err == nil {
2023-11-13 11:27:34 +08:00
WebScan.WebScan(info)
2022-02-17 14:37:06 +08:00
} else {
2021-03-30 18:12:54 +08:00
errlog := fmt.Sprintf("[-] webtitle %v %v", info.Url, err)
2024-12-18 22:00:18 +08:00
Common.LogError(errlog)
2021-03-30 18:12:54 +08:00
}
return err
}
2024-12-18 21:56:08 +08:00
func GOWebTitle(info *Config.HostInfo) (err error, CheckData []WebScan.CheckDatas) {
if info.Url == "" {
2022-02-17 14:37:06 +08:00
switch info.Ports {
case "80":
info.Url = fmt.Sprintf("http://%s", info.Host)
2022-02-17 14:37:06 +08:00
case "443":
info.Url = fmt.Sprintf("https://%s", info.Host)
2022-02-17 14:37:06 +08:00
default:
2021-09-10 22:43:50 +08:00
host := fmt.Sprintf("%s:%s", info.Host, info.Ports)
2024-12-18 22:00:18 +08:00
protocol := GetProtocol(host, Common.Timeout)
2021-09-10 22:43:50 +08:00
info.Url = fmt.Sprintf("%s://%s:%s", protocol, info.Host, info.Ports)
}
2020-12-29 17:17:10 +08:00
} else {
if !strings.Contains(info.Url, "://") {
2022-02-17 14:37:06 +08:00
host := strings.Split(info.Url, "/")[0]
2024-12-18 22:00:18 +08:00
protocol := GetProtocol(host, Common.Timeout)
2021-09-10 22:43:50 +08:00
info.Url = fmt.Sprintf("%s://%s", protocol, info.Url)
}
2020-12-29 17:17:10 +08:00
}
2021-09-10 22:43:50 +08:00
2023-11-13 11:27:34 +08:00
err, result, CheckData := geturl(info, 1, CheckData)
2021-05-14 16:02:22 +08:00
if err != nil && !strings.Contains(err.Error(), "EOF") {
2022-02-17 14:37:06 +08:00
return
2020-12-29 17:17:10 +08:00
}
2021-09-10 20:32:51 +08:00
2023-11-13 11:27:34 +08:00
//有跳转
if strings.Contains(result, "://") {
2022-02-17 14:37:06 +08:00
info.Url = result
2023-11-13 11:27:34 +08:00
err, result, CheckData = geturl(info, 3, CheckData)
2022-02-17 14:37:06 +08:00
if err != nil {
return
}
}
2021-09-10 22:43:50 +08:00
if result == "https" && !strings.HasPrefix(info.Url, "https://") {
2021-05-14 16:02:22 +08:00
info.Url = strings.Replace(info.Url, "http://", "https://", 1)
2023-11-13 11:27:34 +08:00
err, result, CheckData = geturl(info, 1, CheckData)
//有跳转
if strings.Contains(result, "://") {
2022-02-17 14:37:06 +08:00
info.Url = result
2023-11-13 11:27:34 +08:00
err, _, CheckData = geturl(info, 3, CheckData)
if err != nil {
2022-02-17 14:37:06 +08:00
return
}
2020-12-29 17:17:10 +08:00
}
}
2023-11-13 11:27:34 +08:00
//是否访问图标
//err, _, CheckData = geturl(info, 2, CheckData)
if err != nil {
2022-02-17 14:37:06 +08:00
return
2020-12-29 17:17:10 +08:00
}
2022-02-17 14:37:06 +08:00
return
2020-12-29 17:17:10 +08:00
}
2024-12-18 21:56:08 +08:00
func geturl(info *Config.HostInfo, flag int, CheckData []WebScan.CheckDatas) (error, string, []WebScan.CheckDatas) {
2022-02-17 14:37:06 +08:00
//flag 1 first try
//flag 2 /favicon.ico
//flag 3 302
//flag 4 400 -> https
Url := info.Url
if flag == 2 {
URL, err := url.Parse(Url)
if err == nil {
Url = fmt.Sprintf("%s://%s/favicon.ico", URL.Scheme, URL.Host)
} else {
Url += "/favicon.ico"
}
}
req, err := http.NewRequest("GET", Url, nil)
2022-02-17 14:37:06 +08:00
if err != nil {
return err, "", CheckData
}
2024-12-18 22:00:18 +08:00
req.Header.Set("User-agent", Common.UserAgent)
req.Header.Set("Accept", Common.Accept)
2022-02-17 14:37:06 +08:00
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")
2024-12-18 22:00:18 +08:00
if Common.Cookie != "" {
req.Header.Set("Cookie", Common.Cookie)
}
2024-12-18 22:00:18 +08:00
//if Common.Pocinfo.Cookie != "" {
// req.Header.Set("Cookie", "rememberMe=1;"+Common.Pocinfo.Cookie)
2023-11-13 11:27:34 +08:00
//} else {
// req.Header.Set("Cookie", "rememberMe=1")
//}
2022-02-17 14:37:06 +08:00
req.Header.Set("Connection", "close")
var client *http.Client
if flag == 1 {
client = lib.ClientNoRedirect
} else {
client = lib.Client
}
2022-02-17 14:37:06 +08:00
resp, err := client.Do(req)
if err != nil {
2021-05-14 16:02:22 +08:00
return err, "https", CheckData
2020-12-29 17:17:10 +08:00
}
2022-02-17 14:37:06 +08:00
defer resp.Body.Close()
var title string
body, err := getRespBody(resp)
if err != nil {
return err, "https", CheckData
}
2023-11-13 17:41:54 +08:00
CheckData = append(CheckData, WebScan.CheckDatas{body, fmt.Sprintf("%s", resp.Header)})
2022-02-17 14:37:06 +08:00
var reurl string
if flag != 2 {
2023-11-13 17:41:54 +08:00
if !utf8.Valid(body) {
body, _ = simplifiedchinese.GBK.NewDecoder().Bytes(body)
}
2022-02-17 14:37:06 +08:00
title = gettitle(body)
length := resp.Header.Get("Content-Length")
if length == "" {
length = fmt.Sprintf("%v", len(body))
}
redirURL, err1 := resp.Location()
if err1 == nil {
reurl = redirURL.String()
}
2023-11-13 12:42:02 +08:00
result := fmt.Sprintf("[*] WebTitle %-25v code:%-3v len:%-6v title:%v", resp.Request.URL, resp.StatusCode, length, title)
2022-02-17 14:37:06 +08:00
if reurl != "" {
2023-11-13 11:27:34 +08:00
result += fmt.Sprintf(" 跳转url: %s", reurl)
2022-02-17 14:37:06 +08:00
}
2024-12-18 22:00:18 +08:00
Common.LogSuccess(result)
2022-02-17 14:37:06 +08:00
}
if reurl != "" {
return nil, reurl, CheckData
}
if resp.StatusCode == 400 && !strings.HasPrefix(info.Url, "https") {
return nil, "https", CheckData
}
return nil, "", CheckData
}
2021-05-12 10:57:12 +08:00
func getRespBody(oResp *http.Response) ([]byte, error) {
var body []byte
if oResp.Header.Get("Content-Encoding") == "gzip" {
gr, err := gzip.NewReader(oResp.Body)
if err != nil {
return nil, err
}
defer gr.Close()
for {
buf := make([]byte, 1024)
n, err := gr.Read(buf)
if err != nil && err != io.EOF {
return nil, err
}
if n == 0 {
break
}
body = append(body, buf...)
}
} else {
2023-06-05 19:02:55 +03:00
raw, err := io.ReadAll(oResp.Body)
2021-05-12 10:57:12 +08:00
if err != nil {
return nil, err
}
body = raw
}
return body, nil
}
2021-09-10 22:43:50 +08:00
2022-02-17 14:37:06 +08:00
func gettitle(body []byte) (title string) {
2024-01-15 16:22:40 +08:00
re := regexp.MustCompile("(?ims)<title.*?>(.*?)</title>")
2022-02-17 14:37:06 +08:00
find := re.FindSubmatch(body)
if len(find) > 1 {
title = string(find[1])
title = strings.TrimSpace(title)
title = strings.Replace(title, "\n", "", -1)
title = strings.Replace(title, "\r", "", -1)
title = strings.Replace(title, "&nbsp;", " ", -1)
if len(title) > 100 {
title = title[:100]
}
2023-11-13 17:41:54 +08:00
if title == "" {
title = "\"\"" //空格
}
} else {
title = "None" //没有title
2022-02-17 14:37:06 +08:00
}
return
}
2023-11-13 11:27:34 +08:00
func GetProtocol(host string, Timeout int64) (protocol string) {
protocol = "http"
//如果端口是80或443,跳过Protocol判断
2022-02-17 14:37:06 +08:00
if strings.HasSuffix(host, ":80") || !strings.Contains(host, ":") {
2023-11-13 11:27:34 +08:00
return
} else if strings.HasSuffix(host, ":443") {
protocol = "https"
return
2022-02-17 14:37:06 +08:00
}
2024-12-18 22:00:18 +08:00
socksconn, err := Common.WrapperTcpWithTimeout("tcp", host, time.Duration(Timeout)*time.Second)
2022-05-08 02:19:41 +08:00
if err != nil {
return
}
2023-11-13 11:27:34 +08:00
conn := tls.Client(socksconn, &tls.Config{MinVersion: tls.VersionTLS10, InsecureSkipVerify: true})
2021-09-10 22:43:50 +08:00
defer func() {
if conn != nil {
2022-05-08 00:16:58 +08:00
defer func() {
if err := recover(); err != nil {
2024-12-18 22:00:18 +08:00
Common.LogError(err)
2022-05-08 00:16:58 +08:00
}
}()
2021-09-10 22:43:50 +08:00
conn.Close()
}
}()
2022-05-09 12:27:05 +08:00
conn.SetDeadline(time.Now().Add(time.Duration(Timeout) * time.Second))
err = conn.Handshake()
2021-09-10 22:43:50 +08:00
if err == nil || strings.Contains(err.Error(), "handshake failure") {
protocol = "https"
}
return protocol
}