-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
274 lines (235 loc) · 5.6 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/sha256"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
flag "github.com/spf13/pflag"
"github.com/btcsuite/btcutil/base58"
"github.com/fopina/privatebin/types"
"github.com/fopina/privatebin/utils"
"golang.org/x/crypto/pbkdf2"
)
const (
specIterations = 100000
specKeySize = 256
specTagSize = 128
specAlgorithm = "aes"
specMode = "gcm"
specCompression = "none"
pbDefaultURL = "privatebin.net"
pbDefaultExpiration = "1week"
)
// PasteRequest .
type PasteRequest struct {
V int `json:"v"`
AData []interface{} `json:"adata"`
Meta PasteRequestMeta `json:"meta"`
CT string `json:"ct"`
}
// PasteRequestMeta .
type PasteRequestMeta struct {
Expire string `json:"expire"`
}
// PasteResponse .
type PasteResponse struct {
Status int `json:"status"`
ID string `json:"id"`
URL string `json:"url"`
DeleteToken string `json:"deletetoken"`
}
// PasteContent .
type PasteContent struct {
Paste string `json:"paste"`
Attachment string `json:"attachment,omitempty"`
AttachmentName string `json:"attachment_name,omitempty"`
}
// PasteSpec .
type PasteSpec struct {
IV string
Salt string
Iterations int
KeySize int
TagSize int
Algorithm string
Mode string
Compression string
}
// SpecArray .
func (spec *PasteSpec) SpecArray() []interface{} {
return []interface{}{
spec.IV,
spec.Salt,
spec.Iterations,
spec.KeySize,
spec.TagSize,
spec.Algorithm,
spec.Mode,
spec.Compression,
}
}
// PasteData .
type PasteData struct {
*PasteSpec
Data []byte
}
// adata .
func (paste *PasteData) adata() []interface{} {
return []interface{}{
paste.SpecArray(),
"plaintext",
0,
0,
}
}
var version string = "DEV"
var date string
func main() {
versionPtr := flag.BoolP("version", "v", false, "display version")
urlPtr := flag.StringP("url", "u", pbDefaultURL, "privatebin host")
attachmentPtr := flag.StringP("attach", "a", "", "attach a file")
expiration := types.ExpirationValue("1week")
flag.VarP(&expiration, "expire", "e", "expiration")
flag.Parse()
if *versionPtr {
fmt.Println("Version: " + version + " (built on " + date + ")")
return
}
pbURL := strings.TrimRight(*urlPtr, "/")
if !strings.Contains(pbURL, "://") {
pbURL = "https://" + pbURL
}
// Read from STDIN (Piped input)
input, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
// Remove extra line breaks to prevent PrivateBin from breaking.
if bytes.HasSuffix(input, []byte("\n")) {
input = input[:len(input)-1]
}
pc := PasteContent{Paste: utils.StripANSI(string(input))}
if *attachmentPtr != "" {
data, err := ioutil.ReadFile(*attachmentPtr)
if err != nil {
panic(err)
}
pc.Attachment = utils.Base64(data)
pc.AttachmentName = filepath.Base(*attachmentPtr)
}
// Marshal the paste content to escape JSON characters.
pasteContent, err := json.Marshal(&pc)
if err != nil {
panic(err)
}
// Generate a master key for the paste.
masterKey, err := utils.GenRandomBytes(32)
if err != nil {
panic(err)
}
// Encrypt the paste data
pasteData, err := encrypt(masterKey, pasteContent)
if err != nil {
panic(err)
}
// Create a new Paste Request.
pasteRequest := &PasteRequest{
V: 2,
AData: pasteData.adata(),
Meta: PasteRequestMeta{
Expire: expiration.String(),
},
CT: utils.Base64(pasteData.Data),
}
// Get the Request Body.
body, err := json.Marshal(pasteRequest)
if err != nil {
panic(err)
}
// Create a new HTTP Client and HTTP Request.
client := &http.Client{}
req, err := http.NewRequest("POST", pbURL, bytes.NewBuffer(body))
if err != nil {
panic(err)
}
// Set the request headers.
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Content-Length", strconv.Itoa(len(body)))
req.Header.Set("X-Requested-With", "JSONHttpRequest")
// Run the http request.
res, err := client.Do(req)
if err != nil {
panic(err)
}
// Close the request body once we are done.
defer func() {
if err := res.Body.Close(); err != nil {
panic(err)
}
}()
// Read the response body.
response, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
// Decode the response.
pasteResponse := &PasteResponse{}
if err := json.Unmarshal(response, &pasteResponse); err != nil {
panic(err)
}
fmt.Printf("%s%s#%s\n", pbURL, pasteResponse.URL, base58.Encode(masterKey))
}
func encrypt(master []byte, message []byte) (*PasteData, error) {
// Generate a initialization vector.
iv, err := utils.GenRandomBytes(12)
if err != nil {
return nil, err
}
// Generate salt.
salt, err := utils.GenRandomBytes(8)
if err != nil {
return nil, err
}
// Create the Paste Data and generate a key.
paste := &PasteData{
PasteSpec: &PasteSpec{
IV: utils.Base64(iv),
Salt: utils.Base64(salt),
Iterations: specIterations,
KeySize: specKeySize,
TagSize: specTagSize,
Algorithm: specAlgorithm,
Mode: specMode,
Compression: specCompression,
},
}
key := pbkdf2.Key(master, salt, paste.Iterations, 32, sha256.New)
// Get the "adata" for the paste.
adata, err := json.Marshal(paste.adata())
if err != nil {
return nil, err
}
// Create a new Cipher
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
// Create a new GCM.
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
// Sign the message.
data := gcm.Seal(nil, iv, message, adata)
// Update and return the paste data.
paste.Data = data
return paste, nil
}