Skip to content

use the task's metadata to populate the writeup #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ the ctftime urls will be used to aid in the generation of writeups with the `wri

---

### 🌹 <u>de</u>scription
### 🗣️ <u>de</u>scription

```
$ ret description [task-description]
Expand Down Expand Up @@ -521,13 +521,6 @@ the writeup will be saved in a file called `writeup.md`
if a file called `writeup.md` already exists the command will abort
there is a small window for a time-of-check/time-of-use race here - you have been warned!

1. uses the first url from `"ctftimeurls"` to insert a url at the top of the writeup. inserts the rest as comments if there are more than one
2. imports all notes taken with the `notes` command into the description area
3. creates a space for a python script and then imports the script created by `pwn` if it exists
4. imports the flag captured with the `capture` command if it exists
5. uses the `"username"` from `~/.config/ret` to attribute to this writeup to you
6. inserts a date stamp for today's date using yyyy/mm/dd format

🔗 https://github.com/rerrorctf/ret/blob/main/commands/writeup.go

---
Expand Down
2 changes: 1 addition & 1 deletion commands/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func init() {
List: false,
},
},
SeeAlso: nil})
SeeAlso: []string{"name", "description", "event", "remote", "writeup"}})
}

func CategoryHelp() string {
Expand Down
10 changes: 5 additions & 5 deletions commands/description.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func init() {
Commands = append(Commands, Command{
Name: "description",
Emoji: "🌹", // a rose by any other name would smell as 1337
Emoji: "🗣️", // a rose by any other name would smell as 1337
Func: Description,
Help: DescriptionHelp,
Arguments: []Argument{
Expand All @@ -20,7 +20,7 @@ func init() {
List: false,
},
},
SeeAlso: nil})
SeeAlso: []string{"name", "category", "event", "remote", "writeup"}})
}

func DescriptionHelp() string {
Expand All @@ -36,16 +36,16 @@ func displayCurrentTaskDescription() {
return
}

fmt.Printf("🌹 "+theme.ColorBlue+"%s"+theme.ColorReset+"\n", description)
fmt.Printf("🗣️ "+theme.ColorBlue+"%s"+theme.ColorReset+"\n", description)
}

func setCurrentTaskDescription(newDescription string) {
oldDescription := util.GetCurrentTaskDescription()

if len(oldDescription) > 0 {
fmt.Printf(theme.ColorGray+"🌹 changing description from: "+theme.ColorRed+"%s"+theme.ColorGray+" to: "+theme.ColorGreen+"%s"+theme.ColorReset+"\n", oldDescription, newDescription)
fmt.Printf(theme.ColorGray+"🗣️ changing description from: "+theme.ColorRed+"%s"+theme.ColorGray+" to: "+theme.ColorGreen+"%s"+theme.ColorReset+"\n", oldDescription, newDescription)
} else {
fmt.Printf(theme.ColorGray+"🌹 setting description to: "+theme.ColorGreen+"%s"+theme.ColorReset+"\n", newDescription)
fmt.Printf(theme.ColorGray+"🗣️ setting description to: "+theme.ColorGreen+"%s"+theme.ColorReset+"\n", newDescription)
}

util.SetCurrentTaskDescription(newDescription)
Expand Down
2 changes: 1 addition & 1 deletion commands/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func init() {
List: false,
},
},
SeeAlso: nil})
SeeAlso: []string{"name", "category", "description", "remote", "writeup"}})
}

func EventHelp() string {
Expand Down
2 changes: 1 addition & 1 deletion commands/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func init() {
List: false,
},
},
SeeAlso: nil})
SeeAlso: []string{"event", "category", "description", "remote", "writeup"}})
}

func NameHelp() string {
Expand Down
2 changes: 1 addition & 1 deletion commands/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func init() {
Default: "9001",
},
},
SeeAlso: nil})
SeeAlso: []string{"name", "event", "category", "description", "writeup"}})
}

func RemoteHelp() string {
Expand Down
65 changes: 27 additions & 38 deletions commands/writeup.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package commands

import (
"encoding/json"
"fmt"
"log"
"os"
"ret/config"
"ret/data"
"ret/theme"
"ret/util"
"time"
Expand All @@ -19,20 +17,14 @@ func init() {
Func: Writeup,
Help: WriteupHelp,
Arguments: nil,
SeeAlso: []string{"notes", "pwn", "capture", "ctftime"}})
SeeAlso: []string{"name", "category", "event", "remote", "description", "pwn", "capture"}})
}

func WriteupHelp() string {
return "create a markdown writeup using a template with ret\n\n" +
"the writeup will be saved in a file called `writeup.md`\n\n" +
"if a file called `writeup.md` already exists the command will abort\n" +
"there is a small window for a time-of-check/time-of-use race here - you have been warned!\n\n" +
"1. uses the first url from " + theme.ColorYellow + "`\"ctftimeurls\"`" + theme.ColorReset + " to insert a url at the top of the writeup. inserts the rest as comments if there are more than one\n" +
"2. imports all notes taken with the " + theme.ColorGreen + "`notes`" + theme.ColorReset + " command into the description area\n" +
"3. creates a space for a python script and then imports the script created by " + theme.ColorGreen + "`pwn`" + theme.ColorReset + " if it exists\n" +
"4. imports the flag captured with the " + theme.ColorGreen + "`capture`" + theme.ColorReset + " command if it exists\n" +
"5. uses the " + theme.ColorYellow + "`\"username\"`" + theme.ColorReset + " from " + theme.ColorCyan + "`~/.config/ret`" + theme.ColorReset + " to attribute to this writeup to you\n" +
"6. inserts a date stamp for today's date using yyyy/mm/dd format\n"
"there is a small window for a time-of-check/time-of-use race here - you have been warned!\n"
}

