Skip to content

Commit

Permalink
Fix some review issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pomo-mondreganto committed Aug 21, 2023
1 parent 9e746cb commit bcc87ec
Show file tree
Hide file tree
Showing 16 changed files with 705 additions and 341 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ TARGET := image-full
CONTAINER_NAME := neo_env

NEED_COMMANDS := curl wget dig nc file nslookup ifconfig python3 pip3 vim
NEED_PACKAGES := pymongo pymysql psycopg2 redis z3 secrets checklib requests pwn numpy bs4 hashpumpy dnslib regex lxml gmpy2 sympy grequests
NEED_PACKAGES := pymongo pymysql psycopg2 redis z3 secrets checklib requests pwn numpy bs4 hashpumpy dnslib regex lxml gmpy2 sympy grequests websocket

.PHONY: lint-go
lint-go:
Expand All @@ -18,6 +18,10 @@ lint-proto:
.PHONY: lint
lint: lint-go lint-proto

.PHONY: goimports
goimports:
goimports -local github.com/c4t-but-s4d/neo -w -d $(find . -type f -name '*.go' -not -path "./proto/*")

.PHONY: test
test:
go test -race -timeout 1m ./...
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ can be a pain and wastes time. **Neo** helps in two primary ways:
automatically.

2. Exploit writers don't upload the newly-created exploits to the exploit server, neither do they manage the
distribution
by hand, but rather submit them to the **Neo server** using the same client, and the server does all the work,
distributing the exploit
among the available clients.
distribution manually, but rather submit them to the **Neo server** using the same client,
and the server does all the work, distributing the exploit among the available clients.

## Usage

