Skip to content

Commit

Permalink
fix #41: Provide ClamAV integration
Browse files Browse the repository at this point in the history
Adding a new feature that allows the application to be configured to send all uploaded content to an external process, a ClamAV daemon, that scans it for malware.
  • Loading branch information
guusdk committed Sep 10, 2023
1 parent 9023681 commit 3f85980
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 8 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ A full set of usage instructions are provided by adding the ``--help`` argument:
--announcedWebProtocol <arg> The Protocol that is to be used by
the end users. Defaults to the
webProtocol value
--clamavHost <arg> The FQDN or IP address of the host
running the optional ClamAV malware
scanner, if any.
--clamavPort <arg> The TCP port number for the optional
ClamAV malware scanner, if any.
--domain <arg> The domain that will be used for the
component with the XMPP domain.
--fileRepo <arg> Store files in a directory provided
by the file system. Provide the
desired path as a value. Path must
exist.
-h,--help Displays this help text.
-h,--help Displays this help text.
--maxFileSize <arg> The maximum allowed size per file,
in bytes. Use -1 to disable file
size limit. Defaults to 5242880
Expand Down Expand Up @@ -108,3 +113,21 @@ A full set of usage instructions are provided by adding the ``--help`` argument:
--xmppPort <arg> The TCP port number on the xmppHost,
to which a connection will be made.
Defaults to 5275.

Scanning for Malware
--------------------
To facilitate virus scanning, you can configure the application to use ClamAV. ClamAV is a third-party, open source
(GPLv2) anti-virus toolkit, available at https://www.clamav.net/

To configure this application to use ClamAV, install, configure and run clamav-daemon, the scanner daemon of ClamAV.
Configure the daemon in such a way that Openfire can access it via TCP.

Note: ClamAV is configured with a maximum file size. Ensure that this is at least as big as the `maxFileSize` that is
provided as an argument to the HTTP File Upload Component.

Then, start the HTTP File Upload Component application with the `clamavHost` and `clamavPort` arguments. When these are
provided, the application will supply each file that is being uploaded to the ClamAV daemon for scanning. A file upload
will fail when the ClamAV daemon could not be reached, or, obviously, when it detects malware.

While malware scanning can offer some protection against distributing unwanted content, it has limitations. Particularly
when the uploaded data is encrypted, the scanner is unlikely able to detect any malware in it.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>nl.goodbytes.xmpp.xep</groupId>
<artifactId>httpfileuploadcomponent</artifactId>
<version>1.5.1-SNAPSHOT</version>
<version>1.6.0-SNAPSHOT</version>

<name>HTTP File Upload Component</name>
<description>Implementation of an XMPP External Component that implements XEP-0363 'HTTP File Upload'.</description>
Expand Down Expand Up @@ -166,6 +166,12 @@
<version>3.8.5</version>
</dependency>

<dependency>
<groupId>xyz.capybara</groupId>
<artifactId>clamav-client</artifactId>
<version>2.1.2</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
39 changes: 36 additions & 3 deletions src/main/java/nl/goodbytes/xmpp/xep0363/Launcher.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017-2022 Guus der Kinderen. All rights reserved.
* Copyright (c) 2017-2023 Guus der Kinderen. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@

package nl.goodbytes.xmpp.xep0363;

import nl.goodbytes.xmpp.xep0363.clamav.ClamavMalwareScanner;
import nl.goodbytes.xmpp.xep0363.repository.DirectoryRepository;
import nl.goodbytes.xmpp.xep0363.repository.TempDirectoryRepository;
import nl.goodbytes.xmpp.xep0363.slot.DefaultSlotProvider;
Expand All @@ -32,6 +33,7 @@
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.*;

