This repository has been archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
129 lines (110 loc) · 2.61 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"text/template"
"golang.org/x/crypto/openpgp"
)
type homePageData struct {
ShowError bool
Err string
}
var tmpl = template.Must(template.ParseFiles("./public/index.html"))
var privateKey *openpgp.Entity
var flag []byte
func main() {
keyfile, err := os.Open("privatekey.asc")
if err != nil {
panic(err)
}
keyring, err := openpgp.ReadArmoredKeyRing(keyfile)
if err != nil {
panic(err)
}
privateKey = keyring[0]
password, ok := os.LookupEnv("KEY_PASSWORD")
if !ok {
panic("Missing KEY_PASSWORD enviornment variable")
}
privateKey.PrivateKey.Decrypt([]byte(password))
flagFile, err := os.Open("./flag.txt")
if err != nil {
panic(err)
}
flag, err = ioutil.ReadAll(flagFile)
if err != nil {
panic(err)
}
http.HandleFunc("/sendKey", sendKey)
http.HandleFunc("/", sendIndex)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func sendIndex(rw http.ResponseWriter, req *http.Request) {
err := tmpl.Execute(rw, homePageData{
ShowError: false,
Err: "",
})
if err != nil {
panic(err)
}
}
func getIdentities(m map[string]*openpgp.Identity) []string {
keys := make([]string, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
func sendKey(rw http.ResponseWriter, req *http.Request) {
if req.FormValue("key") == "" {
rw.WriteHeader(http.StatusBadRequest)
tmpl.Execute(rw, homePageData{
ShowError: true,
Err: "You must submit your key",
})
return
}
keyReader := strings.NewReader(req.FormValue("key"))
list, err := openpgp.ReadArmoredKeyRing(keyReader)
if err != nil {
rw.WriteHeader(http.StatusBadRequest)
tmpl.Execute(rw, homePageData{
ShowError: true,
Err: "There was an error reading your key",
})
return
}
key := list[0]
if key.PrimaryKey.PublicKey == nil {
rw.WriteHeader(http.StatusBadRequest)
tmpl.Execute(rw, homePageData{
ShowError: true,
Err: "That doesn't look like a PGP public key.",
})
return
}
rw.Header().Add("Content-Disposition", "attachment; filename=\"flag.txt.gpg\"")
rw.Header().Add("Content-Type", "application/pgp-encrypted") // RFC3156
w, err := openpgp.Encrypt(rw, list, privateKey, nil, nil)
if err != nil {
panic(err)
}
identities := getIdentities(key.Identities)
fmt.Fprintf(w, `
Hey %s,
This flag was made just for you: %s
Enjoy!`, strings.Split(identities[1], " <")[0], flag)
err = w.Close()
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
tmpl.Execute(rw, homePageData{
ShowError: true,
Err: "An internal server error occured while encrypting your flag.",
})
return
}
}