Skip to content

Commit fa8e67a

Browse files
committed
setup pwn to make a simply docker env for testing locally
fixes: #18
1 parent 7c060a6 commit fa8e67a

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

commands/pwn.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ func guessBinary() string {
5050
}
5151

5252
func makeScript(ip string, port int) {
53+
binary := guessBinary()
54+
5355
script := fmt.Sprintf(
5456
"#!/usr/bin/env python3\n\n"+
5557
"#\n# pwn template made with 🚩 https://github.com/rerrorctf/rctf 🚩\n#\n\n"+
@@ -65,7 +67,7 @@ func makeScript(ip string, port int) {
6567
"#gdb.attach(p, gdbscript=\"\")\n\n"+
6668
"# pwn it here\n\n"+
6769
"p.interactive()\n",
68-
guessBinary(), ip, port)
70+
binary, ip, port)
6971

7072
err := os.WriteFile(config.PwnScriptName, []byte(script), 0644)
7173
if err != nil {
@@ -77,7 +79,41 @@ func makeScript(ip string, port int) {
7779
log.Fatalln("error chmoding file:", err)
7880
}
7981

80-
fmt.Printf("🐚 "+theme.ColorGray+"ready to pwn:"+theme.ColorReset+" $ ./%s\n", config.PwnScriptName)
82+
dockerfile := fmt.Sprintf(
83+
"#\n# Dockerfile template made with 🚩 https://github.com/rerrorctf/rctf 🚩\n#\n\n"+
84+
"FROM ubuntu:24.04\n\n"+
85+
"RUN apt update && apt install -y socat\n\n"+
86+
"RUN groupadd --gid 1001 pwn\n\n"+
87+
"RUN useradd --uid 1001 --gid 1001 --home-dir /home/pwn --create-home --shell /sbin/nologin pwn\n\n"+
88+
"WORKDIR /home/pwn\n\n"+
89+
"COPY %s .\n\n"+
90+
"COPY flag.txt .\n\n"+
91+
"RUN chmod +x ./%s\n\n"+
92+
"EXPOSE %d\n\n"+
93+
"USER pwn\n\n"+
94+
"CMD [\"socat\", \"tcp-listen:%d,fork,reuseaddr\", \"exec:./%s\"]\n",
95+
binary, binary, port, port, binary)
96+
97+
err = os.WriteFile("Dockerfile", []byte(dockerfile), 0644)
98+
if err != nil {
99+
log.Fatalln("error writing to file:", err)
100+
}
101+
102+
compose := fmt.Sprintf(
103+
"#\n# compose.yml template made with 🚩 https://github.com/rerrorctf/rctf 🚩\n#\n\n"+
104+
"services:\n"+
105+
" task:\n"+
106+
" build: .\n"+
107+
" ports:\n"+
108+
" - %d:%d\n",
109+
port, port)
110+
111+
err = os.WriteFile("compose.yml", []byte(compose), 0644)
112+
if err != nil {
113+
log.Fatalln("error writing to file:", err)
114+
}
115+
116+
fmt.Printf("🐚 "+theme.ColorGray+"ready to pwn:"+theme.ColorReset+" $ sudo docker compose up --build -d && ./%s\n", config.PwnScriptName)
81117
}
82118

83119
func Pwn(args []string) {
@@ -95,6 +131,16 @@ func Pwn(args []string) {
95131
log.Fatalf("💥 "+theme.ColorRed+"error"+theme.ColorReset+": \"%s\" already exists!\n", config.PwnScriptName)
96132
}
97133

134+
_, err = os.Stat("./Dockerfile")
135+
if !os.IsNotExist(err) {
136+
log.Fatalf("💥 " + theme.ColorRed + "error" + theme.ColorReset + ": \"Dockerfile\" already exists!\n")
137+
}
138+
139+
_, err = os.Stat("./compose.yml")
140+
if !os.IsNotExist(err) {
141+
log.Fatalf("💥 " + theme.ColorRed + "error" + theme.ColorReset + ": \"Dockerfile\" already exists!\n")
142+
}
143+
98144
var ip string
99145
var port int
100146
util.GetRemoteParams(args, &ip, &port)

0 commit comments

Comments
 (0)