Skip to content

Commit

Permalink
Merge pull request #1303 from BishopFox/audit/log
Browse files Browse the repository at this point in the history
Add additional details to audit log
  • Loading branch information
moloch-- committed Jun 16, 2023
2 parents 57e2111 + 73f155f commit 6647abe
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
10 changes: 6 additions & 4 deletions server/rpc/rpc-client-logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ import (
)

var (
// ErrInvalidStreamName - Invalid stream name
ErrInvalidStreamName = status.Error(codes.InvalidArgument, "Invalid stream name")
rpcClientLogs = log.NamedLogger("rpc", "client-logs")
streamNamePattern = regexp.MustCompile("^[a-z0-9_-]+$")

rpcClientLogs = log.NamedLogger("rpc", "client-logs")
streamNamePattern = regexp.MustCompile("^[a-z0-9_-]+$")
)

type LogStream struct {
Expand Down Expand Up @@ -72,7 +74,7 @@ func (l *LogStream) Write(data []byte) (int, error) {
return n, err
}
go gzipFile(partFileName)
l.logFile, err = os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
l.logFile, err = os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o600)
if err != nil {
return n, err
}
Expand Down Expand Up @@ -129,7 +131,7 @@ func openNewLogStream(logsDir string, stream string) (*LogStream, error) {
rpcClientLogs.Warnf("Client console log file already exists: %s", logPath)
logPath = filepath.Join(logsDir, filepath.Base(fmt.Sprintf("%s_%s_%s.log", stream, dateTime, randomSuffix(6))))
}
logFile, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
logFile, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o600)
if err != nil {
return nil, err
}
Expand Down
43 changes: 32 additions & 11 deletions server/transport/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ import (
"github.com/bishopfox/sliver/server/core"
"github.com/bishopfox/sliver/server/db"
"github.com/bishopfox/sliver/server/log"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
grpc_tags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
)

Expand All @@ -55,14 +56,14 @@ func initMiddleware(remoteAuth bool) []grpc.ServerOption {
grpc_logrus.ReplaceGrpcLogger(logrusEntry)
if remoteAuth {
return []grpc.ServerOption{
grpc_middleware.WithUnaryServerChain(
grpc.ChainUnaryInterceptor(
grpc_auth.UnaryServerInterceptor(tokenAuthFunc),
auditLogUnaryServerInterceptor(),
grpc_tags.UnaryServerInterceptor(grpc_tags.WithFieldExtractor(grpc_tags.CodeGenRequestFieldExtractor)),
grpc_logrus.UnaryServerInterceptor(logrusEntry, logrusOpts...),
grpc_logrus.PayloadUnaryServerInterceptor(logrusEntry, deciderUnary),
),
grpc_middleware.WithStreamServerChain(
grpc.ChainStreamInterceptor(
grpc_auth.StreamServerInterceptor(tokenAuthFunc),
grpc_tags.StreamServerInterceptor(grpc_tags.WithFieldExtractor(grpc_tags.CodeGenRequestFieldExtractor)),
grpc_logrus.StreamServerInterceptor(logrusEntry, logrusOpts...),
Expand All @@ -71,14 +72,14 @@ func initMiddleware(remoteAuth bool) []grpc.ServerOption {
}
} else {
return []grpc.ServerOption{
grpc_middleware.WithUnaryServerChain(
grpc.ChainUnaryInterceptor(
grpc_auth.UnaryServerInterceptor(serverAuthFunc),
auditLogUnaryServerInterceptor(),
grpc_tags.UnaryServerInterceptor(grpc_tags.WithFieldExtractor(grpc_tags.CodeGenRequestFieldExtractor)),
grpc_logrus.UnaryServerInterceptor(logrusEntry, logrusOpts...),
grpc_logrus.PayloadUnaryServerInterceptor(logrusEntry, deciderUnary),
),
grpc_middleware.WithStreamServerChain(
grpc.ChainStreamInterceptor(
grpc_auth.StreamServerInterceptor(serverAuthFunc),
grpc_tags.StreamServerInterceptor(grpc_tags.WithFieldExtractor(grpc_tags.CodeGenRequestFieldExtractor)),
grpc_logrus.StreamServerInterceptor(logrusEntry, logrusOpts...),
Expand Down Expand Up @@ -184,10 +185,12 @@ func codeToLevel(code codes.Code) logrus.Level {
}

type auditUnaryLogMsg struct {
Request string `json:"request"`
Method string `json:"method"`
Session string `json:"session,omitempty"`
Beacon string `json:"beacon,omitempty"`
Request string `json:"request"`
Method string `json:"method"`
Session string `json:"session,omitempty"`
Beacon string `json:"beacon,omitempty"`
RemoteIP string `json:"remote_ip"`
User string `json:"user"`
}

func auditLogUnaryServerInterceptor() grpc.UnaryServerInterceptor {
Expand All @@ -203,10 +206,14 @@ func auditLogUnaryServerInterceptor() grpc.UnaryServerInterceptor {
middlewareLog.Errorf("Middleware failed to insert details: %s", err)
}

p, _ := peer.FromContext(ctx)

// Construct Log Message
msg := &auditUnaryLogMsg{
Request: string(rawRequest),
Method: info.FullMethod,
Request: string(rawRequest),
Method: info.FullMethod,
User: getUser(p),
RemoteIP: p.Addr.String(),
}
if session != nil {
sessionJSON, _ := json.Marshal(session)
Expand All @@ -225,6 +232,20 @@ func auditLogUnaryServerInterceptor() grpc.UnaryServerInterceptor {
}
}

func getUser(client *peer.Peer) string {
tlsAuth, ok := client.AuthInfo.(credentials.TLSInfo)
if !ok {
return ""
}
if len(tlsAuth.State.VerifiedChains) == 0 || len(tlsAuth.State.VerifiedChains[0]) == 0 {
return ""
}
if tlsAuth.State.VerifiedChains[0][0].Subject.CommonName != "" {
return tlsAuth.State.VerifiedChains[0][0].Subject.CommonName
}
return ""
}

func getActiveTarget(rawRequest []byte) (*clientpb.Session, *clientpb.Beacon, error) {

var activeBeacon *clientpb.Beacon
Expand Down
3 changes: 0 additions & 3 deletions server/transport/mtls.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ func getOperatorServerTLSConfig(host string) *tls.Config {
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS13,
}
if certs.TLSKeyLogger != nil {
tlsConfig.KeyLogWriter = certs.TLSKeyLogger
}

return tlsConfig
}

0 comments on commit 6647abe

Please sign in to comment.