@@ -2,153 +2,64 @@ package main
2
2
3
3
import (
4
4
"fmt"
5
- "regexp"
6
5
"strings"
7
6
)
8
7
9
- var reCreds = regexp .MustCompile ("(?m)^([a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+\\ .[a-zA-Z]+):([^ ~/$| ].*$)" )
10
- var reEmail = regexp .MustCompile ("[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+\\ .[a-zA-Z]+" )
11
- var rePrivKey = regexp .MustCompile ("(?s)BEGIN (RSA|DSA|) PRIVATE KEY.*END (RSA|DSA|) PRIVATE KEY" )
12
- var reAwsKey = regexp .MustCompile ("(?is)(AKIA[A-Z0-9]{16})[\" ',: =]+([A-Za-z0-9+/]{40})" )
13
- var reBase64 = regexp .MustCompile ("^([a-zA-Z0-9+/]+)$" )
14
-
15
- // Find AWS access keys and secrets
16
- func processAWSKeys (contents , key string ) bool {
17
- // Base64 content yields false positives. Skip documents that are only Base64
18
- if b64 := reBase64 .FindString (contents ); b64 != "" {
19
- return false
20
- }
21
-
22
- awsKeys := reAwsKey .FindAllStringSubmatch (contents , - 1 )
23
-
24
- // No keys found.
25
- if awsKeys == nil {
26
- return false
27
- }
28
-
29
- for _ , awsKey := range awsKeys {
30
- conf .ds .Write ("awskeys" , strings .Join (awsKey [1 :], ":" ), []byte (key ))
31
- }
32
-
33
- return true
34
- }
35
-
36
- // Look for email addresses and save them to a file.
37
- func processEmails (contents , key string ) bool {
38
- emails := reEmail .FindAllString (contents , - 1 )
39
-
40
- // No emails found.
41
- if emails == nil {
42
- return false
43
- }
44
-
45
- for _ , email := range emails {
46
- email = cleanEmail (email )
47
-
48
- if email == "" {
49
- continue
50
- }
51
-
52
- conf .ds .Write ("emails" , email , []byte (key ))
53
- }
54
-
55
- return true
56
- }
57
-
58
- // Look for credentials in the format of email:password and save them to a file.
59
- func processCredentials (contents , key string ) bool {
60
- creds := reCreds .FindAllString (contents , - 1 )
61
-
62
- // No creds found.
63
- if creds == nil {
64
- return false
65
- }
66
-
67
- for _ , cred := range creds {
68
- if cred == "" {
69
- continue
70
- }
71
-
72
- conf .ds .Write ("creds" , cred , []byte (key ))
73
- }
74
-
75
- return true
76
- }
77
-
78
- // Look for private keys.
79
- func processPrivKey (contents , key string ) bool {
80
- privKeys := rePrivKey .FindAllString (contents , - 1 )
81
-
82
- // No keys found.
83
- if privKeys == nil {
84
- return false
85
- }
86
-
87
- for _ , privKey := range privKeys {
88
- conf .ds .Write ("privkeys" , privKey , []byte (key ))
89
- }
90
-
91
- return true
92
- }
93
-
94
- func savePaste (key , value string ) {
8
+ func savePaste (key , content string ) {
95
9
if conf .Save == false {
96
10
return
97
11
}
98
12
99
- if len (value ) > conf .MaxSize {
13
+ if len (content ) > conf .MaxSize {
100
14
return
101
15
}
102
16
103
- conf .ds .Write ("pastes" , key , []byte (value ))
17
+ conf .ds .Write ("pastes" , key , []byte (content ))
104
18
}
105
19
106
- func processContent (key , content string ) {
107
- conf .ds = getStoreConn ()
108
- defer conf .ds .Close ()
109
-
110
- // Find and save specific data.
111
- switch {
112
- case processCredentials (content , key ):
113
- savePaste (key , content )
114
- case processEmails (content , key ):
115
- savePaste (key , content )
116
- case processPrivKey (content , key ):
117
- savePaste (key , content )
118
- case processAWSKeys (content , key ):
119
- savePaste (key , content )
120
- default :
121
- }
122
-
123
- // Save pastes that match any of our regular expressions. Use these to find
124
- // interesting data that will eventually be processed with a more specific
125
- // method.
20
+ func processRegexes (key , content string ) {
126
21
save := false
127
22
for i , _ := range conf .Regexes {
128
23
r := conf .Regexes [i ]
129
- rKey := fmt .Sprintf ("%s-%s" , r .Prefix , key )
130
- match := r .compiled .FindString (content )
131
24
132
- if match != "" {
133
- save = true
134
- conf .ds .Write ("regexes" , rKey , nil )
25
+ switch r .Match {
26
+ case "all" :
27
+ items := r .compiled .FindAllString (content , - 1 )
28
+
29
+ if items != nil {
30
+ save = true
31
+ }
32
+
33
+ for k := range items {
34
+ rKey := fmt .Sprintf ("%s-%s-%d" , r .Prefix , key , k )
35
+ conf .ds .Write ("regexes" , rKey , []byte (items [k ]))
36
+ }
37
+ case "one" :
38
+ match := r .compiled .FindString (content )
39
+ rKey := fmt .Sprintf ("%s-%s" , r .Prefix , key )
40
+
41
+ if match != "" {
42
+ save = true
43
+ conf .ds .Write ("regexes" , rKey , []byte (match ))
44
+ }
45
+ default :
135
46
}
136
47
}
137
48
138
49
if save {
139
50
savePaste (key , content )
140
51
}
52
+ }
141
53
142
- // Save pastes that match any of our keywords. Use these to find interesting
143
- // data that will eventually be processed with a more specific method.
144
- save = false
54
+ func processKeywords (key , content string ) {
55
+ save := false
145
56
for i , _ := range conf .Keywords {
146
57
kwd := conf .Keywords [i ]
147
58
kwdKey := fmt .Sprintf ("%s-%s" , kwd .Prefix , key )
148
59
149
60
if strings .Contains (strings .ToLower (content ), strings .ToLower (kwd .Keyword )) {
150
61
save = true
151
- conf .ds .Write ("keywords" , kwdKey , nil )
62
+ conf .ds .Write ("keywords" , kwdKey , [] byte ( key ) )
152
63
}
153
64
}
154
65
@@ -157,16 +68,11 @@ func processContent(key, content string) {
157
68
}
158
69
}
159
70
160
- // Remove common false positives in email addresses.
161
- func cleanEmail (email string ) string {
162
- email = strings .ToLower (email )
163
-
164
- switch {
165
- case strings .HasSuffix (email , "2x.png" ):
166
- return ""
167
- case strings .HasSuffix (email , ".so" ):
168
- return ""
169
- default :
170
- return email
171
- }
71
+
72
+ func processContent (key , content string ) {
73
+ conf .ds = getStoreConn ()
74
+ defer conf .ds .Close ()
75
+
76
+ processRegexes (key , content )
77
+ processKeywords (key , content )
172
78
}
0 commit comments