Skip to content

Commit 26e4cf3

Browse files
committed
More Optimizations!
- Replaced 'errors.New' with the 'xerr.New' (Less imports than the 'errors' package) - Replaced 'errors.New' in the "data" package with a number specific error values, include whence values. - Fixed LICENSE file - Added a test file to show the improvements of "xerr" over "fmt.Errorf"
1 parent b507f32 commit 26e4cf3

33 files changed

+807
-391
lines changed

LICENSE

+647-253
Large diffs are not rendered by default.

c2/config.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package c2
22

33
import (
4-
"errors"
54
"io"
65
"strconv"
76
"time"
@@ -82,13 +81,13 @@ var (
8281

8382
// ErrMultipleHints is an error returned by the 'Profile' function if more that one Connection Hint Setting is
8483
// attempted to be applied by the Config.
85-
ErrMultipleHints = errors.New("config attempted to add multiple transforms")
84+
ErrMultipleHints = xerr.New("config attempted to add multiple transforms")
8685
// ErrInvalidSetting is an error returned by the 'Profile' function if any of the specified Settings are invalid
8786
// or do contain valid information. The error returned will be a wrapped version of this error.
88-
ErrInvalidSetting = errors.New("config setting is invalid")
87+
ErrInvalidSetting = xerr.New("config setting is invalid")
8988
// ErrMultipleTransforms is an error returned by the 'Profile' function if more that one Transform Setting is
9089
// attempted to be applied by the Config. Unlink Wrappers, Transforms cannot be stacked.
91-
ErrMultipleTransforms = errors.New("config attempted to add multiple transforms")
90+
ErrMultipleTransforms = xerr.New("config attempted to add multiple transforms")
9291
)
9392

9493
// Setting is an alias for a byte array that represents a setting in binary form. This can be used inside a

c2/listener.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ package c2
22

33
import (
44
"context"
5-
"errors"
65
"net"
76
"sync/atomic"
87
"time"
98

109
"github.com/iDigitalFlame/xmt/com"
1110
"github.com/iDigitalFlame/xmt/device"
11+
"github.com/iDigitalFlame/xmt/util/xerr"
1212
)
1313

1414
// ErrInvalidPacketCount is returned when attempting to read a packet marked
1515
// as multi or frag an the total count returned is zero.
16-
var ErrInvalidPacketCount = errors.New("frag total is zero on a multi or frag packet")
16+
var ErrInvalidPacketCount = xerr.New("frag total is zero on a multi or frag packet")
1717

1818
// Listener is a struct that is passed back when a C2 Listener is added to the Server. The Listener struct
1919
// allows for controlling the Listener and setting callback functions to be used when a client connects,

c2/proxy.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package c2
22

33
import (
44
"context"
5-
"errors"
65
"net"
76
"sync/atomic"
87

@@ -344,7 +343,7 @@ func (s *Session) Proxy(b string, c serverListener, p *Profile) (*Proxy, error)
344343
return nil, xerr.Wrap("unable to listen on "+b, err)
345344
}
346345
if h == nil {
347-
return nil, errors.New("unable to listen on " + b)
346+
return nil, xerr.New("unable to listen on " + b)
348347
}
349348
if s.log == nil {
350349
s.log = logx.NOP

c2/schedule.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ package c2
22

33
import (
44
"context"
5-
"errors"
65
"strconv"
76
"time"
87

98
"github.com/iDigitalFlame/xmt/c2/task"
109
"github.com/iDigitalFlame/xmt/com"
1110
"github.com/iDigitalFlame/xmt/util"
11+
"github.com/iDigitalFlame/xmt/util/xerr"
1212
)
1313

1414
// ErrCannotAssign is an error returned by the 'Schedule' function when the random loop cannot find a valid
1515
// JobID (unused). This may occur in random circumstances when the Scheduler is overused.
16-
var ErrCannotAssign = errors.New("unable to assign a unused JobID (is Scheduler full?)")
16+
var ErrCannotAssign = xerr.New("unable to assign a unused JobID (is Scheduler full?)")
1717

1818
// Job is a struct that is used to track and manage Tasks given to Session Clients. This struct has function callbacks
1919
// that can be used to watch for completion and also offers a Wait function to pause execution until a response is received.
@@ -149,7 +149,7 @@ func (x *Scheduler) Schedule(s *Session, p *com.Packet) (*Job, error) {
149149
p.Device = s.Device.ID
150150
}
151151
if _, ok := x.jobs[p.Job]; ok {
152-
return nil, errors.New("job ID " + strconv.Itoa(int(p.Job)) + " is already being tracked")
152+
return nil, xerr.New("job ID " + strconv.Itoa(int(p.Job)) + " is already being tracked")
153153
}
154154
if err := s.Write(p); err != nil {
155155
return nil, err

c2/server.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package c2
22

33
import (
44
"context"
5-
"errors"
65
"strings"
76

87
"github.com/PurpleSec/logx"
@@ -26,10 +25,10 @@ const (
2625
var (
2726
// ErrNoConnector is a error returned by the Connect and Listen functions when the Connector is nil and the
2827
// provided Profile is also nil or does not contain a connection hint.
29-
ErrNoConnector = errors.New("invalid or missing connector")
28+
ErrNoConnector = xerr.New("invalid or missing connector")
3029
// ErrEmptyPacket is a error returned by the Connect function when the expected return result from the
3130
// server was invalid or not expected.
32-
ErrEmptyPacket = errors.New("server sent an invalid response")
31+
ErrEmptyPacket = xerr.New("server sent an invalid response")
3332
)
3433

3534
// Server is the manager for all C2 Listener and Sessions connection and states. This struct also manages all
@@ -259,14 +258,14 @@ func (s *Server) Listen(n, b string, c serverListener, p *Profile) (*Listener, e
259258
}
260259
x := strings.ToLower(n)
261260
if _, ok := s.active[x]; ok {
262-
return nil, errors.New("listener " + x + " is already active")
261+
return nil, xerr.New("listener " + x + " is already active")
263262
}
264263
h, err := c.Listen(b)
265264
if err != nil {
266265
return nil, xerr.Wrap("unable to listen on "+b, err)
267266
}
268267
if h == nil {
269-
return nil, errors.New("unable to listen on " + b)
268+
return nil, xerr.New("unable to listen on " + b)
270269
}
271270
if s.Log == nil {
272271
s.Log = logx.NOP

c2/session.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package c2
22

33
import (
44
"context"
5-
"errors"
65
"io"
76
"net"
87
"strconv"
@@ -22,9 +21,9 @@ const maxErrors = 2
2221
var (
2322
// ErrUnable is an error returned for a generic action if there is some condition that prevents the action
2423
// from running.
25-
ErrUnable = errors.New("cannot preform this action")
24+
ErrUnable = xerr.New("cannot preform this action")
2625
// ErrFullBuffer is returned from the WritePacket function when the send buffer for Session is full.
27-
ErrFullBuffer = errors.New("cannot add a Packet to a full send buffer")
26+
ErrFullBuffer = xerr.New("cannot add a Packet to a full send buffer")
2827
)
2928

3029
// Session is a struct that represents a connection between the client and the Listener. This struct does some
@@ -301,7 +300,7 @@ func (c *cluster) add(p *com.Packet) error {
301300
return nil
302301
}
303302
if len(c.data) > 0 && !c.data[0].Belongs(p) {
304-
return errors.New("packet ID " + strconv.FormatUint(uint64(p.ID), 16) + " does not match combining Packet ID")
303+
return xerr.New("packet ID " + strconv.FormatUint(uint64(p.ID), 16) + " does not match combining Packet ID")
305304
}
306305
if p.Flags.Len() > c.max {
307306
c.max = p.Flags.Len()

c2/task/script.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package task
22

33
import (
44
"context"
5-
"errors"
65
"reflect"
76
"strconv"
87

98
"github.com/iDigitalFlame/xmt/com"
109
"github.com/iDigitalFlame/xmt/device"
10+
"github.com/iDigitalFlame/xmt/util/xerr"
1111
)
1212

1313
// Engine is an interface that allows for extending XMT with non-compiled code for easy deployability and flexibility.
@@ -57,10 +57,10 @@ func (s scriptTasker) Do(x context.Context, p *com.Packet) (*com.Packet, error)
5757
// See the 'cmd/script' package for scripting engines.
5858
func RegisterEngine(i uint8, s Engine) error {
5959
if i < 21 {
60-
return errors.New("script mapping ID " + strconv.Itoa(int(i)) + " is invalid")
60+
return xerr.New("script mapping ID " + strconv.Itoa(int(i)) + " is invalid")
6161
}
6262
if Mappings[i] != nil {
63-
return errors.New("script mapping ID " + strconv.Itoa(int(i)) + " is currently used by " + reflect.TypeOf(Mappings[i]).String())
63+
return xerr.New("script mapping ID " + strconv.Itoa(int(i)) + " is currently used by " + reflect.TypeOf(Mappings[i]).String())
6464
}
6565
Mappings[i] = scriptTasker{s}
6666
return nil

c2/transform/dns.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package transform
22

33
import (
4-
"errors"
54
"io"
65
"strings"
76
"sync"
87

98
"github.com/iDigitalFlame/xmt/util"
9+
"github.com/iDigitalFlame/xmt/util/xerr"
1010
)
1111

1212
const (
@@ -38,7 +38,7 @@ var (
3838
// ErrInvalidLength is an error raised by the Read and Write functions
3939
// if the byte array supplied is smaller than the required byte size to
4040
// Transform into a DNS packet.
41-
ErrInvalidLength = errors.New("length of byte array is invalid")
41+
ErrInvalidLength = xerr.New("length of byte array is invalid")
4242

4343
bufs = sync.Pool{
4444
New: func() interface{} {

c2/vars.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package c2
33
import (
44
"bytes"
55
"context"
6-
"errors"
76
"io"
87
"net"
98
"sync"
@@ -155,7 +154,7 @@ func notify(l *Listener, s *Session, p *com.Packet) error {
155154
if p.ID == MvRegister {
156155
p.Device = s.Device.ID
157156
} else {
158-
return errors.New(`received a Session ID "` + p.Device.String() + `"that does not match our own ID "` + s.ID.String() + `"`)
157+
return xerr.New(`received a Session ID "` + p.Device.String() + `"that does not match our own ID "` + s.ID.String() + `"`)
159158
}
160159
}
161160
if l != nil && p.Flags&com.FlagOneshot != 0 {

c2/wrapper/compress.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package wrapper
33
import (
44
"compress/gzip"
55
"compress/zlib"
6-
"errors"
76
"io"
87
"strconv"
8+
9+
"github.com/iDigitalFlame/xmt/util/xerr"
910
)
1011

1112
const (
@@ -28,7 +29,7 @@ type GzipWrap int8
2829
// is invalid.
2930
func NewZlib(level int) (ZlibWrap, error) {
3031
if level < zlib.HuffmanOnly || level > zlib.BestCompression {
31-
return 0, errors.New("invalid compression level " + strconv.Itoa(level))
32+
return 0, xerr.New("invalid compression level " + strconv.Itoa(level))
3233
}
3334
return ZlibWrap(level), nil
3435
}
@@ -37,7 +38,7 @@ func NewZlib(level int) (ZlibWrap, error) {
3738
// is invalid.
3839
func NewGzip(level int) (GzipWrap, error) {
3940
if level < gzip.HuffmanOnly || level > gzip.BestCompression {
40-
return 0, errors.New("invalid compression level " + strconv.Itoa(level))
41+
return 0, xerr.New("invalid compression level " + strconv.Itoa(level))
4142
}
4243
return GzipWrap(level), nil
4344
}

c2/wrapper/crypto.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ package wrapper
22

33
import (
44
"crypto/cipher"
5-
"errors"
65
"io"
76

87
"github.com/iDigitalFlame/xmt/data/crypto"
8+
"github.com/iDigitalFlame/xmt/util/xerr"
99
)
1010

1111
// ErrInvalid is returned when the arguments provided to any of the New* functions when the
1212
// arguments are nil or empty.
13-
var ErrInvalid = errors.New("provided crypto arguments cannot be nil")
13+
var ErrInvalid = xerr.New("provided crypto arguments cannot be nil")
1414

1515
// Block is a struct that contains an IV and Block-based Cipher that can be used to Wrap/Unwrap with the specified
1616
// encryption algorithm.

cmd/cmd.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"bytes"
55
"context"
6-
"errors"
76
"io"
87
"os"
98
"strconv"
@@ -18,20 +17,20 @@ const exitStopped uint32 = 0x1337
1817

1918
var (
2019
// ErrEmptyCommand is an error returned when attempting to start a Process that has an empty 'Args' array.
21-
ErrEmptyCommand = errors.New("process arguments are empty")
20+
ErrEmptyCommand = xerr.New("process arguments are empty")
2221
// ErrNotCompleted is returned when attempting to access the exit code on a running process or wait on a
2322
// non-stared proess.
24-
ErrNotCompleted = errors.New("the process has not yet completed or was not started")
23+
ErrNotCompleted = xerr.New("the process has not yet completed or was not started")
2524
// ErrAlreadyStarted is an error returned by the 'Start' or 'Run' functions when attempting to start a process
2625
// that has already been started via a 'Start' or 'Run' function call.
27-
ErrAlreadyStarted = errors.New("process has already been started")
26+
ErrAlreadyStarted = xerr.New("process has already been started")
2827
// ErrNoProcessFound is returned by the SetParent* functions on Windows devices when a specified parent process
2928
// could not be found.
30-
ErrNoProcessFound = errors.New("could not find a suitable parent process")
29+
ErrNoProcessFound = xerr.New("could not find a suitable parent process")
3130

32-
errStdinSet = errors.New("process Stdin already set")
33-
errStderrSet = errors.New("process Stderr already set")
34-
errStdoutSet = errors.New("process Stdout already set")
31+
errStdinSet = xerr.New("process Stdin already set")
32+
errStderrSet = xerr.New("process Stderr already set")
33+
errStdoutSet = xerr.New("process Stdout already set")
3534
)
3635

3736
// Process is a struct that represents an executable command and allows for setting

cmd/cmd_nix.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
package cmd
44

55
import (
6-
"errors"
76
"os"
87
"os/exec"
98
"sync/atomic"
109
"syscall"
1110

1211
"github.com/iDigitalFlame/xmt/device/devtools"
12+
"github.com/iDigitalFlame/xmt/util/xerr"
1313
)
1414

1515
const (
@@ -51,7 +51,7 @@ func (p *Process) wait() {
5151
// Fork will attempt to use built-in system utilities to fork off the process into a separate, but similar process.
5252
// If successful, this function will return the PID of the new process.
5353
func Fork() (uint32, error) {
54-
return 0, errors.New("currently unimplemented on *nix systems (WIP)")
54+
return 0, xerr.New("currently unimplemented on *nix systems (WIP)")
5555
}
5656

5757
// Pid returns the current process PID. This function returns zero if the process has not been started.

cmd/zcmd_windows.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package cmd
44

55
import (
6-
"errors"
76
"io"
87
"os"
98
"reflect"
@@ -30,7 +29,7 @@ var (
3029

3130
funcAllocConsole = dllKernel32.NewProc("AllocConsole")
3231
funcCreateProcess = dllKernel32.NewProc("CreateProcessW")
33-
funcCreateProcessAsUser = dllKernel32.NewProc("CreateProcessAsUserA")
32+
funcCreateProcessAsUser = dllKernel32.NewProc("CreateProcessAsUserW")
3433
funcUpdateProcThreadAttribute = dllKernel32.NewProc("UpdateProcThreadAttribute")
3534
funcInitializeProcThreadAttributeList = dllKernel32.NewProc("InitializeProcThreadAttributeList")
3635
)
@@ -122,7 +121,7 @@ func createEnv(s []string) (*uint16, error) {
122121
var t, i, l int
123122
for _, s := range s {
124123
if q := strings.IndexByte(s, 61); q <= 0 {
125-
return nil, errors.New(`invalid environment string "` + s + `"`)
124+
return nil, xerr.New(`invalid environment string "` + s + `"`)
126125
}
127126
t += len(s) + 1
128127
}
@@ -308,8 +307,7 @@ func newParentEx(p windows.Handle, i *windows.StartupInfo) (*startupInfoEx, erro
308307
x.StartupInfo.Cb = uint32(unsafe.Sizeof(x))
309308
r, _, err = funcUpdateProcThreadAttribute.Call(
310309
uintptr(unsafe.Pointer(x.AttributeList)), 0, 0x00020000,
311-
uintptr(unsafe.Pointer(&p)), uintptr(unsafe.Sizeof(p)),
312-
uintptr(unsafe.Pointer(nil)), uintptr(unsafe.Pointer(nil)),
310+
uintptr(unsafe.Pointer(&p)), uintptr(unsafe.Sizeof(p)), 0, 0,
313311
)
314312
if r == 0 {
315313
return nil, xerr.Wrap("winapi UpdateProcThreadAttribute error", err)

com/packet.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com
22

33
import (
44
"bytes"
5-
"errors"
65
"strconv"
76

87
"github.com/iDigitalFlame/xmt/data"
@@ -71,7 +70,7 @@ func (p *Packet) Add(n *Packet) error {
7170
return nil
7271
}
7372
if p.ID != n.ID {
74-
return errors.New("Packet ID " + strconv.FormatUint(uint64(n.ID), 16) + " does not match combining Packet ID " + strconv.FormatUint(uint64(p.ID), 16))
73+
return xerr.New("Packet ID " + strconv.FormatUint(uint64(n.ID), 16) + " does not match combining Packet ID " + strconv.FormatUint(uint64(p.ID), 16))
7574
}
7675
if _, err := n.WriteTo(p); err != nil {
7776
return xerr.Wrap("unable to write to Packet", err)

0 commit comments

Comments
 (0)