Skip to content

Commit

Permalink
wip refactor of generate code
Browse files Browse the repository at this point in the history
  • Loading branch information
moloch-- committed Sep 29, 2022
1 parent 0fab805 commit 1fd850c
Show file tree
Hide file tree
Showing 13 changed files with 679 additions and 754 deletions.
4 changes: 2 additions & 2 deletions client/command/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ func externalBuild(config *clientpb.ImplantConfig, save string, con *console.Sli
case <-time.After(100 * time.Millisecond):
elapsed := time.Since(start)
msg := fmt.Sprintf("Waiting for external build %s (template: %s) ... %s",
externalImplantConfig.Name,
externalImplantConfig.Config.Name,
externalImplantConfig.Config.TemplateName,
elapsed.Round(time.Second),
)
Expand All @@ -665,7 +665,7 @@ func externalBuild(config *clientpb.ImplantConfig, save string, con *console.Sli
if event.EventType != consts.BuildCompletedEvent {
continue
}
if string(event.Data) == externalImplantConfig.Name {
if string(event.Data) == externalImplantConfig.Config.Name {
con.RemoveEventListener(listenerID)
waiting = false
}
Expand Down
1,049 changes: 520 additions & 529 deletions protobuf/clientpb/client.pb.go

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions protobuf/clientpb/client.proto
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,8 @@ message ImplantConfig {
}

message ExternalImplantConfig {
string Name = 1;
ImplantConfig Config = 2;
string OTPSecret = 3;
ImplantConfig Config = 1;
string OTPSecret = 2;
}

message ExternalImplantBinary {
Expand Down
32 changes: 18 additions & 14 deletions server/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import (
"io"
"os"
"os/signal"
"path/filepath"

consts "github.com/bishopfox/sliver/client/constants"
"github.com/bishopfox/sliver/protobuf/clientpb"
"github.com/bishopfox/sliver/protobuf/commonpb"
"github.com/bishopfox/sliver/protobuf/rpcpb"
"github.com/bishopfox/sliver/server/db/models"
"github.com/bishopfox/sliver/server/generate"
"github.com/bishopfox/sliver/server/log"
"github.com/bishopfox/sliver/util"
)

var (
Expand Down Expand Up @@ -87,7 +88,7 @@ func buildEvents(rpc rpcpb.SliverRPCClient) <-chan *clientpb.Event {

// Trigger event based on type
switch event.EventType {
case consts.BuildEvent:
case consts.ExternalBuildEvent:
events <- event
default:
builderLog.Debugf("Ignore event (%s)", event.EventType)
Expand All @@ -108,6 +109,10 @@ func (b *sliverBuilder) HandleBuildEvent(event *clientpb.Event) {
builderLog.Errorf("Failed to get implant config: %s", err)
return
}
if extConfig == nil {
builderLog.Errorf("nil extConfig")
return
}

// check to see if the event matches a target we're configured to build for
if !contains(b.config.GOOSs, extConfig.Config.GOOS) {
Expand All @@ -122,27 +127,25 @@ 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
}

builderLog.Infof("Building for %s/%s (format: %s)", extConfig.Config.GOOS, extConfig.Config.GOARCH, extConfig.Config.Format)
if extConfig.Config.TemplateName == "" {
extConfig.Config.TemplateName = generate.SliverTemplateName
}
if extConfig == nil {
err = util.AllowedName(extConfig.Config.Name)
if err != nil {
builderLog.Errorf("Invalid implant name: %s", err)
return
}
_, extModel := generate.ImplantConfigFromProtobuf(extConfig.Config)

extModel := models.ImplantConfig{}.FromProtobuf(extConfig.Config)
builderLog.Infof("Building %s for %s/%s (format: %s)", extConfig.Config.Name, extConfig.Config.GOOS, extConfig.Config.GOARCH, extConfig.Config.Format)

var fPath string
switch extConfig.Config.Format {
case clientpb.OutputFormat_SERVICE:
fallthrough
case clientpb.OutputFormat_EXECUTABLE:
fPath, err = generate.SliverExecutable(extConfig.Config.Name, extModel, false)
fPath, err = generate.SliverExecutable(extConfig.Config.Name, extConfig.OTPSecret, extModel, false)
case clientpb.OutputFormat_SHARED_LIB:
fPath, err = generate.SliverSharedLibrary(extConfig.Config.Name, extModel, false)
fPath, err = generate.SliverSharedLibrary(extConfig.Config.Name, extConfig.OTPSecret, extModel, false)
case clientpb.OutputFormat_SHELLCODE:
fPath, err = generate.SliverShellcode(extConfig.Config.Name, extModel, false)
fPath, err = generate.SliverShellcode(extConfig.Config.Name, extConfig.OTPSecret, extModel, false)
default:
builderLog.Errorf("invalid output format: %s", extConfig.Config.Format)
return
Expand All @@ -157,7 +160,7 @@ func (b *sliverBuilder) HandleBuildEvent(event *clientpb.Event) {
return
}

fileName := extConfig.Config.Name
fileName := filepath.Base(extConfig.Config.Name)
if extConfig.Config.GOOS == "windows" {
fileName += ".exe"
}
Expand All @@ -166,14 +169,15 @@ func (b *sliverBuilder) HandleBuildEvent(event *clientpb.Event) {
Name: extConfig.Config.Name,
ImplantConfigID: extConfig.Config.ID,
File: &commonpb.File{
Name: extConfig.Config.Name,
Name: fileName,
Data: data,
},
})
if err != nil {
builderLog.Errorf("Failed to save build: %s", err)
return
}
builderLog.Infof("Successfully built %s", fileName)
}

func contains[T comparable](elems []T, v T) bool {
Expand Down
44 changes: 0 additions & 44 deletions server/db/models/implant.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,50 +142,6 @@ func (ic *ImplantConfig) BeforeCreate(tx *gorm.DB) (err error) {
return nil
}

func (ic ImplantConfig) FromProtobuf(pbConfig *clientpb.ImplantConfig) *ImplantConfig {
return &ImplantConfig{
ID: uuid.FromStringOrNil(pbConfig.ID),
IsBeacon: pbConfig.IsBeacon,
BeaconInterval: pbConfig.BeaconInterval,
BeaconJitter: pbConfig.BeaconJitter,

GOOS: pbConfig.GOOS,
GOARCH: pbConfig.GOARCH,

MtlsCACert: pbConfig.MtlsCACert,
MtlsCert: pbConfig.MtlsCert,
MtlsKey: pbConfig.MtlsKey,

Debug: pbConfig.Debug,
Evasion: pbConfig.Evasion,
ObfuscateSymbols: pbConfig.ObfuscateSymbols,
TemplateName: pbConfig.TemplateName,

ReconnectInterval: pbConfig.ReconnectInterval,
MaxConnectionErrors: pbConfig.MaxConnectionErrors,
ConnectionStrategy: pbConfig.ConnectionStrategy,

LimitDatetime: pbConfig.LimitDatetime,
LimitDomainJoined: pbConfig.LimitDomainJoined,
LimitHostname: pbConfig.LimitHostname,
LimitUsername: pbConfig.LimitUsername,
LimitFileExists: pbConfig.LimitFileExists,
LimitLocale: pbConfig.LimitLocale,

IsSharedLib: pbConfig.IsSharedLib,
IsService: pbConfig.IsService,
IsShellcode: pbConfig.IsShellcode,
Format: pbConfig.Format,
WGImplantPrivKey: pbConfig.WGImplantPrivKey,
WGServerPubKey: pbConfig.WGServerPubKey,
WGPeerTunIP: pbConfig.WGPeerTunIP,
WGKeyExchangePort: pbConfig.WGKeyExchangePort,
WGTcpCommsPort: pbConfig.WGTcpCommsPort,

FileName: ic.FileName,
}
}

// ToProtobuf - Convert ImplantConfig to protobuf equiv
func (ic *ImplantConfig) ToProtobuf() *clientpb.ImplantConfig {
config := &clientpb.ImplantConfig{
Expand Down
Loading

0 comments on commit 1fd850c

Please sign in to comment.