ratonvirus-clamby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2ab97421ef8f1bcf87fc7465487f76f65d703b9e31ffc7fa156846863887ff90
4
+ data.tar.gz: d917af42da7b4602fb6e674e863c8ee16ba454457cc7d500396f16c35b1b1eaa
5
+ SHA512:
6
+ metadata.gz: 308ed44513124fd828b795f4bee414ead467a4ff8ae3c086d15b2a02a39a6dcf3fd9ccd7ce180a90e1278f06fdb11eecc923e34bd5a0704f840da11531f16a00
7
+ data.tar.gz: 6d551e25b9247bc0c72e11a5dce518e6920c8ddc27c6b5d149d3646751b687e32dcb7b4bc2c4759cafc61edee31fe5b088c5c3e6e1b11b03ad2451d6cdd138ea
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2018 Mainio Tech Ltd.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,256 @@
1
+ # Ratonvirus - Clamby
2
+
3
+ Developed by [Mainio Tech](https://www.mainiotech.fi/).
4
+
5
+ [![Build Status](https://api.travis-ci.org/mainio/ratonvirus-clamby.svg?branch=master)](https://travis-ci.org/mainio/ratonvirus-clamby)
6
+ [![codecov](https://codecov.io/gh/mainio/ratonvirus-clamby/branch/master/graph/badge.svg)](https://codecov.io/gh/mainio/ratonvirus-clamby)
7
+
8
+ This gem provides a [Clamby](https://github.com/kobaltz/clamby) scanner for
9
+ [Ratonvirus](https://github.com/mainio/ratonvirus).
10
+
11
+ It allows Ratovirus to scan the files using [ClamAV](https://www.clamav.net/).
12
+
13
+ ## Prerequisites
14
+
15
+ You need to have ClamAV installed on the target machine for the antivirus checks
16
+ to actually work. With the default configuration, you will also need the ClamAV
17
+ daemon installed in order to make the antivirus checks more efficient.
18
+
19
+ For full ClamAV installation instructions, please refer to
20
+ [ClamAV documentation](https://www.clamav.net/documents/installing-clamav).
21
+
22
+ For configuring ClamAV, please refer to
23
+ [Clamby documentation](https://github.com/kobaltz/clamby).
24
+
25
+ ### ClamAV installation on Ubuntu/Debian
26
+
27
+ For proper ClamAV configuration in Ubuntu/Debian environments, follow these
28
+ steps:
29
+
30
+ #### 1. ClamAV and daemon installation
31
+
32
+ ```bash
33
+ $ sudo apt install clamav clamav-daemon
34
+ ```
35
+
36
+ #### 2. ClamAV configuration
37
+
38
+ ```
39
+ # Change the following from /etc/clamav/freshclam.conf
40
+ # Change `local` to your country code
41
+ DatabaseMirror db.local.clamav.net
42
+ ```
43
+
44
+ ```
45
+ # Change the following from /etc/clamav/clamd.conf
46
+ # Most Rails apps use symlinks in the production environment
47
+ FollowDirectorySymlinks true
48
+ FollowFileSymlinks true
49
+ ```
50
+
51
+ #### 3. AppArmor configuration for clamd
52
+
53
+ Make sure that the folder where your application is running is included in the
54
+ readable directories list:
55
+
56
+ ```bash
57
+ $ sudo less /etc/apparmor.d/usr.sbin.clamd
58
+ ```
59
+
60
+ If not, edit the local AppArmor configuration:
61
+
62
+ ```bash
63
+ $ sudo nano /etc/apparmor.d/local/usr.sbin.clamd
64
+ ```
65
+
66
+ Add the following line there with your application directory:
67
+
68
+ ```
69
+ # Allow scanning for the application subdirs
70
+ /path/to/your/app/** r,
71
+ ```
72
+
73
+ And finally reload apparmor configuration:
74
+
75
+ ```bash
76
+ $ sudo systemctl reload apparmor
77
+ ```
78
+
79
+ #### 4. Restart ClamAV daemons
80
+
81
+ ```bash
82
+ $ sudo systemctl restart clamav-freshclam
83
+ $ sudo systemctl restart clamav-daemon
84
+ ```
85
+
86
+ ### Ensure that ClamAV installation is working properly
87
+
88
+ Go to your application folder and create simple test files there to test the
89
+ virus scanning:
90
+
91
+ ```bash
92
+ $ cd /path/to/your/app
93
+ $ echo 'This is clean' > clean.pdf
94
+ $ wget -O dirty.pdf https://secure.eicar.org/eicar.com
95
+ ```
96
+
97
+ The file `dirty.pdf` fetched from the URL is an
98
+ [EICAR test file](https://en.wikipedia.org/wiki/EICAR_test_file) used to test
99
+ the response of the antivirus scan.
100
+
101
+ Run the antivirus tests for both of these files using `clamdscan`:
102
+
103
+ ```bash
104
+ $ clamdscan clean.pdf dirty.pdf
105
+ ```
106
+
107
+ You should see the following type of output from that command when ClamAV and
108
+ its daemon are correctly working:
109
+
110
+ ```
111
+ /path/to/your/app/clean.pdf: OK
112
+ /path/to/your/app/dirty.pdf: Eicar-Test-Signature FOUND
113
+
114
+ ----------- SCAN SUMMARY -----------
115
+ Infected files: 1
116
+ Time: 0.001 sec (0 m 0 s)
117
+ ```
118
+
119
+ NOTE:
120
+
121
+ It is important that you test this in the actual production environment inside
122
+ the application folder or the folder where the users are uploading the files in
123
+ order to ensure that ClamAV daemon is able to access that folder and read files
124
+ from it.
125
+
126
+ Also note that Decidim uses
127
+ [CarrierWave](https://github.com/carrierwaveuploader/carrierwave) to handle its
128
+ file uploads and processing on the server, so make sure you are also testing the
129
+ possible temporary paths of CarrierWave.
130
+
131
+ ## Installation
132
+
133
+ Add this line to your application's Gemfile:
134
+
135
+ ```ruby
136
+ gem 'ratonvirus'
137
+ gem 'ratonvirus-clamby'
138
+ ```
139
+
140
+ Then execute:
141
+
142
+ ```bash
143
+ $ bundle
144
+ ```
145
+
146
+ And finally configure the scanner for Ratonvirus:
147
+
148
+ ```ruby
149
+ Ratonvirus.configure do |config|
150
+ config.scanner = :clamby
151
+ end
152
+ ```
153
+
154
+ ## Possible scanning errors
155
+
156
+ There are multiple scanning errors that this script may produce for the file
157
+ attribute. Here are the explanations for each of the errors.
158
+
159
+ Please note that if you have done any changes to the default configurations,
160
+ not all of these errors may be
161
+
162
+ ### antivirus_virus_detected ("contains a virus")
163
+
164
+ This means that the given file contains a virus detected by ClamAV.
165
+
166
+ This virus can be shown in few different occasions:
167
+
168
+ - The `clamdscan` executable did its work successfully, detected a virus and
169
+ returned with an exit code 1.
170
+ - The `clamdscan` executable is not executable by the user under which the Rails
171
+ app is run. This caused the system call to return with an exit code 126.
172
+ - The `clamdscan` executable is not available in the machine. This caused the
173
+ system call to return with an exit code 127.
174
+
175
+ Shown when the `clamdscan` executable returns with the exit code other than 0 or
176
+ 2.
177
+
178
+ ### antivirus_client_error ("could not be processed for virus scan")
179
+
180
+ This means that the given file contains a virus detected by ClamAV.
181
+
182
+ In this case the `clamdscan` executable did not finish its work successfully and
183
+ an error was produced. This can be generally caused by the `clamav-daemon`
184
+ service because of few different reasons:
185
+
186
+ - The daemon cannot access the file to be checked. Please refer to the
187
+ configuration section for further information.
188
+ - The daemon service is not running on the target machine. Please refer to the
189
+ configuration section for further information.
190
+ - The daemon service is currently handling too many concurrent virus checks.
191
+ This should be fixed by itself once the daemon finishes the previous checks.
192
+
193
+ Shown when the `clamdscan` executable returns with the exit code 2.
194
+
195
+ ### antivirus_file_not_found ("could not be found for virus scan")
196
+
197
+ This means that the file passed to the ClamAV virus scan is no longer available
198
+ when the scan was about to be performed.
199
+
200
+ In this case, the `clamdscan` executable is not run.
201
+
202
+ Shown when the file has disappeared from the file system between the upload
203
+ procedure and Ratonvirus scans. This could also happen in case there is a
204
+ problem in with the storage engine when moving the file to the local filesystem.
205
+
206
+ ## Testing without installing ClamAV
207
+
208
+ If you want to test that the scanner is working correctly without installing
209
+ ClamAV, you can create a dummy ClamAV executable in your app's `bin` path as
210
+ follows:
211
+
212
+ ```bash
213
+ $ cd /path/to/your/app
214
+ $ wget -O bin/clamdscan https://git.io/fpKZr && chmod 755 bin/clamdscan
215
+ ```
216
+
217
+ This downloads a bash script created to test the ClamAV executables without
218
+ installing ClamAV. You can inspect the script from
219
+ [here](https://gist.github.com/ahukkanen/ad28be993333b751013ddbc4cde2acef) prior
220
+ to downloading and running it.
221
+
222
+ The executable is being executed by Clamby to check for the viruses.
223
+
224
+ After creating these files, you should be able to test the Clamby scanner from
225
+ your Rails application by adding the folder where this executable resides to the
226
+ PATH environment variable for your Rails application. You can do this when you
227
+ start your Rails development server as follows.
228
+
229
+ ```
230
+ $ PATH=./bin:$PATH bundle exec rails s
231
+ ```
232
+
233
+ You should now be able to upload the
234
+ [EICAR test file](https://en.wikipedia.org/wiki/EICAR_test_file) to the proposal
235
+ form and see a `contains a virus` error when submitting the form. When
236
+ submitting any other file, the scanning should pass and you should not see any
237
+ errors produced by Ratonvirus.
238
+
239
+ Feel free to try the scanner with different exit codes as well, they are
240
+ described below:
241
+
242
+ - 0: No virus found.
243
+ - 1: Virus(es) found.
244
+ - 2: An error occured.
245
+ - 126: The file is not executable.
246
+ - 127: The executable could not be found.
247
+
248
+ For testing these, modify the `bin/clamdscan` executable to contain the
249
+ following lines:
250
+
251
+ ```bash
252
+ #!/bin/bash
253
+ exit 1
254
+ ```
255
+
256
+ Modify the exit code to the one you want to test.
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ # Run all tests, with coverage report
6
+ RSpec::Core::RakeTask.new(:coverage) do |t, task_args|
7
+ ENV['CODECOV'] = '1'
8
+ t.verbose = false
9
+ end
10
+
11
+ # Run all tests, include all
12
+ RSpec::Core::RakeTask.new(:spec) do |t, task_args|
13
+ t.verbose = false
14
+ end
15
+
16
+ # Run both by default
17
+ task default: [:spec, :coverage]
@@ -0,0 +1,9 @@
1
+ require 'clamby'
2
+
3
+ require_relative 'clamby/version'
4
+ require_relative 'scanner/clamby'
5
+
6
+ module Ratonvirus
7
+ module Clamby
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ratonvirus
4
+ module Clamby
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ratonvirus
4
+ module Scanner
5
+ class Clamby < Base
6
+ CLAMBY_DEFAULT_CONFIG = ::Clamby::DEFAULT_CONFIG.merge(
7
+ # We want to handle checking locally on initialization, not on every
8
+ # scan.
9
+ check: false,
10
+ # Should be encouraged as it is much faster.
11
+ daemonize: true,
12
+ # We are checking already executable? and therefore not needed.
13
+ error_clamscan_missing: false,
14
+ # We want Ratonvirus to report issues errors on client errors.
15
+ error_clamscan_client_error: true,
16
+ # We want Ratonvirus to report issues on file missing errors.
17
+ error_file_missing: true,
18
+ # No output is required not to fill the logs. The scanning errors
19
+ output_level: 'off',
20
+ ).freeze
21
+
22
+ class << self
23
+ def configure(config={})
24
+ ::Clamby.configure(config)
25
+ end
26
+
27
+ def reset
28
+ configure(CLAMBY_DEFAULT_CONFIG.dup)
29
+ end
30
+
31
+ # Avoid multiple calls to the clamscan utility to check whether the
32
+ # system is executable.
33
+ def executable?
34
+ # Clamby should return `nil` when clamscan is not available.
35
+ !!::Clamby::Command.clamscan_version
36
+ end
37
+ end
38
+
39
+ # Allow users to configure Clamby the way they want to.
40
+ def setup
41
+ self.class.configure(config[:clamby] || {})
42
+
43
+ super
44
+ end
45
+
46
+ protected
47
+ def run_scan(path)
48
+ # In case the file is not present at all, scanning should always pass
49
+ # because nil is not a virus.
50
+ return if path.nil?
51
+
52
+ begin
53
+ errors << :antivirus_virus_detected if ::Clamby.virus?(path)
54
+ rescue ::Clamby::ClamscanClientError
55
+ # This can happen e.g. if the clamdscan utility does not have access
56
+ # to read the file path. For debugging, try to run the clamdscan
57
+ # utility manually for the same file:
58
+ # clamdscan /path/to/file.pdf
59
+ #
60
+ # Also, make sure that the file uploads store directory is readable
61
+ # by the clamdscan utility. E.g. /path/to/app/public/uploads/tmp.
62
+ #
63
+ # Another possible reason is that in case there are too many
64
+ # concurrent virus checks ongoing, it may also trigger this error.
65
+ errors << :antivirus_client_error
66
+ rescue ::Clamby::FileNotFound
67
+ # This should be pretty rare since the scanner should not be even
68
+ # called when the file is not available. As the storage backend may
69
+ # be configured, this may still happen with some storage backends.
70
+ errors << :antivirus_file_not_found
71
+ end
72
+ end
73
+
74
+ # Make sure we are starting up with the default configuration.
75
+ reset
76
+ end
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ratonvirus-clamby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Antti Hukkanen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ratonvirus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: clamby
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.16.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.16.0
83
+ description: Provides ClamAV scanner backed by Clamby for the Ratonvirus gem.
84
+ email:
85
+ - antti.hukkanen@mainiotech.fi
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - LICENSE
91
+ - README.md
92
+ - Rakefile
93
+ - lib/ratonvirus/clamby.rb
94
+ - lib/ratonvirus/clamby/version.rb
95
+ - lib/ratonvirus/scanner/clamby.rb
96
+ homepage: https://github.com/mainio/ratonvirus-clamby
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.7.7
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Clamby scanner for Ratonvirus.
120
+ test_files: []