-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpaste.go
181 lines (159 loc) · 4.21 KB
/
paste.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
package main
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"path"
"strings"
"text/tabwriter"
log "github.com/sirupsen/logrus"
"golang.org/x/exp/maps"
gomail "gopkg.in/gomail.v2"
)
const (
apiEndpoint = "https://scrape.pastebin.com/api_scraping.php"
)
type paste struct {
FullURL string `json:"full_url"`
ScrapeURL string `json:"scrape_url"`
Date string `json:"date"`
Key string `json:"key"`
Size string `json:"size"`
Expire string `json:"expire"`
Title string `json:"title"`
Syntax string `json:"syntax"`
Hits string `json:"hits"`
User string `json:"user"`
Content string
Matches map[string][]string
}
func (p *paste) String() string {
var buffer bytes.Buffer
bw := bufio.NewWriter(&buffer)
tw := tabwriter.NewWriter(bw, 0, 5, 3, ' ', 0)
keywords := strings.Join(maps.Keys(p.Matches), ", ")
if _, err := fmt.Fprintf(tw, "Pastebin Alert for Keywords %s\n\n", keywords); err != nil {
return fmt.Sprintf("error on tostring: %v", err)
}
fields := []struct {
prefix string
content string
}{
{"Title", p.Title},
{"URL", p.FullURL},
{"User", p.User},
{"Date", dateToString(p.Date)},
{"Size", p.Size},
{"Expire", dateToString(p.Expire)},
{"Syntax", p.Syntax},
{"Hits", p.Hits},
}
for _, x := range fields {
if x.content != "" {
if _, err := fmt.Fprintf(tw, "%s:\t%s\n", x.prefix, x.content); err != nil {
return fmt.Sprintf("error on tostring: %v", err)
}
}
}
if err := tw.Flush(); err != nil {
return fmt.Sprintf("error on tostring: %v", err)
}
for k, v := range p.Matches {
if _, err := fmt.Fprintf(bw, "\nMatches for %s:\n", k); err != nil {
return fmt.Sprintf("error on tostring: %v", err)
}
for _, m := range v {
if _, err := fmt.Fprintf(bw, "%s\n", m); err != nil {
return fmt.Sprintf("error on tostring: %v", err)
}
}
}
if err := bw.Flush(); err != nil {
return fmt.Sprintf("error on tostring: %v", err)
}
return buffer.String()
}
func (p *paste) sendPasteMessage(config configuration) error {
m := gomail.NewMessage()
m.SetHeader("From", config.Mailfrom)
m.SetHeader("To", config.Mailto)
keywords := strings.Join(maps.Keys(p.Matches), ", ")
m.SetHeader("Subject", fmt.Sprintf("Pastebin Alert for %s", keywords))
filename := fmt.Sprintf("%s.zip", randomString(10))
fullPath := path.Join(os.TempDir(), filename)
zipFile, err := createZip("content.txt", p.Content)
if err != nil {
return err
}
f, err := os.Create(fullPath)
if err != nil {
return err
}
defer f.Close()
defer os.Remove(fullPath)
_, err = f.Write(zipFile)
if err != nil {
return err
}
m.Attach(fullPath)
m.SetBody("text/plain", p.String())
err = sendEmail(config, m)
return err
}
func (p paste) fetch(ctx context.Context, keywords *map[string]keywordType, cidrs *[]cidrType) (*paste, error) {
log.Debugf("checking paste %s", p.Key)
resp, err := httpRequest(ctx, p.ScrapeURL)
if err != nil {
// Ignore HTTP based errors like timeout and connection reset
return nil, nil
}
if resp.StatusCode == http.StatusOK || resp.ContentLength > 0 {
b, err := httpRespBodyToString(resp)
if err != nil {
return nil, err
}
found, key := checkKeywords(b, keywords)
found2, key2 := checkCIDRs(b, cidrs)
if found || found2 {
// merge key1 and key2
for k, v := range key2 {
key[k] = v
}
p.Content = b
p.Matches = key
return &p, nil
}
} else {
b, err := httpRespBodyToString(resp)
return nil, fmt.Errorf("output: %s, error: %v", b, err)
}
// nothing found
return nil, nil
}
func fetchPasteList(ctx context.Context) ([]paste, error) {
var list []paste
log.Debugf("fetching paste list")
url := fmt.Sprintf("%s?limit=100", apiEndpoint)
resp, err := httpRequest(ctx, url)
if err != nil {
// Ignore HTTP based errors like timeout and connection reset
return list, nil
}
body, err := httpRespBodyToString(resp)
if err != nil {
return list, err
}
// ip does not have access. Do not panic so error mail will be sent
if strings.Contains(body, "DOES NOT HAVE ACCESS") {
return list, fmt.Errorf("%s", body)
}
jsonErr := json.Unmarshal([]byte(body), &list)
if jsonErr != nil {
return list, fmt.Errorf("error on parsing json: %v. json: %s", jsonErr, body)
}
return list, nil
}