-
Notifications
You must be signed in to change notification settings - Fork 13
/
clamav.go
167 lines (147 loc) · 4.23 KB
/
clamav.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
// ClueGetter - Does things with mail
//
// Copyright 2016 Dolf Schimmel, Freeaqingme.
//
// This Source Code Form is subject to the terms of the two-clause BSD license.
// For its contents, please refer to the LICENSE file.
//
// Test using: http://sanesecurity.com/support/signature-testing/
//
package core
import (
"bytes"
"time"
clamd "github.com/Freeaqingme/go-clamd"
)
var (
clamdClient *clamd.Clamd
)
func init() {
enable := func() bool { return Config.Clamav.Enabled }
init := clamavInit
milterCheck := clamavMilterCheck
ModuleRegister(&ModuleOld{
name: "clamav",
enable: &enable,
init: &init,
milterCheck: &milterCheck,
})
}
func clamavInit() {
clamdClient = clamd.NewClamd(Config.Clamav.Address)
err := clamdClient.Ping()
if err != nil {
Log.Fatal("Could not connect to Clamav: %s", err.Error())
}
go func() {
ticker := time.NewTicker(time.Minute)
for {
select {
case <-ticker.C:
go func() {
CluegetterRecover("clamavDumpStats")
clamavDumpStats()
}()
}
}
}()
}
func clamavDumpStats() {
stats, err := clamdClient.Stats()
if err != nil {
Log.Error("Error while dumping stats from ClamAV: %s", err.Error())
}
Log.Info("ClamAV stats: %v", stats)
}
func clamavMilterCheck(msg *Message, abort chan bool) *MessageCheckResult {
if !msg.session.config.Clamav.Enabled {
return nil
}
sconf := msg.session.config.Clamav
msgStr := msg.String()
if len(msgStr) > sconf.Max_Size {
msgStr = msgStr[:sconf.Max_Size]
}
res, err := clamdClient.ScanStream(bytes.NewReader(msgStr), abort)
if err != nil {
Log.Error("Problem while talking to Clamd while checking for %s: %s", msg.QueueId, err.Error())
return &MessageCheckResult{
Module: "clamav",
SuggestedAction: MessageError,
Message: "An internal error occurred.",
Score: 25,
Determinants: map[string]interface{}{"error": err.Error()},
}
}
// There really should only be 1 item in res, but just in case there's
// more, we do want to close everything.
defer func() {
for v := range res {
Log.Notice("Got an additional ClamAV result, but it was discarded while scanning %s: %s",
msg.QueueId, v.Raw)
}
}()
v := <-res
if v == nil {
Log.Info("clamavMilterCheck(): No result received over result cannel. Channel closed?")
return nil
}
switch v.Status {
case clamd.RES_OK:
return &MessageCheckResult{
Module: "clamav",
SuggestedAction: MessageReject,
Message: "",
Score: 0,
Determinants: clamavGetDeterminants(v),
}
case clamd.RES_FOUND:
return &MessageCheckResult{
Module: "clamav",
SuggestedAction: MessageReject,
Message: "Our system has detected that this message appears to contain malicious or " +
"otherwise harmful content. Therefore, this message has been blocked.",
Score: msg.session.config.Clamav.Default_Score,
Determinants: clamavGetDeterminants(v),
}
case clamd.RES_PARSE_ERROR:
Log.Error("Could not parse output from Clamd while checking for %s. Raw output: %s",
msg.QueueId, v.Raw)
return &MessageCheckResult{
Module: "clamav",
SuggestedAction: MessageError,
Message: "An internal error occurred.",
Score: 25,
Determinants: clamavGetDeterminants(v),
}
case clamd.RES_ERROR:
Log.Error("Clamd returned an error while checking for %s: %s", msg.QueueId, v.Description)
return &MessageCheckResult{
Module: "clamav",
SuggestedAction: MessageError,
Message: "An internal error occurred.",
Score: 25,
Determinants: clamavGetDeterminants(v),
}
default:
Log.Error("Clamd returned an unrecognized status code. This shouldn't happen! "+
"While checking for %s: %s", msg.QueueId, v.Status)
return &MessageCheckResult{
Module: "clamav",
SuggestedAction: MessageError,
Message: "An internal error occurred.",
Score: 25,
Determinants: clamavGetDeterminants(v),
}
}
}
func clamavGetDeterminants(res *clamd.ScanResult) map[string]interface{} {
return map[string]interface{}{
"raw": res.Raw,
"description": res.Description,
"path": res.Path,
"hash": res.Hash,
"size": res.Size,
"status": res.Status,
}
}