Expand Down
2 changes: 1 addition & 1 deletion client_env/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ WORKDIR /app
COPY go.* ./
COPY cmd cmd
COPY internal internal
COPY lib lib
COPY proto/go proto/go
COPY pkg pkg
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg/mod \
Expand Down
9 changes: 1 addition & 8 deletions cmd/client/cli/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,8 @@ func (ac *addCLI) Run(ctx context.Context) error {
if err != nil {
return fmt.Errorf("failed to get config from server: %w", err)
}
exists := false
for _, v := range state.Exploits {
if v.ExploitId == ac.exploitID {
exists = true
break
}
}

if exists {
if getExploitFromState(state, ac.exploitID) != nil {
fmt.Println("The exploit with this id already exists. Do you want to override (add new version) y/N ?")
var tmp string
if _, err := fmt.Scanln(&tmp); err != nil {
Expand Down
10 changes: 2 additions & 8 deletions cmd/client/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"sync"

"github.com/c4t-but-s4d/neo/internal/client"
"github.com/c4t-but-s4d/neo/pkg/grpcauth"
Expand All @@ -25,19 +24,16 @@ type NeoCLI interface {
type baseCLI struct {
cfg *client.Config

mu sync.Mutex
clientID string
}

func (cmd *baseCLI) client() (*client.Client, error) {
var opts []grpc.DialOption
opts = append(
opts,
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(
grpc.UseCompressor(gzip.Name),
),
)
}
if cmd.cfg.GrpcAuthKey != "" {
interceptor := grpcauth.NewClientInterceptor(cmd.cfg.GrpcAuthKey)
opts = append(
Expand All @@ -54,8 +50,6 @@ func (cmd *baseCLI) client() (*client.Client, error) {
}

func (cmd *baseCLI) ClientID() string {
cmd.mu.Lock()
defer cmd.mu.Unlock()
if cmd.clientID != "" {
return cmd.clientID
}
Expand Down
9 changes: 1 addition & 8 deletions cmd/client/cli/dry_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,7 @@ func (rc *dryRunCLI) Run(ctx context.Context) error {
return fmt.Errorf("failed to parse config: %w", err)
}

exists := false
for _, v := range state.Exploits {
if v.ExploitId == rc.exploitID {
exists = true
break
}
}
if !exists {
if getExploitFromState(state, rc.exploitID) == nil {
return fmt.Errorf("exploit %s does not exist, add it first", rc.exploitID)
}

Expand Down
15 changes: 2 additions & 13 deletions cmd/client/cli/set_disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (

"github.com/spf13/cobra"

epb "github.com/c4t-but-s4d/neo/proto/go/exploits"

"github.com/c4t-but-s4d/neo/internal/client"
)

Expand Down Expand Up @@ -35,18 +33,9 @@ func (sc *setDisabledCli) Run(ctx context.Context) error {
return fmt.Errorf("failed to get config from server: %w", err)
}

var spl *epb.ExploitState
for _, v := range state.Exploits {
if v.ExploitId == sc.exploitID {
spl = v
break
}
}

if spl == nil {
if spl := getExploitFromState(state, sc.exploitID); spl == nil {
return fmt.Errorf("exploit %s does not exist", sc.exploitID)
}
if err := c.SetExploitDisabled(ctx, spl.ExploitId, sc.disabled); err != nil {
} else if err := c.SetExploitDisabled(ctx, spl.ExploitId, sc.disabled); err != nil {
return fmt.Errorf("set disabled failed: %w", err)
}

Expand Down
9 changes: 1 addition & 8 deletions cmd/client/cli/single.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,8 @@ func (sc *singleRunCLI) Run(ctx context.Context) error {
if err != nil {
return fmt.Errorf("failed to get config from server: %w", err)
}
exists := false
for _, v := range state.Exploits {
if v.ExploitId == sc.exploitID {
exists = true
break
}
}

if !exists {
if getExploitFromState(state, sc.exploitID) == nil {
logrus.Fatalf("Exploit %s does not exist. Please, add it first.", sc.exploitID)
}
if err := c.SingleRun(ctx, sc.exploitID); err != nil {
Expand Down
18 changes: 17 additions & 1 deletion cmd/client/cli/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
package cli

import "bytes"
import (
"bytes"

"github.com/samber/lo"

"github.com/c4t-but-s4d/neo/proto/go/exploits"
)

func isBinary(data []byte) bool {
return bytes.Equal(data[:4], []byte("\x7fELF"))
}

func getExploitFromState(state *exploits.ServerState, exploitID string) *exploits.ExploitState {
exp, ok := lo.Find(state.Exploits, func(s *exploits.ExploitState) bool {
return s.ExploitId == exploitID
})
if !ok {
return nil
}
return exp
}
29 changes: 13 additions & 16 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"

"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/emptypb"

epb "github.com/c4t-but-s4d/neo/proto/go/exploits"

Expand Down Expand Up @@ -41,10 +40,9 @@ func (nc *Client) GetServerState(ctx context.Context) (*epb.ServerState, error)
resp, err := nc.exploits.Ping(
ctx,
&epb.PingRequest{
ClientId: nc.ID,
Payload: &epb.PingRequest_ServerInfoRequest{
ServerInfoRequest: &epb.PingRequest_ServerInfo{
ClientId: nc.ID,
},
ServerInfoRequest: &epb.PingRequest_ServerInfo{},
},
},
)
Expand All @@ -58,10 +56,10 @@ func (nc *Client) Heartbeat(ctx context.Context) (*epb.ServerState, error) {
resp, err := nc.exploits.Ping(
ctx,
&epb.PingRequest{
ClientId: nc.ID,
Payload: &epb.PingRequest_HeartbeatRequest{
HeartbeatRequest: &epb.PingRequest_Heartbeat{
ClientId: nc.ID,
Weight: int32(nc.Weight),
Weight: int32(nc.Weight),
},
},
},
Expand All @@ -76,10 +74,9 @@ func (nc *Client) Leave(ctx context.Context) error {
if _, err := nc.exploits.Ping(
ctx,
&epb.PingRequest{
ClientId: nc.ID,
Payload: &epb.PingRequest_LeaveRequest{
LeaveRequest: &epb.PingRequest_Leave{
ClientId: nc.ID,
},
LeaveRequest: &epb.PingRequest_Leave{},
},
},
); err != nil {
Expand Down Expand Up @@ -138,7 +135,7 @@ func (nc *Client) UploadFile(ctx context.Context, r io.Reader) (*fspb.FileInfo,
}

func (nc *Client) BroadcastCommand(ctx context.Context, command string) error {
req := &epb.Command{Command: command}
req := &epb.BroadcastRequest{Command: command}
if _, err := nc.exploits.BroadcastCommand(ctx, req); err != nil {
return fmt.Errorf("making broadcast command request: %w", err)
}
Expand Down Expand Up @@ -168,13 +165,13 @@ func (nc *Client) SetExploitDisabled(ctx context.Context, id string, disabled bo
return nil
}

func (nc *Client) ListenBroadcasts(ctx context.Context) (<-chan *epb.Command, error) {
stream, err := nc.exploits.BroadcastRequests(ctx, &emptypb.Empty{})
func (nc *Client) ListenBroadcasts(ctx context.Context) (<-chan *epb.BroadcastSubscribeResponse, error) {
stream, err := nc.exploits.BroadcastSubscribe(ctx, &epb.BroadcastSubscribeRequest{})
if err != nil {
return nil, fmt.Errorf("creating broadcast requests stream: %w", err)
}

results := make(chan *epb.Command)
results := make(chan *epb.BroadcastSubscribeResponse)
go func() {
defer close(results)
for {
Expand All @@ -194,13 +191,13 @@ func (nc *Client) ListenBroadcasts(ctx context.Context) (<-chan *epb.Command, er
return results, nil
}

func (nc *Client) ListenSingleRuns(ctx context.Context) (<-chan *epb.SingleRunRequest, error) {
stream, err := nc.exploits.SingleRunRequests(ctx, &emptypb.Empty{})
func (nc *Client) ListenSingleRuns(ctx context.Context) (<-chan *epb.SingleRunSubscribeResponse, error) {
stream, err := nc.exploits.SingleRunSubscribe(ctx, &epb.SingleRunSubscribeRequest{})
if err != nil {
return nil, fmt.Errorf("creating single run requests stream: %w", err)
}

results := make(chan *epb.SingleRunRequest)
results := make(chan *epb.SingleRunSubscribeResponse)
go func() {
defer close(results)
for {
Expand Down
6 changes: 3 additions & 3 deletions internal/exploit/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewRunner(
client: c,
maxJobs: maxJobs,
maxEndlessJobs: maxEndlessJobs,
singleRuns: make(chan *epb.SingleRunRequest),
singleRuns: make(chan *epb.SingleRunSubscribeResponse),
restarts: make(chan struct{}, 1),
logSender: logSender,
metricsPusher: push.
Expand All @@ -66,7 +66,7 @@ type Runner struct {
maxJobs int
maxEndlessJobs int

singleRuns chan *epb.SingleRunRequest
singleRuns chan *epb.SingleRunSubscribeResponse
restarts chan struct{}

logger *logrus.Entry
Expand Down Expand Up @@ -344,7 +344,7 @@ func (r *Runner) startPushingMetrics(ctx context.Context) {
}
}

func (r *Runner) handleBroadcastCommand(ctx context.Context, cmd *epb.Command) error {
func (r *Runner) handleBroadcastCommand(ctx context.Context, cmd *epb.BroadcastSubscribeResponse) error {
c := exec.CommandContext(ctx, "/bin/bash", "-c", cmd.Command)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
Expand Down
Loading

0 comments on commit bcc87ec

Please sign in to comment.