Skip to content

Commit edd85a2

Browse files
authored
Merge pull request #191 from dmur1/add-share-command-#185
Add share command #185
2 parents 02a344b + d0ba51a commit edd85a2

File tree

3 files changed

+122
-43
lines changed

3 files changed

+122
-43
lines changed

commands/gist.go

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package commands
22

33
import (
4-
"bytes"
5-
"encoding/json"
64
"fmt"
7-
"io"
85
"log"
9-
"net/http"
106
"os"
117
"ret/config"
128
"ret/theme"
9+
"ret/util"
1310
"strings"
1411
)
1512

@@ -36,7 +33,7 @@ func GistHelp() string {
3633

3734
func Gist(args []string) {
3835
if len(args) == 0 {
39-
log.Fatalf("💥 " + theme.ColorRed + "error" + theme.ColorReset + ": exepcted 1 or more arguments\n")
36+
log.Fatalf("💥 " + theme.ColorRed + "error" + theme.ColorReset + ": expected 1 or more arguments\n")
4037
}
4138

4239
if len(config.GistToken) == 0 {
@@ -59,42 +56,5 @@ func Gist(args []string) {
5956
}
6057
}
6158

62-
gist := map[string]interface{}{
63-
"description": "🐙 made with https://github.com/rerrorctf/ret",
64-
"public": false,
65-
"files": files,
66-
}
67-
68-
body, err := json.Marshal(gist)
69-
if err != nil {
70-
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": %v\n", err)
71-
}
72-
73-
req, err := http.NewRequest("POST", "https://api.github.com/gists", bytes.NewBuffer(body))
74-
if err != nil {
75-
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": %v\n", err)
76-
}
77-
req.Header.Set("Accept", "application/vnd.github+json")
78-
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", config.GistToken))
79-
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
80-
81-
client := &http.Client{}
82-
resp, err := client.Do(req)
83-
if err != nil {
84-
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": %v\n", err)
85-
}
86-
87-
body, err = io.ReadAll(resp.Body)
88-
if err != nil {
89-
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": %v\n", err)
90-
}
91-
92-
var result map[string]interface{}
93-
if err := json.Unmarshal(body, &result); err != nil {
94-
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": %v\n", err)
95-
}
96-
97-
fmt.Printf("%s\n", result["html_url"].(string))
98-
99-
resp.Body.Close()
59+
fmt.Printf("%s\n", util.Gist(files))
10060
}

commands/share.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"ret/config"
8+
"ret/theme"
9+
"ret/util"
10+
)
11+
12+
func init() {
13+
Commands = append(Commands, Command{
14+
Name: "share",
15+
Emoji: "🌐",
16+
Func: Share,
17+
Help: ShareHelp,
18+
Url: "https://github.com/rerrorctf/ret/blob/main/commands/share.go",
19+
Arguments: nil,
20+
SeeAlso: []string{"notes", "capture", "chat", "gist", "pwn"}})
21+
}
22+
23+
func ShareHelp() string {
24+
return "share task progress with ret\n\n"
25+
}
26+
27+
func Share(args []string) {
28+
if len(config.GistToken) == 0 {
29+
log.Fatalf("💥 " + theme.ColorRed + "error" + theme.ColorReset + ": no gist token in ~/.config/ret\n")
30+
}
31+
32+
files := map[string]interface{}{}
33+
34+
buffer, err := os.ReadFile(config.PwnScriptName)
35+
if err == nil {
36+
files[config.PwnScriptName] = map[string]interface{}{
37+
"content": string(buffer),
38+
}
39+
}
40+
41+
buffer, err = os.ReadFile(config.NotesFileName)
42+
if err == nil {
43+
// does not like .ret/notes.json
44+
files["notes.json"] = map[string]interface{}{
45+
"content": string(buffer),
46+
}
47+
}
48+
49+
flag, err := util.GetCurrentFlag()
50+
if err != nil {
51+
flag = config.FlagFormat
52+
} else {
53+
files["flag.txt"] = map[string]interface{}{
54+
"content": string(flag),
55+
}
56+
}
57+
58+
gistUrl := ""
59+
if len(files) > 0 {
60+
gistUrl = util.Gist(files)
61+
}
62+
63+
Chat([]string{fmt.Sprintf("🏁 %s\n**%s**", flag, gistUrl)})
64+
}

util/gist.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package util
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"log"
9+
"net/http"
10+
"ret/config"
11+
"ret/theme"
12+
)
13+
14+
func Gist(files map[string]interface{}) string {
15+
gist := map[string]interface{}{
16+
"description": "🐙 made with https://github.com/rerrorctf/ret",
17+
"public": false,
18+
"files": files,
19+
}
20+
21+
body, err := json.Marshal(gist)
22+
if err != nil {
23+
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": %v\n", err)
24+
}
25+
26+
req, err := http.NewRequest("POST", "https://api.github.com/gists", bytes.NewBuffer(body))
27+
if err != nil {
28+
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": %v\n", err)
29+
}
30+
req.Header.Set("Accept", "application/vnd.github+json")
31+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", config.GistToken))
32+
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
33+
34+
client := &http.Client{}
35+
resp, err := client.Do(req)
36+
if err != nil {
37+
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": %v\n", err)
38+
}
39+
40+
body, err = io.ReadAll(resp.Body)
41+
if err != nil {
42+
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": %v\n", err)
43+
}
44+
45+
var result map[string]interface{}
46+
if err := json.Unmarshal(body, &result); err != nil {
47+
log.Fatalf("💥 "+theme.ColorRed+" error"+theme.ColorReset+": %v\n", err)
48+
}
49+
50+
url := result["html_url"].(string)
51+
52+
resp.Body.Close()
53+
54+
return url
55+
}

0 commit comments

Comments
 (0)