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
|
# File 'lib/phisher_phinder/mail_parser/received_headers/from_parser.rb', line 12
def parse(component)
unless component
return {
advertised_authenticated_sender: nil,
advertised_sender: nil,
helo: nil,
sender: {
host: nil,
ip: nil
},
}.merge(@starttls_parser.parse(nil))
end
patterns = [
%r{
from\s\[(?<advertised_sender>[\S]+)\]\s
\((?<sender_host>\S+?)\.?\s
\[(?<sender_ip>[^\]]+)\]\)\s
\(Authenticated\ssender:\s(?<advertised_authenticated_sender>[^\)]+)\)
}x,
/from\s\[(?<sender_ip>[^\]]+)\]\s\(helo=(?<helo>[^\)]+)\)/,
%r{
from\s\[(?<advertised_sender>[\S]+)\]\s
\((?<sender_host>\S+?)\.?\s
\[(?<sender_ip>[^\]]+)\]\)
}x,
/from\s(?<sender_ip>[^\]]+)\s\(EHLO\s(?<helo>[^\)]+)\)/,
/from\s(?<advertised_sender>[\S]+)\s\((?<sender_host>\S+?)\.?\s\[(?<sender_ip>[^\]]+)\]\) \((?<starttls>[^\)]+\))/,
/from\s(?<advertised_sender>[\S]+)\s\((?<sender_host>\S+?)\.?\s\[(?<sender_ip>[^\]]+)\]\) \((?<starttls>[^\)]+\))/,
/from\s(?<advertised_sender>[\S]+)\s\(HELO\s(?<helo>[^)]+)\)\s\(\)/,
/from\s(?<advertised_sender>[\S]+)\s\(HELO\s(?<helo>[^)]+)\)\s\(\[(?<sender_ip>[^\]]+)\]\)/,
/from\s(?<advertised_sender>[\S]+)\s\((?<sender_host>\S+?)\.?\s\[(?<sender_ip>[^\]]+)\]\)/,
/from\s(?<advertised_sender>\S+)\s\((?<sender_host>\S+?)\.?\s(?<sender_ip>\S+?)\)/,
/from\s(?<advertised_sender>\S+)\s\(\[(?<sender_ip>[^\]]+)\]\)/,
/from\s(?<advertised_sender>\S+)\s\((?<sender_ip>[^)]+)\)/,
/\(from\s(?<advertised_sender>[^)]+)\)/,
/from\s(?<advertised_sender>\S+)/,
]
matches = patterns.inject(nil) do |memo, pattern|
memo || component.match(pattern)
end
output = {
advertised_sender: expand_advertised_sender((matches, :advertised_sender)),
helo: matches.names.include?('helo') ? matches[:helo] : nil,
sender: {
host: matches.names.include?('sender_host') ? matches[:sender_host] : nil,
ip: matches.names.include?('sender_ip') ? @extended_ip_factory.build(matches[:sender_ip]) : nil
},
advertised_authenticated_sender: matches.names.include?('advertised_authenticated_sender') ? matches[:advertised_authenticated_sender] : nil
}
if matches.names.include?('starttls')
output.merge(@starttls_parser.parse(matches[:starttls]))
else
output.merge(@starttls_parser.parse(nil))
end
end
|