Skip to content

Commit 1fb0a9c

Browse files
committed
feat: impl bind mode
1 parent 33e7ec0 commit 1fb0a9c

File tree

12 files changed

+83
-73
lines changed

12 files changed

+83
-73
lines changed

client/command/basic/bind.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package basic
22

33
import (
44
"github.com/chainreactors/IoM-go/client"
5-
"github.com/chainreactors/IoM-go/consts"
65
"github.com/chainreactors/IoM-go/proto/client/clientpb"
76
"github.com/chainreactors/IoM-go/proto/implant/implantpb"
87
"github.com/chainreactors/malice-network/client/core"
@@ -49,8 +48,8 @@ func InitCmd(cmd *cobra.Command, con *core.Console) error {
4948
}
5049

5150
func Init(con *core.Console, sess *client.Session) (bool, error) {
52-
_, err := con.Rpc.InitBindSession(sess.Context(), &implantpb.Request{
53-
Name: consts.ModuleInit,
51+
_, err := con.Rpc.InitBindSession(sess.Context(), &implantpb.Init{
52+
Data: sess.Raw(),
5453
})
5554
if err != nil {
5655
return false, err

client/command/listener/commands.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ job
5757
return StartPipelineCmd(cmd, con)
5858
},
5959
Example: `~~~
60-
tcp start tcp_test
60+
pipeline start tcp_test
6161
~~~`,
6262
}
6363

client/command/listener/pipeline.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,10 @@ func ListPipelineCmd(cmd *cobra.Command, con *core.Console) error {
101101
func StartPipelineCmd(cmd *cobra.Command, con *core.Console) error {
102102
name := cmd.Flags().Arg(0)
103103

104-
if _, ok := con.Pipelines[name]; ok {
105-
_, err := con.Rpc.StopPipeline(con.Context(), &clientpb.CtrlPipeline{
104+
if p, ok := con.Pipelines[name]; ok && p.Enable {
105+
con.Rpc.StopPipeline(con.Context(), &clientpb.CtrlPipeline{
106106
Name: name,
107107
})
108-
if err != nil {
109-
return err
110-
}
111108
}
112109
certName, _ := cmd.Flags().GetString("cert-name")
113110
_, err := con.Rpc.StartPipeline(con.Context(), &clientpb.CtrlPipeline{

client/command/pipeline/commands.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ func Commands(con *core.Console) []*cobra.Command {
2020
Args: cobra.MaximumNArgs(1),
2121
Example: `~~~
2222
// Register a TCP pipeline with the default settings
23-
tcp --listener tcp_default
23+
tcp --listener listener
2424
2525
// Register a TCP pipeline with a custom name, host, and port
26-
tcp --name tcp_test --listener tcp_default --host 192.168.0.43 --port 5003
26+
tcp --name tcp_test --listener listener --host 192.168.0.43 --port 5003
2727
2828
// Register a TCP pipeline with TLS enabled and specify certificate and key paths
29-
tcp --listener tcp_default --tls --cert_path /path/to/cert --key_path /path/to/key
29+
tcp --listener listener --tls --cert_path /path/to/cert --key_path /path/to/key
3030
~~~`,
3131
}
3232
common.BindFlag(tcpCmd, common.PipelineFlagSet, common.TlsCertFlagSet, common.SecureFlagSet, common.EncryptionFlagSet)
@@ -53,13 +53,13 @@ tcp --listener tcp_default --tls --cert_path /path/to/cert --key_path /path/to/k
5353
Args: cobra.MaximumNArgs(1),
5454
Example: `~~~
5555
// Register an HTTP pipeline with the default settings
56-
http --listener http_default
56+
http --listener listener
5757
5858
// Register an HTTP pipeline with custom headers and error page
59-
http --name http_test --listener http_default --host 192.168.0.43 --port 8080 --headers "Content-Type=text/html" --error-page /path/to/error.html
59+
http --name http_test --listener listener --host 192.168.0.43 --port 8080 --headers "Content-Type=text/html" --error-page /path/to/error.html
6060
6161
// Register an HTTP pipeline with TLS enabled
62-
http --listener http_default --tls --cert_path /path/to/cert --key_path /path/to/key
62+
http --listener listener --tls --cert_path /path/to/cert --key_path /path/to/key
6363
~~~`,
6464
}
6565

@@ -95,7 +95,7 @@ http --listener http_default --tls --cert_path /path/to/cert --key_path /path/to
9595
Example: `
9696
new bind pipeline
9797
~~~
98-
bind listener
98+
bind --listener listener
9999
~~~
100100
`,
101101
}

client/command/sessions/commands.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@ session -a --static
5353
f.StringP("name", "n", "", "session name")
5454
f.StringP("target", "t", "", "session target")
5555
f.String("pipeline", "", "pipeline id")
56-
bindSessNewCmd.MarkFlagRequired("target")
57-
bindSessNewCmd.MarkFlagRequired("pipeline")
5856
})
59-
57+
bindSessNewCmd.MarkFlagRequired("target")
58+
bindSessNewCmd.MarkFlagRequired("pipeline")
6059
common.BindFlagCompletions(bindSessNewCmd, func(comp carapace.ActionMap) {
6160
comp["pipeline"] = common.AllPipelineCompleter(con)
6261
})

client/command/sessions/new.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ func NewBindSession(con *core.Console, PipelineID string, target string, name st
3636
Target: target,
3737
Type: consts.ImplantMaleficBind,
3838
RegisterData: &implantpb.Register{
39-
Name: name,
39+
Name: name,
40+
Timer: &implantpb.Timer{},
4041
},
4142
})
4243
if err != nil {
@@ -46,8 +47,8 @@ func NewBindSession(con *core.Console, PipelineID string, target string, name st
4647
if err != nil {
4748
return nil, err
4849
}
49-
_, err = con.Rpc.InitBindSession(sess.Context(), &implantpb.Request{
50-
Name: consts.ModuleInit,
50+
_, err = con.Rpc.InitBindSession(sess.Context(), &implantpb.Init{
51+
Data: sess.Raw(),
5152
})
5253
if err != nil {
5354
return nil, err

client/core/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ func (s *Server) EventHandler() {
182182
return
183183
}
184184
s.Update()
185+
if s.GetInteractive() != nil {
186+
s.UpdateSession(s.GetInteractive().SessionId)
187+
}
185188
s.EventStatus = true
186189
client.Log.Info("starting event loop\n")
187190
defer func() {

server/internal/core/connection.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ func GetConnection(conn *cryptostream.Conn, pipelineID string, secureConfig *imp
7979
// 优先从 ListenerSessions 获取,如果没有则从 secureConfig 获取交换密钥对
8080
func GetKeyPairForSession(sid uint32, secureConfig *implanttypes.SecureConfig) *clientpb.KeyPair {
8181
// 优先从 session 中获取 KeyPair
82+
if secureConfig == nil || !secureConfig.Enable {
83+
return nil
84+
}
8285
if session := ListenerSessions.Get(sid); session != nil {
8386
return session.KeyPair
8487
}

server/internal/core/pipeline.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ func (p *PipelineConfig) WrapConn(conn io.ReadWriteCloser) (*cryptostream.Conn,
6262
return cryptostream.WrapPeekConn(conn, crys, p.Parser)
6363
}
6464

65+
// WrapBindConn wraps a connection for bind mode without pre-reading
66+
// Bind mode expects server to send data first, then receive response
67+
func (p *PipelineConfig) WrapBindConn(conn io.ReadWriteCloser) (*cryptostream.Conn, error) {
68+
crys, err := configs.NewCrypto(p.Encryption.ToProtobuf())
69+
if err != nil {
70+
return nil, err
71+
}
72+
return cryptostream.WrapBindConn(conn, crys)
73+
}
74+
6575
//
6676
//func (p *PipelineConfig) ToFile() *clientpb.Pipeline {
6777
// return &clientpb.Pipeline{

server/internal/stream/peekconn.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,36 @@ func WrapPeekConn(conn io.ReadWriteCloser, cryptos []Cryptor, parserName string)
102102
}, nil
103103
}
104104

105+
// WrapBindConn wraps a connection for bind mode without pre-reading
106+
// Bind mode: Server actively sends data first, then receives response from implant
107+
// Only supports malefic parser in bind mode
108+
func WrapBindConn(conn io.ReadWriteCloser, cryptos []Cryptor) (*Conn, error) {
109+
if len(cryptos) == 0 {
110+
return nil, net.ErrClosed
111+
}
112+
113+
// Use the first cryptor for bind mode
114+
c := cryptos[0]
115+
116+
// Bind mode only uses malefic parser
117+
p, err := parser.NewParser("malefic")
118+
if err != nil {
119+
return nil, err
120+
}
121+
122+
if _, ok := conn.(net.Conn); ok {
123+
conn = NewCryptoConn(conn.(net.Conn), c)
124+
} else {
125+
conn = NewCryptoRWC(conn, c)
126+
}
127+
128+
return &Conn{
129+
ReadWriteCloser: conn,
130+
Parser: p,
131+
buf: nil, // No pre-read buffer for bind mode
132+
}, nil
133+
}
134+
105135
type Conn struct {
106136
io.ReadWriteCloser
107137
buf []byte

0 commit comments

Comments
 (0)