finished round-trip pattern for RPC/client design

This commit is contained in:
moloch
2019-02-04 11:18:44 -06:00
parent c9cbc608f5
commit 35331acbb6
4 changed files with 98 additions and 13 deletions
+27
View File
@@ -21,6 +21,7 @@ func Init(app *grumble.App, rpc RPCServer) {
f.Int("k", "kill", -1, "kill a background job")
},
Run: func(ctx *grumble.Context) error {
fmt.Println()
jobs(ctx, rpc)
fmt.Println()
return nil
@@ -36,6 +37,7 @@ func Init(app *grumble.App, rpc RPCServer) {
f.Int("l", "lport", 8888, "tcp listen port")
},
Run: func(ctx *grumble.Context) error {
fmt.Println()
startMTLSListener(ctx, rpc)
fmt.Println()
return nil
@@ -50,6 +52,7 @@ func Init(app *grumble.App, rpc RPCServer) {
f.String("d", "domain", "", "parent domain to use for DNS C2")
},
Run: func(ctx *grumble.Context) error {
fmt.Println()
startDNSListener(ctx, rpc)
fmt.Println()
return nil
@@ -65,6 +68,7 @@ func Init(app *grumble.App, rpc RPCServer) {
f.Int("l", "lport", 31337, "tcp listen port")
},
Run: func(ctx *grumble.Context) error {
fmt.Println()
startMultiplayerMode(ctx, rpc)
fmt.Println()
return nil
@@ -81,6 +85,7 @@ func Init(app *grumble.App, rpc RPCServer) {
f.String("i", "interact", "", "interact with a sliver")
},
Run: func(ctx *grumble.Context) error {
fmt.Println()
sessions(ctx, rpc)
fmt.Println()
return nil
@@ -92,6 +97,7 @@ func Init(app *grumble.App, rpc RPCServer) {
Help: "Background an active session",
LongHelp: help.GetHelpFor(consts.BackgroundStr),
Run: func(ctx *grumble.Context) error {
fmt.Println()
background(ctx, rpc)
fmt.Println()
return nil
@@ -104,6 +110,7 @@ func Init(app *grumble.App, rpc RPCServer) {
LongHelp: help.GetHelpFor(consts.KillStr),
AllowArgs: true,
Run: func(ctx *grumble.Context) error {
fmt.Println()
kill(ctx, rpc)
fmt.Println()
return nil
@@ -116,6 +123,7 @@ func Init(app *grumble.App, rpc RPCServer) {
LongHelp: help.GetHelpFor(consts.InfoStr),
AllowArgs: true,
Run: func(ctx *grumble.Context) error {
fmt.Println()
info(ctx, rpc)
fmt.Println()
return nil
@@ -128,6 +136,7 @@ func Init(app *grumble.App, rpc RPCServer) {
LongHelp: help.GetHelpFor(consts.UseStr),
AllowArgs: true,
Run: func(ctx *grumble.Context) error {
fmt.Println()
use(ctx, rpc)
fmt.Println()
return nil
@@ -148,6 +157,7 @@ func Init(app *grumble.App, rpc RPCServer) {
f.String("s", "save", "", "directory/file to the binary to")
},
Run: func(ctx *grumble.Context) error {
fmt.Println()
generate(ctx, rpc)
fmt.Println()
return nil
@@ -166,6 +176,7 @@ func Init(app *grumble.App, rpc RPCServer) {
f.Int("i", "iterations", 1, "iterations of the encoder")
},
Run: func(ctx *grumble.Context) error {
fmt.Println()
msf(ctx, rpc)
fmt.Println()
return nil
@@ -185,6 +196,7 @@ func Init(app *grumble.App, rpc RPCServer) {
f.Int("i", "iterations", 1, "iterations of the encoder")
},
Run: func(ctx *grumble.Context) error {
fmt.Println()
inject(ctx, rpc)
fmt.Println()
return nil
@@ -201,6 +213,7 @@ func Init(app *grumble.App, rpc RPCServer) {
f.String("o", "owner", "", "filter based on owner")
},
Run: func(ctx *grumble.Context) error {
fmt.Println()
ps(ctx, rpc)
fmt.Println()
return nil
@@ -213,6 +226,7 @@ func Init(app *grumble.App, rpc RPCServer) {
LongHelp: help.GetHelpFor(consts.PingStr),
AllowArgs: true,
Run: func(ctx *grumble.Context) error {
fmt.Println()
ping(ctx, rpc)
fmt.Println()
return nil
@@ -224,6 +238,7 @@ func Init(app *grumble.App, rpc RPCServer) {
Help: "Get sliver pid",
LongHelp: help.GetHelpFor(consts.GetPIDStr),
Run: func(ctx *grumble.Context) error {
fmt.Println()
getPID(ctx, rpc)
fmt.Println()
return nil
@@ -235,6 +250,7 @@ func Init(app *grumble.App, rpc RPCServer) {
Help: "Get sliver UID",
LongHelp: help.GetHelpFor(consts.GetUIDStr),
Run: func(ctx *grumble.Context) error {
fmt.Println()
getUID(ctx, rpc)
fmt.Println()
return nil
@@ -246,6 +262,7 @@ func Init(app *grumble.App, rpc RPCServer) {
Help: "Get sliver GID",
LongHelp: help.GetHelpFor(consts.GetGIDStr),
Run: func(ctx *grumble.Context) error {
fmt.Println()
getGID(ctx, rpc)
fmt.Println()
return nil
@@ -257,6 +274,7 @@ func Init(app *grumble.App, rpc RPCServer) {
Help: "Get sliver user",
LongHelp: help.GetHelpFor(consts.WhoamiStr),
Run: func(ctx *grumble.Context) error {
fmt.Println()
whoami(ctx, rpc)
fmt.Println()
return nil
@@ -268,6 +286,7 @@ func Init(app *grumble.App, rpc RPCServer) {
Help: "List current directory",
LongHelp: help.GetHelpFor(consts.LsStr),
Run: func(ctx *grumble.Context) error {
fmt.Println()
ls(ctx, rpc)
fmt.Println()
return nil
@@ -280,6 +299,7 @@ func Init(app *grumble.App, rpc RPCServer) {
LongHelp: help.GetHelpFor(consts.RmStr),
AllowArgs: true,
Run: func(ctx *grumble.Context) error {
fmt.Println()
rm(ctx, rpc)
fmt.Println()
return nil
@@ -292,6 +312,7 @@ func Init(app *grumble.App, rpc RPCServer) {
LongHelp: help.GetHelpFor(consts.MkdirStr),
AllowArgs: true,
Run: func(ctx *grumble.Context) error {
fmt.Println()
mkdir(ctx, rpc)
fmt.Println()
return nil
@@ -304,6 +325,7 @@ func Init(app *grumble.App, rpc RPCServer) {
LongHelp: help.GetHelpFor(consts.CdStr),
AllowArgs: true,
Run: func(ctx *grumble.Context) error {
fmt.Println()
cd(ctx, rpc)
fmt.Println()
return nil
@@ -315,6 +337,7 @@ func Init(app *grumble.App, rpc RPCServer) {
Help: "Print working directory",
LongHelp: help.GetHelpFor(consts.PwdStr),
Run: func(ctx *grumble.Context) error {
fmt.Println()
pwd(ctx, rpc)
fmt.Println()
return nil
@@ -327,6 +350,7 @@ func Init(app *grumble.App, rpc RPCServer) {
LongHelp: help.GetHelpFor(consts.CatStr),
AllowArgs: true,
Run: func(ctx *grumble.Context) error {
fmt.Println()
cat(ctx, rpc)
fmt.Println()
return nil
@@ -339,6 +363,7 @@ func Init(app *grumble.App, rpc RPCServer) {
LongHelp: help.GetHelpFor(consts.DownloadStr),
AllowArgs: true,
Run: func(ctx *grumble.Context) error {
fmt.Println()
download(ctx, rpc)
fmt.Println()
return nil
@@ -351,6 +376,7 @@ func Init(app *grumble.App, rpc RPCServer) {
LongHelp: help.GetHelpFor(consts.UploadStr),
AllowArgs: true,
Run: func(ctx *grumble.Context) error {
fmt.Println()
upload(ctx, rpc)
fmt.Println()
return nil
@@ -363,6 +389,7 @@ func Init(app *grumble.App, rpc RPCServer) {
LongHelp: help.GetHelpFor(consts.ProcdumpStr),
AllowArgs: true,
Run: func(ctx *grumble.Context) error {
fmt.Println()
procdump(ctx, rpc)
fmt.Println()
return nil
+6
View File
@@ -328,6 +328,9 @@ func GenerateCertificate(rootDir string, host string, caType string, isCA bool,
log.Printf("Certificate authenticates host: %v", host)
template.DNSNames = append(template.DNSNames, host)
}
} else {
log.Printf("Client certificate authenticates CN: %v", host)
template.Subject.CommonName = host
}
// Sign certificate or self-sign if CA
@@ -418,6 +421,9 @@ func GenerateRSACertificate(rootDir string, host string, caType string, isCA boo
log.Printf("Certificate authenticates host: %v", host)
template.DNSNames = append(template.DNSNames, host)
}
} else {
log.Printf("Client certificate authenticates CN: %v", host)
template.Subject.CommonName = host
}
// Sign certificate or self-sign if CA
+46 -2
View File
@@ -1,10 +1,54 @@
package handlers
import (
"log"
consts "sliver/client/constants"
pb "sliver/protobuf/client"
"sliver/server/core"
"github.com/golang/protobuf/proto"
)
// RPCResponse - Called with response data, mapped back to reqID
type RPCResponse func([]byte)
// RPCHandler - RPC handlers accept bytes and return bytes
type RPCHandler func([]byte, RPCResponse)
var (
rpcHandlers = map[string]interface{}{}
rpcHandlers = map[string]RPCHandler{
consts.SessionsStr: rpcSessions,
}
)
// GetRPCHandlers - Returns a map of server-side msg handlers
func GetRPCHandlers() map[string]interface{} {
func GetRPCHandlers() map[string]RPCHandler {
return rpcHandlers
}
func rpcSessions(_ []byte, resp RPCResponse) {
sessions := &pb.Sessions{}
if 0 < len(*core.Hive.Slivers) {
for _, sliver := range *core.Hive.Slivers {
sessions.Slivers = append(sessions.Slivers, &pb.Sliver{
ID: int32(sliver.ID),
Name: sliver.Name,
Hostname: sliver.Hostname,
Username: sliver.Username,
UID: sliver.UID,
GID: sliver.GID,
OS: sliver.Os,
Arch: sliver.Arch,
Transport: sliver.Transport,
RemoteAddress: sliver.RemoteAddress,
PID: sliver.PID,
Filename: sliver.Filename,
})
}
}
data, err := proto.Marshal(sessions)
if err != nil {
log.Printf("Error encoding rpc response %v", err)
}
resp(data)
}
+19 -11
View File
@@ -11,7 +11,7 @@ import (
"sliver/server/assets"
"sliver/server/certs"
"sliver/server/core"
serverHandlers "sliver/server/handlers"
rpc "sliver/server/handlers"
pb "sliver/protobuf/client"
@@ -54,9 +54,9 @@ func acceptClientConnections(ln net.Listener) {
}
}
func printConnState(conn *tls.Conn) {
log.Print(">>>>>>>>>>>>>>>> State <<<<<<<<<<<<<<<<")
state := conn.ConnectionState()
func printConnState(tlsConn *tls.Conn) {
log.Print(">>>>>>>>>>>>>>>> TLS State <<<<<<<<<<<<<<<<")
state := tlsConn.ConnectionState()
log.Printf("Version: %x", state.Version)
log.Printf("HandshakeComplete: %t", state.HandshakeComplete)
log.Printf("DidResume: %t", state.DidResume)
@@ -82,8 +82,13 @@ func handleClientConnection(conn net.Conn) {
}
tlsConn.Read([]byte{}) // Unless you read 0 bytes the TLS handshake will not complete
printConnState(tlsConn)
log.Printf("Accepted incoming connection: %s", conn.RemoteAddr())
client := core.GetClient("test")
certs := tlsConn.ConnectionState().PeerCertificates
if len(certs) < 1 {
return
}
operator := certs[0].Subject.CommonName // Get operator name from cert CN
log.Printf("Accepted incoming client connection: %s (%s)", conn.RemoteAddr(), operator)
client := core.GetClient(operator)
core.Clients.AddClient(client)
defer func() {
@@ -92,17 +97,20 @@ func handleClientConnection(conn net.Conn) {
}()
go func() {
handlers := serverHandlers.GetRPCHandlers()
handlers := rpc.GetRPCHandlers()
for {
envelope, err := socketReadEnvelope(conn)
if err != nil {
log.Printf("Socket read error %v", err)
return
}
if envelope.ID != "" {
client.Response(envelope)
} else if handler, ok := handlers[envelope.Type]; ok {
go handler.(func(*core.Client, []byte))(client, envelope.Data)
if handler, ok := handlers[envelope.Type]; ok {
go handler(envelope.Data, func(data []byte) {
client.Send <- &pb.Envelope{
ID: envelope.ID,
Data: data,
}
})
}
}
}()