Skip to content

Commit

Permalink
Basic workflow is working
Browse files Browse the repository at this point in the history
  • Loading branch information
moloch-- committed Oct 15, 2022
1 parent 677a079 commit 8474692
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
11 changes: 10 additions & 1 deletion server/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/bishopfox/sliver/protobuf/clientpb"
"github.com/bishopfox/sliver/protobuf/commonpb"
"github.com/bishopfox/sliver/protobuf/rpcpb"
"github.com/bishopfox/sliver/server/codenames"
"github.com/bishopfox/sliver/server/generate"
"github.com/bishopfox/sliver/server/log"
"github.com/bishopfox/sliver/util"
Expand Down Expand Up @@ -127,6 +128,9 @@ func (b *sliverBuilder) HandleBuildEvent(event *clientpb.Event) {
builderLog.Warnf("This builder is not configured to build for format %s, ignore event", extConfig.Config.Format)
return
}
if extConfig.Config.Name == "" {
extConfig.Config.Name, _ = codenames.GetCodename()
}
err = util.AllowedName(extConfig.Config.Name)
if err != nil {
builderLog.Errorf("Invalid implant name: %s", err)
Expand All @@ -135,6 +139,8 @@ func (b *sliverBuilder) HandleBuildEvent(event *clientpb.Event) {
_, extModel := generate.ImplantConfigFromProtobuf(extConfig.Config)

builderLog.Infof("Building %s for %s/%s (format: %s)", extConfig.Config.Name, extConfig.Config.GOOS, extConfig.Config.GOARCH, extConfig.Config.Format)
builderLog.Infof(" [c2] mtls:%t wg:%t http/s:%t dns:%t", extModel.MTLSc2Enabled, extModel.WGc2Enabled, extModel.HTTPc2Enabled, extModel.DNSc2Enabled)
builderLog.Infof("[pivots] tcp:%t named-pipe:%t", extModel.TCPPivotc2Enabled, extModel.NamePipec2Enabled)

var fPath string
switch extConfig.Config.Format {
Expand All @@ -154,6 +160,8 @@ func (b *sliverBuilder) HandleBuildEvent(event *clientpb.Event) {
builderLog.Errorf("Failed to generate sliver: %s", err)
return
}
builderLog.Infof("Build completed successfully: %s", fPath)

data, err := os.ReadFile(fPath)
if err != nil {
builderLog.Errorf("Failed to read generated sliver: %s", err)
Expand All @@ -165,6 +173,7 @@ func (b *sliverBuilder) HandleBuildEvent(event *clientpb.Event) {
fileName += ".exe"
}

builderLog.Infof("Uploading '%s' to server ...", extConfig.Config.Name)
_, err = b.rpc.GenerateExternalSaveBuild(context.Background(), &clientpb.ExternalImplantBinary{
Name: extConfig.Config.Name,
ImplantConfigID: extConfig.Config.ID,
Expand All @@ -177,7 +186,7 @@ func (b *sliverBuilder) HandleBuildEvent(event *clientpb.Event) {
builderLog.Errorf("Failed to save build: %s", err)
return
}
builderLog.Infof("Successfully built %s", fileName)
builderLog.Infof("All done, built and saved %s", fileName)
}

func contains[T comparable](elems []T, v T) bool {
Expand Down
5 changes: 4 additions & 1 deletion server/cli/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,14 @@ var builderCmd = &cobra.Command{
config, err := clientAssets.ReadConfig(configPath)
if err != nil {
builderLog.Fatalf("Invalid config file: %s", err)
os.Exit(-1)
}
// connect to the server
builderLog.Infof("Connecting to %s@%s:%d ...", config.Operator, config.LHost, config.LPort)
rpc, ln, err := transport.MTLSConnect(config)
if err != nil {
builderLog.Fatalf("Failed to connect to server: %s", err)
builderLog.Errorf("Failed to connect to server: %s", err)
os.Exit(-2)
}
defer ln.Close()
builder.StartBuilder(rpc, builderConfig)
Expand Down
28 changes: 28 additions & 0 deletions server/db/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,34 @@ func ImplantConfigByID(id string) (*models.ImplantConfig, error) {
return &config, err
}

// ImplantConfigWithC2sByID - Fetch implant build by name
func ImplantConfigWithC2sByID(id string) (*models.ImplantConfig, error) {
if len(id) < 1 {
return nil, ErrRecordNotFound
}
configID := uuid.FromStringOrNil(id)
if configID == uuid.Nil {
return nil, ErrRecordNotFound
}
config := models.ImplantConfig{}
err := Session().Where(&models.ImplantConfig{
ID: configID,
}).First(&config).Error
if err != nil {
return nil, err
}

c2s := []models.ImplantC2{}
err = Session().Where(&models.ImplantC2{
ImplantConfigID: config.ID,
}).Find(&c2s).Error
if err != nil {
return nil, err
}
config.C2 = c2s
return &config, err
}

// ImplantConfigByECCPublicKey - Fetch implant build by it's ecc public key
func ImplantConfigByECCPublicKeyDigest(publicKeyDigest [32]byte) (*models.ImplantConfig, error) {
config := models.ImplantConfig{}
Expand Down
13 changes: 9 additions & 4 deletions server/rpc/rpc-generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
consts "github.com/bishopfox/sliver/client/constants"
"github.com/bishopfox/sliver/protobuf/clientpb"
"github.com/bishopfox/sliver/protobuf/commonpb"
"github.com/bishopfox/sliver/server/assets"
"github.com/bishopfox/sliver/server/codenames"
"github.com/bishopfox/sliver/server/core"
"github.com/bishopfox/sliver/server/cryptography"
Expand Down Expand Up @@ -294,7 +295,7 @@ func (rpc *Server) GenerateExternal(ctx context.Context, req *clientpb.GenerateR
}

func (rpc *Server) GenerateExternalSaveBuild(ctx context.Context, req *clientpb.ExternalImplantBinary) (*commonpb.Empty, error) {
implantConfig, err := db.ImplantConfigByID(req.ImplantConfigID)
implantConfig, err := db.ImplantConfigWithC2sByID(req.ImplantConfigID)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid implant config id")
}
Expand All @@ -308,10 +309,11 @@ func (rpc *Server) GenerateExternalSaveBuild(ctx context.Context, req *clientpb.
}
_, err = db.ImplantBuildByName(req.Name)
if err == nil {
rpcLog.Errorf("Build '%s' already exists!", req.Name)
return nil, ErrBuildExists
}

tmpFile, err := os.CreateTemp("", "sliver-external-build")
tmpFile, err := os.CreateTemp(assets.GetRootAppDir(), "tmp-external-build-*")
if err != nil {
rpcLog.Errorf("Failed to create temporary file: %s", err)
return nil, status.Error(codes.Internal, "Failed to write implant binary to temp file")
Expand All @@ -322,8 +324,11 @@ func (rpc *Server) GenerateExternalSaveBuild(ctx context.Context, req *clientpb.
rcpLog.Errorf("Failed to write implant binary to temp file: %s", err)
return nil, status.Error(codes.Internal, "Failed to write implant binary to temp file")
}
err = generate.ImplantBuildSave(req.Name, implantConfig, "")
rpcLog.Infof("Saving external build '%s' from %s", req.Name, tmpFile.Name())

err = generate.ImplantBuildSave(req.Name, implantConfig, tmpFile.Name())
if err != nil {
rpcLog.Errorf("Failed to save external build: %s", err)
return nil, err
}

Expand All @@ -336,7 +341,7 @@ func (rpc *Server) GenerateExternalSaveBuild(ctx context.Context, req *clientpb.
}

func (rpc *Server) GenerateExternalGetImplantConfig(ctx context.Context, req *clientpb.ImplantConfig) (*clientpb.ExternalImplantConfig, error) {
implantConfig, err := db.ImplantConfigByID(req.ID)
implantConfig, err := db.ImplantConfigWithC2sByID(req.ID)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "invalid implant config id")
}
Expand Down

0 comments on commit 8474692

Please sign in to comment.