func Writeup(args []string) {
Expand All @@ -44,57 +36,54 @@ func Writeup(args []string) {
log.Fatalf("💥 "+theme.ColorRed+"error"+theme.ColorReset+": \"%s\" already exists!\n", filePath)
}

urls := ""
if len(config.CtfTimeUrls) > 0 {
urls = config.CtfTimeUrls[0]
event := util.GetCurrentTaskEvent()
if len(event) == 0 {
event = "https://ctftime.link.goes.here"
}

for _, ctfTimeUrl := range config.CtfTimeUrls[1:] {
urls += "\n<!-- " + ctfTimeUrl + " -->"
name := util.GetCurrentTaskName()
if len(name) == 0 {
name = "NAME"
}

if urls == "" {
urls = "https://ctftime.link.goes.here"
category := util.GetCurrentTaskCategory()
if len(category) == 0 {
category = "CATEGORY"
}

flag := util.GetCurrentTaskFlag()
if len(flag) == 0 {
flag = config.FlagFormat
description := util.GetCurrentTaskDescription()
if len(description) == 0 {
description = "DESCRIPTION"
}

notesStr := ""
ip := util.GetCurrentTaskIp()
port := util.GetCurrentTaskPort()

jsonData, err := os.ReadFile(config.NotesFileName)
if err == nil {
var notes data.Notes
err = json.Unmarshal(jsonData, &notes)
if err == nil {
for _, note := range notes.Notes {
notesStr += fmt.Sprintf("%s\n\n", note.Note)
}
}
script, _ := os.ReadFile("./" + config.PwnScriptName)

flag := util.GetCurrentTaskFlag()
if len(flag) == 0 {
flag = config.FlagFormat
}

script, _ := os.ReadFile("./" + config.PwnScriptName)

name := config.Username
if name == "" {
name = "YOUR-NAME-GOES-HERE"
username := config.Username
if username == "" {
username = "YOUR-NAME-GOES-HERE"
}

date := time.Now().Format("2006/01/02")

template := fmt.Sprintf(
"%s\n\n"+
"# NAME (CATEGORY)\n\n"+
"%s"+
"# %s (%s)\n\n"+
"%s\n\n"+
"nc %s %d\n\n"+
"## Solution\n\n"+
"```python\n"+
"%s"+
"```\n\n"+
"## Flag\n`%s`\n\n"+
"%s %s\n", urls, notesStr, script, flag, name, date)
"%s %s\n", event, name, category, description, ip, port, script, flag, username, date)

err = os.WriteFile(filePath, []byte(template), 0644)

Expand Down