/**
Expand All @@ -58,8 +60,9 @@ public class Launcher
private final SlotProvider slotProvider;
private final Long maxFileSize;
private final boolean wildcardCORS;
private final MalwareScanner malwareScanner;

public Launcher( String xmppHost, Integer xmppPort, String domain, String sharedSecret, String webProtocol, String webHost, Integer webPort, String webContextRoot, String announcedWebProtocol, String announcedWebHost, Integer announcedWebPort, String announcedWebContextRoot, Repository repository, Long maxFileSize, boolean wildcardCORS)
public Launcher( String xmppHost, Integer xmppPort, String domain, String sharedSecret, String webProtocol, String webHost, Integer webPort, String webContextRoot, String announcedWebProtocol, String announcedWebHost, Integer announcedWebPort, String announcedWebContextRoot, Repository repository, Long maxFileSize, boolean wildcardCORS, MalwareScanner malwareScanner)
{
this.xmppHost = xmppHost != null ? xmppHost : "localhost";
this.xmppPort = xmppPort != null ? xmppPort : 5275;
Expand All @@ -77,6 +80,7 @@ public Launcher( String xmppHost, Integer xmppPort, String domain, String shared
this.slotProvider = new DefaultSlotProvider();
this.maxFileSize = maxFileSize != null ? maxFileSize : SlotManager.DEFAULT_MAX_FILE_SIZE;
this.wildcardCORS = wildcardCORS;
this.malwareScanner = malwareScanner;
}

public static void main( String[] args )
Expand Down Expand Up @@ -224,6 +228,23 @@ public static void main( String[] args )
.build()
);

options.addOption(
Option.builder()
.longOpt( "clamavHost" )
.hasArg()
.desc( "The FQDN or IP address of the host running the optional ClamAV malware scanner, if any." )
.build()
);

options.addOption(
Option.builder()
.longOpt( "clamavPort" )
.hasArg()
.desc( "The TCP port number for the optional ClamAV malware scanner, if any." )
.type( Integer.class )
.build()
);

try
{
final CommandLineParser parser = new DefaultParser();
Expand All @@ -250,6 +271,8 @@ public static void main( String[] args )
final String sharedSecret = line.getOptionValue( "sharedSecret" );
final Long maxFileSize = line.hasOption( "maxFileSize" ) ? Long.parseLong(line.getOptionValue( "maxFileSize" )) : null;
final boolean wildcardCORS = line.hasOption("wildcardCORS");
final String clamavHost = line.getOptionValue("clamavHost", null);
final Integer clamavPort = line.hasOption( "clamavPort" ) ? Integer.parseInt(line.getOptionValue( "clamavPort" )) : null;

final Repository repository;
if ( line.hasOption( "tempFileRepo" ) )
Expand All @@ -272,7 +295,14 @@ else if (line.hasOption( "fileRepo"))
repository = null;
}

final Launcher launcher = new Launcher( xmppHost, xmppPort, domain, sharedSecret, webProtocol, webHost, webPort, webContextRoot, announcedWebProtocol, announcedWebHost, announcedWebPort, announcedWebContextRoot, repository, maxFileSize, wildcardCORS );
final MalwareScanner clamav;
if ( clamavHost != null ) {
clamav = new ClamavMalwareScanner(clamavHost, clamavPort == null ? 3310 : clamavPort, Duration.ofSeconds(2));
} else {
clamav = null;
}

final Launcher launcher = new Launcher( xmppHost, xmppPort, domain, sharedSecret, webProtocol, webHost, webPort, webContextRoot, announcedWebProtocol, announcedWebHost, announcedWebPort, announcedWebContextRoot, repository, maxFileSize, wildcardCORS, clamav );
launcher.start();
}
}
Expand Down Expand Up @@ -372,6 +402,9 @@ public void start()
Log.info( "Starting repository..." );
RepositoryManager.getInstance().initialize( repository );

Log.info( "Starting malware scanner...");
MalwareScannerManager.getInstance().initialize( malwareScanner );

Log.info( "Starting webserver..." );

jetty = new Server();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2023 Guus der Kinderen. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.goodbytes.xmpp.xep0363;

public class MalwareDetectedException extends Exception
{
}
27 changes: 27 additions & 0 deletions src/main/java/nl/goodbytes/xmpp/xep0363/MalwareScanner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2023 Guus der Kinderen. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.goodbytes.xmpp.xep0363;

import java.io.IOException;

public interface MalwareScanner
{
void initialize() throws IOException;

void destroy();

void scan(final SecureUniqueId uuid) throws MalwareDetectedException, IOException;
}
60 changes: 60 additions & 0 deletions src/main/java/nl/goodbytes/xmpp/xep0363/MalwareScannerManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2023 Guus der Kinderen. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.goodbytes.xmpp.xep0363;

import java.io.IOException;

public class MalwareScannerManager
{
private static MalwareScannerManager INSTANCE;

public synchronized static MalwareScannerManager getInstance()
{
if (INSTANCE == null) {
INSTANCE = new MalwareScannerManager();
}

return INSTANCE;
}

private MalwareScanner malwareScanner;

public boolean isEnabled() {
return this.malwareScanner != null;
}

public void initialize(final MalwareScanner malwareScanner) throws IOException
{
if (this.malwareScanner != null) {
throw new IllegalArgumentException("Already initialized.");
}
this.malwareScanner = malwareScanner;
this.malwareScanner.initialize();
}

public MalwareScanner getMalwareScanner()
{
return this.malwareScanner;
}

public void destroy()
{
if (this.malwareScanner != null) {
this.malwareScanner.destroy();
this.malwareScanner = null;
}
}
}
4 changes: 3 additions & 1 deletion src/main/java/nl/goodbytes/xmpp/xep0363/Repository.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Guus der Kinderen. All rights reserved.
* Copyright (c) 2017-2023 Guus der Kinderen. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,4 +45,6 @@ public interface Repository

// For writing data.
OutputStream getOutputStream( SecureUniqueId uuid ) throws IOException;

boolean delete(SecureUniqueId uuid) throws IOException;
}
23 changes: 22 additions & 1 deletion src/main/java/nl/goodbytes/xmpp/xep0363/Servlet.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017-2022 Guus der Kinderen. All rights reserved.
* Copyright (c) 2017-2023 Guus der Kinderen. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -190,6 +190,7 @@ protected void doPut( HttpServletRequest req, HttpServletResponse resp ) throws
try ( final InputStream in = req.getInputStream();
final OutputStream out = new BufferedOutputStream( repository.getOutputStream( slot.getUuid() ) ) )
{
Log.debug("... receiving content ...");
final byte[] buffer = new byte[ 1024 * 4 ];
int bytesRead;
while ( ( bytesRead = in.read( buffer ) ) != -1 )
Expand All @@ -198,6 +199,26 @@ protected void doPut( HttpServletRequest req, HttpServletResponse resp ) throws
}
}

final MalwareScannerManager malwareScannerManager = MalwareScannerManager.getInstance();
if (malwareScannerManager.isEnabled()) {
try {
Log.debug("... scanning uploaded content for malware ...");
final MalwareScanner malwareScanner = malwareScannerManager.getMalwareScanner();
malwareScanner.scan(slot.getUuid());
Log.info("... malware scanning did not find malware ...");
} catch (MalwareDetectedException e) {
resp.sendError( HttpServletResponse.SC_BAD_REQUEST, "Malware detected in the upload!" );
repository.delete(slot.getUuid());
Log.warn("... responded with BAD_REQUEST. Malware detected in upload of {}.", req.getRemoteAddr(), e);
return;
} catch (Throwable t) {
resp.sendError( HttpServletResponse.SC_BAD_REQUEST, "Malware scanning failed" );
repository.delete(slot.getUuid());
Log.info("... responded with BAD_REQUEST. Malware scanner execution failed.", t);
return;
}
}

try
{
resp.setHeader( "Location", SlotManager.getGetUrl(slot).toExternalForm() );
Expand Down
Loading

0 comments on commit 3f85980

Please sign in to comment.