-
Notifications
You must be signed in to change notification settings - Fork 1
/
spam.conf
178 lines (154 loc) · 4.75 KB
/
spam.conf
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
input {
file {
path => "/media/johnny/C66E87A36E878B3F/Spam data/*"
start_position => beginning
sincedb_path => "/home/johnny/Documents/spam-analysis/record.db"
codec => multiline { #用「Delivered-To:」做每筆資料的切割依據
pattern => "^Delivered-To:"
negate => true
what => "previous"
}
}
}
filter {
#Delivered-To
grok {
match => { "message" => "^Delivered-To:[ \t]*%{DATA}(?<Delivered-to>[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4})"}
}
#fetch ip
if "X-Originating-IP:" in [message] {
grok {
match => { "message" => "^X-Originating-IP:[ \t]*%{IP:X-Originating-IP}"}
}
} else {
grok { #抓取public IP(0.0.0.0~9.255.255.255, 11.0.0.0~172.15.255.255, 172.32.0.0~192.167.255.255, 192.169.0.0~223.255.255.255)
match => [ "message", "(?<X-Originating-IP>(?<![0-9])(?:(?:1[0-2][0-6]|[1-9][1-9]|[0-9])[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})|(?:12[8-9]|1[3-6][0-9]|17[13-9]|18[0-9]|19[01])[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})|172[.](?:[0-9]|1[0-5]|3[2-9]|[4-9][0-9]|25[0-5]|2[0-4][0-9]|1[0-9][0-9])[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})|(?:19[3-9]|2[0-2][0-3])[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})|192[.](?:25[0-5]|2[0-4][0-9]|1[0-57-9][0-9]|16[0-79]|[1-9][0-9]|[0-9])[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})[.](?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2}))(?![0-9]))"]
}
}
#Date
grok { #時間格式:d MMM yyyy hh:mm:ss Z
match => [ "message", "(?<Date>%{MONTHDAY}[ \t]*%{MONTH}[ \t]*%{YEAR}[ \t]*%{TIME}[ \t]*[+-]\d{4})"]
}
#Subject(Maybe multiline)
if "Subject:" in [message] {
grok {
match => [ "message", "^Subject:[ \t]*(?<Subject>((\S+[\S ]*)(\n[\t ]+(\S+[\S ]*))*))"]
add_tag => [ "has subject" ]
}
}
if "has subject" in [tags]{
#Do nothing
}else{
grok {
match => [ "message", "%{DATA}"]
add_field => { "Subject" => "No Subject" }
}
}
#Link count
mutate {
add_field => {"link_count" => "%{message}"}
}
mutate {
split => ["link_count", "http"]
add_tag => "split_http"
}
if "split_http" in [tags] {
mutate {
gsub => ["link_count", "(.|\n)+", "*"]
}
}
#From
grok {
match => [ "message", "^From:[ \t]*%{DATA}(?<From>[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4})"]
}
#To
grok {
match => [ "message", "^To:[ \t]*(?<To>((\S+[\S ]*)(\n[\t ]+(\S+[\S ]*))*))"]
add_tag => [ "valid" ]
}
#Reply-To
grok {
match => [ "message", "^Reply-To:[ \t]*(?<Reply-To>((\S+[\S ]*)(\n[\t ]+(\S+[\S ]*))*))"]
}
if "valid" in [tags] {
#Do nothing
}else{
#如果To欄位不存在,則將Reply-To的內容也定義為To欄位的內容
grok {
match => [ "message", "^Reply-To:[ \t]*(?<To>((\S+[\S ]*)(\n[\t ]+(\S+[\S ]*))*))"]
add_tag => [ "valid" ]
}
}
mutate {
split => ["Reply-To", ","]
gsub => ["Reply-To", "(([\w ]*<)|\s|>)", ""]
}
#Cc
grok {
match => [ "message", "^Cc:[ \t]*(?<CC>((\S+[\S ]*)(\n[\t ]+(\S+[\S ]*))*))"]
}
if "valid" in [tags] {
#Do nothing
}else{
#如果To欄位不存在,則將Cc的內容也定義為To欄位的內容
grok {
match => [ "message", "^Cc:[ \t]*(?<To>((\S+[\S ]*)(\n[\t ]+(\S+[\S ]*))*))"]
add_tag => [ "valid" ]
}
}
mutate {
split => ["CC", ","]
gsub => ["CC", "(([\w ]*<)|\s|>)", ""]
}
#Bcc
grok {
match => [ "message", "^Bcc:[ \t]*(?<BCC>((\S+[\S ]*)(\n[\t ]+(\S+[\S ]*))*))"]
}
if "valid" in [tags] {
#Do nothing
}else{
#如果To欄位不存在,則將Bcc的內容也定義為To欄位的內容
grok {
match => [ "message", "^Bcc:[ \t]*(?<To>((\S+[\S ]*)(\n[\t ]+(\S+[\S ]*))*))"]
add_tag => [ "valid" ]
}
}
mutate { #把密件副本切割成陣列
split => ["BCC", ","]
gsub => ["BCC", "(([\w ]*<)|\s|>)", ""]
}
mutate { #把收件者切割成陣列
split => ["To", ","]
gsub => ["To", "(([\w ]*<)|\s|>)", ""]
}
#time format
date { #將match的時間欄位format成ISO 8601格式,並存入@sendTime欄位中
match => ["Date", "d MMM yyyy HH:mm:ss Z"]
locale => "en"
target => "@sendTime"
}
#convert ip to location info
geoip {
source=> "X-Originating-IP"
database => "/home/johnny/Documents/logstash-1.4.2/vendor/geoip/GeoLiteCity.dat"
#remove_field => ["[geoip][location]"]
}
#我們將Header中有收件者訊息的Email認定為合法的Email Header,否則將其drop掉
if "valid" in [tags] {
#remove useless fields
mutate {
remove_field => [ "message", "tags", "host", "path", "@version", "_version" ]
}
} else {
drop {}
}
}
output {
#stdout {
# codec => rubydebug
#}
elasticsearch {
host => localhost
codec => json
}
}