Skip to content

Commit d59517a

Browse files
committed
wizard go brr
fixes: #33
1 parent 4be6abc commit d59517a

File tree

6 files changed

+194
-36
lines changed

6 files changed

+194
-36
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,14 @@ Prints a list of cheatsheets for quick reference.
259259

260260
https://github.com/rerrorctf/rctf/blob/main/commands/cheatsheet.go
261261

262+
### wizard
263+
264+
```
265+
usage: rctf wizard
266+
```
267+
268+
https://github.com/rerrorctf/rctf/blob/main/commands/wizard.go
269+
262270
## The .rctf Directory Structure
263271

264272
Certain commands, such as `init`, `add` and `status` will use a hidden directory structure.

commands/add.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"io"
1111
"log"
1212
"os"
13-
"os/exec"
1413
"path/filepath"
1514
"rctf/config"
1615
"rctf/data"
@@ -61,18 +60,6 @@ func parseFiles(files *data.Files) {
6160
}
6261
}
6362

64-
func runFileCommandOnFile(path string) string {
65-
fileOutput := exec.Command("file", path)
66-
67-
fileOutputResult, err := fileOutput.Output()
68-
if err != nil {
69-
fmt.Printf("warning: unable to get output from file on %s\n", path)
70-
return ""
71-
}
72-
73-
return string(fileOutputResult[len(path)+2 : len(fileOutputResult)-1])
74-
}
75-
7663
func writeFiles(files *data.Files) {
7764
jsonData, err := json.MarshalIndent(files, "", " ")
7865
if err != nil {
@@ -95,7 +82,7 @@ func addFile(srcPath string) {
9582

9683
_, fileName := filepath.Split(srcPath)
9784

98-
fileOutput := runFileCommandOnFile(srcPath)
85+
fileOutput := util.RunFileCommandOnFile(srcPath)
9986

10087
content, err := os.ReadFile(srcPath)
10188
if err != nil {

commands/wizard.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"rctf/config"
8+
"rctf/theme"
9+
"rctf/util"
10+
"strings"
11+
)
12+
13+
func findInterestingFiles() []string {
14+
files, err := os.ReadDir(".")
15+
if err != nil {
16+
log.Fatalf("💥 " + theme.ColorRed + "error" + theme.ColorReset + ": unable read cwd!\n")
17+
}
18+
19+
interestingFiles := []string{}
20+
21+
for _, file := range files {
22+
if file.IsDir() {
23+
continue
24+
}
25+
26+
fileName := file.Name()
27+
28+
if fileName[0] == '.' {
29+
continue
30+
}
31+
32+
interestingFiles = append(interestingFiles, fileName)
33+
}
34+
35+
if len(interestingFiles) < 1 {
36+
fmt.Printf("🧙💬 " + theme.ColorGreen + "I don't see any interesting files here..." + theme.ColorReset + "\n")
37+
return interestingFiles
38+
}
39+
40+
if len(interestingFiles) > 1 {
41+
fmt.Printf("🧙💬 "+theme.ColorGreen+"I see %d interesting files here..."+theme.ColorReset+"\n", len(interestingFiles))
42+
} else {
43+
fmt.Printf("🧙💬 " + theme.ColorGreen + "I see an interesting file here..." + theme.ColorReset + "\n")
44+
}
45+
46+
for _, file := range interestingFiles {
47+
fmt.Printf(" 👀 "+theme.ColorCyan+"%s\n"+theme.ColorReset, file)
48+
}
49+
50+
return interestingFiles
51+
}
52+
53+
func Wizard(args []string) {
54+
if len(args) > 0 {
55+
switch args[0] {
56+
case "help":
57+
fmt.Fprintf(os.Stderr, theme.ColorGreen+"usage"+theme.ColorReset+": rctf "+theme.ColorBlue+"wizard"+theme.ColorReset+"\n")
58+
fmt.Fprintf(os.Stderr, " 🧙 do "+theme.ColorPurple+"magic"+theme.ColorReset+" with rctf\n")
59+
os.Exit(0)
60+
}
61+
}
62+
63+
// create interesting files list
64+
interestingFiles := findInterestingFiles()
65+
66+
// ensure skeleton
67+
_, err := os.Stat(config.FolderName)
68+
if os.IsNotExist(err) {
69+
fmt.Printf("🧙💬 "+theme.ColorGreen+"I see that you don't have a %s.. let me create that for you!"+theme.ColorReset+" 🪄\n", config.FolderName)
70+
util.EnsureSkeleton()
71+
}
72+
73+
// init task
74+
_, err = os.Stat(config.TaskName)
75+
if os.IsNotExist(err) {
76+
fmt.Printf("🧙💬 "+theme.ColorGreen+"I see that you don't have a %s.. let me create that for you!"+theme.ColorReset+" 🪄\n", config.TaskName)
77+
Init([]string{"flag{.+}"})
78+
}
79+
80+
unzippedAny := false
81+
82+
// unzip
83+
for _, file := range interestingFiles {
84+
result := util.RunFileCommandOnFile(file)
85+
86+
if strings.Contains(result, "Zip archive data") {
87+
fmt.Printf("🧙💬 "+theme.ColorGreen+"I see that %s is a zip file.. let me unzip that for you!"+theme.ColorReset+" 🪄\n", file)
88+
89+
util.UnzipFile(file)
90+
91+
unzippedAny = true
92+
}
93+
}
94+
95+
if unzippedAny {
96+
interestingFiles = findInterestingFiles()
97+
}
98+
99+
// add files
100+
if len(interestingFiles) > 1 {
101+
fmt.Printf("🧙💬 " + theme.ColorGreen + "Let me add those interesting files for you!" + theme.ColorReset + " 🪄\n")
102+
} else {
103+
fmt.Printf("🧙💬 " + theme.ColorGreen + "Let me add that interesting file for you!" + theme.ColorReset + " 🪄\n")
104+
}
105+
106+
Add(interestingFiles)
107+
108+
// show status
109+
Status([]string{})
110+
111+
// if binary then pwn
112+
for _, file := range interestingFiles {
113+
result := util.RunFileCommandOnFile(file)
114+
115+
if strings.Contains(result, "ELF") {
116+
fmt.Printf("🧙💬 "+theme.ColorGreen+"I see that %s is an ELF.. let me pwn that for you!"+theme.ColorReset+" 🪄\n", file)
117+
118+
Pwn([]string{})
119+
break
120+
}
121+
}
122+
}

main.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"rctf/config"
1313
"rctf/data"
1414
"rctf/theme"
15+
"rctf/util"
1516
)
1617

1718
func parseUserConfig() {
@@ -61,24 +62,6 @@ func parseUserConfig() {
6162
}
6263
}
6364

64-
func ensureDirectory(dirPath string) {
65-
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
66-
if config.Verbose {
67-
fmt.Println("mkdir", dirPath)
68-
}
69-
err := os.MkdirAll(dirPath, 0755)
70-
if err != nil {
71-
fmt.Println("error creating directory:", err)
72-
os.Exit(1)
73-
}
74-
}
75-
}
76-
77-
func ensureSkeleton() {
78-
ensureDirectory(config.FolderName)
79-
ensureDirectory(config.FilesFolderName)
80-
}
81-
8265
func main() {
8366
flag.BoolVar(&config.Verbose, "v", false, "enable verbose mode")
8467

@@ -100,6 +83,7 @@ func main() {
10083
fmt.Fprintf(os.Stderr, " 📞 "+theme.ColorBlue+"syscall"+theme.ColorReset+"\n")
10184
fmt.Fprintf(os.Stderr, " 📝 "+theme.ColorBlue+"writeup"+theme.ColorReset+"\n")
10285
fmt.Fprintf(os.Stderr, " 📚 "+theme.ColorBlue+"cheatsheet"+theme.ColorReset+"\n")
86+
fmt.Fprintf(os.Stderr, " 🧙 "+theme.ColorBlue+"wizard"+theme.ColorReset+"\n")
10387
fmt.Fprintf(os.Stderr, "\n🚩 https://github.com/rerrorctf/rctf 🚩\n")
10488
}
10589

@@ -114,10 +98,10 @@ func main() {
11498

11599
switch flag.Arg(0) {
116100
case "init":
117-
ensureSkeleton()
101+
util.EnsureSkeleton()
118102
commands.Init(flag.Args()[1:])
119103
case "add":
120-
ensureSkeleton()
104+
util.EnsureSkeleton()
121105
commands.Add(flag.Args()[1:])
122106
case "status":
123107
commands.Status(flag.Args()[1:])
@@ -126,10 +110,10 @@ func main() {
126110
case "docker":
127111
commands.Docker(flag.Args()[1:])
128112
case "ghidra":
129-
ensureSkeleton()
113+
util.EnsureSkeleton()
130114
commands.Ghidra(flag.Args()[1:])
131115
case "ida":
132-
ensureSkeleton()
116+
util.EnsureSkeleton()
133117
commands.Ida(flag.Args()[1:])
134118
case "monitor":
135119
commands.Monitor(flag.Args()[1:])
@@ -141,6 +125,8 @@ func main() {
141125
commands.Writeup(flag.Args()[1:])
142126
case "cheatsheet":
143127
commands.Cheatsheet(flag.Args()[1:])
128+
case "wizard":
129+
commands.Wizard(flag.Args()[1:])
144130
default:
145131
flag.Usage()
146132
os.Exit(1)

util/directory.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package util
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"rctf/config"
7+
)
8+
9+
func EnsureDirectory(dirPath string) {
10+
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
11+
if config.Verbose {
12+
fmt.Println("mkdir", dirPath)
13+
}
14+
err := os.MkdirAll(dirPath, 0755)
15+
if err != nil {
16+
fmt.Println("error creating directory:", err)
17+
os.Exit(1)
18+
}
19+
}
20+
}
21+
22+
func EnsureSkeleton() {
23+
EnsureDirectory(config.FolderName)
24+
EnsureDirectory(config.FilesFolderName)
25+
}

util/file.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package util
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
)
7+
8+
func RunFileCommandOnFile(path string) string {
9+
fileOutput := exec.Command("file", path)
10+
11+
fileOutputResult, err := fileOutput.Output()
12+
if err != nil {
13+
fmt.Printf("warning: unable to get output from file on %s\n", path)
14+
return ""
15+
}
16+
17+
return string(fileOutputResult[len(path)+2 : len(fileOutputResult)-1])
18+
}
19+
20+
func UnzipFile(path string) {
21+
fileOutput := exec.Command("unzip", path)
22+
23+
fileOutputResult, err := fileOutput.Output()
24+
if err != nil {
25+
fmt.Printf("warning: unable to unzip file %s\n", path)
26+
return
27+
}
28+
29+
fmt.Printf("%s", fileOutputResult)
30+
}

0 commit comments

Comments
 (0)