diff --git a/pom.xml b/pom.xml index 7ea1adf..39b6663 100644 --- a/pom.xml +++ b/pom.xml @@ -13,19 +13,19 @@ - claudineynascimento + claudineyns Claudiney Nascimento contato@claudiney.info - https://github.com/claudineynascimento/ + https://github.com/claudineyns/ - https://github.com/claudineynascimento/icap-server + https://github.com/claudineyns/icap-server MIT License - https://raw.githubusercontent.com/claudineynascimento/icap-server/master/LICENSE + https://raw.githubusercontent.com/claudineyns/icap-server/master/LICENSE diff --git a/src/main/java/net/rfc3507/av/clamav/ClamAVCore.java b/src/main/java/net/rfc3507/av/clamav/ClamAVCore.java index b719ff6..b8566d0 100644 --- a/src/main/java/net/rfc3507/av/clamav/ClamAVCore.java +++ b/src/main/java/net/rfc3507/av/clamav/ClamAVCore.java @@ -71,7 +71,7 @@ private File saveContent(byte[] content) private ClamAVResponse scanContent(File file) throws ClamAVException { String path = file.getParent(); - Logger.getGlobal().info("Scanning file: " + path+file.getName() + "..."); + Logger.getGlobal().info("Scanning file: " + path+"/"+file.getName() + "..."); Logger.getGlobal().info("Scanning path: " + path + "..."); List daemonExec = new LinkedList<>(); diff --git a/src/main/java/net/rfc3507/av/windowsdefender/WindowsDefenderAntivirus.java b/src/main/java/net/rfc3507/av/windowsdefender/WindowsDefenderAntivirus.java index e1ce54c..24ee514 100644 --- a/src/main/java/net/rfc3507/av/windowsdefender/WindowsDefenderAntivirus.java +++ b/src/main/java/net/rfc3507/av/windowsdefender/WindowsDefenderAntivirus.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.UnsupportedEncodingException; import java.util.LinkedList; import java.util.List; import java.util.UUID; @@ -16,8 +17,6 @@ public class WindowsDefenderAntivirus { - private static String checkResult = ""; - public WindowsDefenderResponse checkThreat(byte[] content) throws WindowsDefenderException { @@ -74,7 +73,10 @@ private WindowsDefenderResponse scanContent(File file) throws WindowsDefenderExc throw new WindowsDefenderException(e.getMessage()); } - checkResult = new String(response.toByteArray()); + String checkResult = null; + try { + checkResult = new String(response.toByteArray(), "ascii"); + } catch(UnsupportedEncodingException e) {} Pattern pattern = Pattern.compile("^Threat\\s{18}:\\s(\\S*)", Pattern.MULTILINE); Matcher matcher = pattern.matcher(checkResult); @@ -93,7 +95,6 @@ private File saveContent(byte[] content) throws WindowsDefenderException { File file = new File( System.getProperty("java.io.tmpdir"), -// "C:\\temp\\malware\\", UUID.randomUUID().toString()+".threat"); OutputStream out = null; diff --git a/src/main/java/net/rfc3507/server/ClientHandler.java b/src/main/java/net/rfc3507/server/ClientHandler.java index 86d987f..c302255 100644 --- a/src/main/java/net/rfc3507/server/ClientHandler.java +++ b/src/main/java/net/rfc3507/server/ClientHandler.java @@ -10,6 +10,7 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; +import java.util.UUID; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -121,8 +122,6 @@ private void startHandleIcapRequest() throws Exception { && memory[memory.length-2] == '\r' && memory[memory.length-1] == '\n' ) { - info("### (SERVER: RECEIVE) ### ICAP REQUEST\n"+new String(memory)); - analyseRequestHeader(memory); break; @@ -186,25 +185,21 @@ private void extractEncapsulatedPayloads() throws Exception { if( httpRequestHeaderSize > 0 ) { parseContent = new byte[httpRequestHeaderSize]; readStream(parseContent); - info("### (SERVER: RECEIVE) ### HTTP REQUEST HEADER\n"+new String(parseContent)); httpRequestHeaders.write(parseContent); } if( httpResponseHeaderSize > 0 ) { parseContent = new byte[httpResponseHeaderSize]; readStream(parseContent); - info("### (SERVER: RECEIVE) ### HTTP RESPONSE HEADER\n"+new String(parseContent)); httpResponseHeaders.write(parseContent); } if( "req-body".equals(lastOffsetLabel) ) { readBody(httpRequestBody); - info("### (SERVER: RECEIVE) ### HTTP REQUEST BODY\n"+new String(httpRequestBody.toByteArray())); } if( "res-body".equals(lastOffsetLabel) ) { readBody(httpResponseBody); - info("### (SERVER: RECEIVE) ### HTTP RESPONSE BODY\n"+new String(httpResponseBody.toByteArray())); } } @@ -431,13 +426,11 @@ private void sendCloseConnection() throws IOException { } private void sendContinue() throws IOException { - info("### (SERVER: SEND) ### ICAP RESPONSE: 100 Continue"); out.write("ICAP/1.0 100 Continue\r\n".getBytes()); out.write("\r\n".getBytes()); } private void sendBadRequest(String cause) throws IOException { - info("### (SERVER: SEND) ### ICAP RESPONSE: 400 Bad request"); out.write("ICAP/1.0 400 Bad request\r\n".getBytes()); if( cause == null ) { sendCloseConnection(); @@ -452,19 +445,16 @@ private void sendBadRequest(String cause) throws IOException { } private void sendServiceNotFound() throws IOException { - info("### (SERVER: SEND) ### ICAP RESPONSE: 404 Service not found"); out.write("ICAP/1.0 404 Service not found\r\n".getBytes()); sendCloseConnection(); } private void sendMethodNotAllowed() throws IOException { - info("### (SERVER: SEND) ### ICAP RESPONSE: 405 Method not allowed"); out.write("ICAP/1.0 405 Method not allowed\r\n".getBytes()); sendCloseConnection(); } private void sendServerError(String cause) throws IOException { - info("### (SERVER: SEND) ### ICAP RESPONSE: 500 Server Error"); out.write("ICAP/1.0 500 Server Error\r\n".getBytes()); if( cause == null ) { sendCloseConnection(); @@ -513,8 +503,6 @@ private void handleOptions( String date = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US).format(new Date()); - info("### (SERVER: SEND) ### ICAP RESPONSE: 200 OK"); - out.write(("ICAP/1.0 200 OK\r\n").getBytes()); out.write(("Date: "+date+"\r\n").getBytes()); out.write(("Server: "+serverName+"\r\n").getBytes()); @@ -527,8 +515,8 @@ private void handleOptions( out.write(("Methods: "+REQMOD+", "+RESPMOD+"\r\n").getBytes()); } - out.write(("Service: Java-Tech-Server/1.0\r\n").getBytes()); - out.write(("ISTag:\"ALPHA-B123456-GAMA\"\r\n").getBytes()); + out.write(("Service: ICAP-Server-Java/1.0\r\n").getBytes()); + out.write(("ISTag:\""+UUID.randomUUID().toString()+"\"\r\n").getBytes()); out.write(("Allow: 204\r\n").getBytes()); out.write(("Preview: 0\r\n").getBytes()); out.write(("Transfer-Complete: *\r\n").getBytes()); @@ -589,10 +577,8 @@ private void continueRequestModification() throws Exception { String date = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US).format(new Date()); if( serviceInProgress.startsWith("echo") && httpRequestBody.size() == 0 ) { - info("### (SERVER: SEND) ### ICAP RESPONSE: 204 No Content"); out.write(("ICAP/1.0 204 No Content\r\n").getBytes()); } else { - info("### (SERVER: SEND) ### ICAP RESPONSE: 200 OK"); out.write(("ICAP/1.0 200 OK\r\n").getBytes()); } @@ -619,12 +605,10 @@ private void continueResponseModification() throws Exception { if( serviceInProgress.startsWith("echo") && httpResponseBody.size() == 0 ) { - info("### (SERVER: SEND) ### ICAP RESPONSE: 204 No Content"); out.write(("ICAP/1.0 204 No Content\r\n").getBytes()); } else { - info("### (SERVER: SEND) ### ICAP RESPONSE: 200 OK"); out.write(("ICAP/1.0 200 OK\r\n").getBytes()); } @@ -727,33 +711,27 @@ private void completeHandleEcho() throws Exception { encapsulatedHeaderEcho.append("null-body=").append(offset); } - info("### (SERVER: SEND) ### ICAP RESPONSE HEADER\n: " + encapsulatedHeaderEcho); - out.write(("Encapsulated: "+encapsulatedHeaderEcho+"\r\n").getBytes()); out.write("\r\n".getBytes()); boolean eof = false; if(httpRequestHeaders.size() > 0) { eof = true; - info("### (SERVER: SEND) ### ICAP RESPONSE: HTTP REQUEST HEADER\n" + new String(httpRequestHeaders.toByteArray())); out.write(httpRequestHeaders.toByteArray()); } if(outHttpRequestBody.size() > 0) { eof = true; - info("### (SERVER: SEND) ### ICAP RESPONSE: HTTP REQUEST BODY\n" + new String(outHttpRequestBody.toByteArray())); out.write(outHttpRequestBody.toByteArray()); } if(httpResponseHeaders.size() > 0) { eof = true; - info("### (SERVER: SEND) ### ICAP RESPONSE: HTTP RESPONSE HEADER\n" + new String(httpResponseHeaders.toByteArray())); out.write(httpResponseHeaders.toByteArray()); } if(outHttpResponseBody.size() > 0) { eof = true; - info("### (SERVER: SEND) ### ICAP RESPONSE: HTTP RESPONSE BODY\n" + new String(outHttpResponseBody.toByteArray())); out.write(outHttpResponseBody.toByteArray()); } @@ -786,7 +764,7 @@ private void completeHandleVirusScan() throws Exception { if( threatName != null ) { - responseMessage.append("Virus Found: ").append(threatName); + responseMessage.append("Virus Found: ").append(threatName).append("\n"); outHttpResponseHeaders.write(("Content-Type: text/plain\r\n").getBytes()); outHttpResponseHeaders.write(("Content-Length: "+responseMessage.length()+"\r\n").getBytes()); @@ -834,33 +812,27 @@ private void completeHandleVirusScan() throws Exception { encapsulatedHeaderEcho.append("null-body=").append(offset); } - info("### (SERVER: SEND) ### ICAP RESPONSE HEADER\n: " + encapsulatedHeaderEcho); - out.write(("Encapsulated: "+encapsulatedHeaderEcho+"\r\n").getBytes()); out.write("\r\n".getBytes()); boolean eof = false; if(outHttpRequestHeaders.size() > 0) { eof = true; - info("### (SERVER: SEND) ### ICAP RESPONSE: HTTP REQUEST HEADER\n" + new String(outHttpRequestHeaders.toByteArray())); out.write(outHttpRequestHeaders.toByteArray()); } if(outHttpRequestBody.size() > 0) { eof = true; - info("### (SERVER: SEND) ### ICAP RESPONSE: HTTP REQUEST BODY\n" + new String(outHttpRequestBody.toByteArray())); out.write(outHttpRequestBody.toByteArray()); } if(outHttpResponseHeaders.size() > 0) { eof = true; - info("### (SERVER: SEND) ### ICAP RESPONSE: HTTP RESPONSE HEADER\n" + new String(outHttpResponseHeaders.toByteArray())); out.write(outHttpResponseHeaders.toByteArray()); } if(outHttpResponseBody.size() > 0) { eof = true; - info("### (SERVER: SEND) ### ICAP RESPONSE: HTTP RESPONSE BODY\n" + new String(outHttpResponseBody.toByteArray())); out.write(outHttpResponseBody.toByteArray()); } @@ -875,15 +847,11 @@ private void completeHandleVirusScan() throws Exception { private void findThreatsInPayload() throws Exception { - System.out.println("[ICAP-SERVER] Checking Threats..."); - - String environment = System.getProperty("java.os"); + String environment = System.getProperty("os.name"); if(environment.toLowerCase().contains("windows")) { - System.out.println("[ICAP-SERVER] Checking Threats (Windows)..."); findThreatsInPayloadOnWindows(); } else { - System.out.println("[ICAP-SERVER] Checking Threats (Linux)..."); findThreatsInPayloadOnLinux(); } @@ -956,10 +924,6 @@ private void readStream(byte[] out) throws IOException { } - private void info(String message) { -// Logger.getGlobal().info(message); - } - private void warning(String message) { Logger.getGlobal().warning(message); } diff --git a/src/main/java/net/rfc3507/server/Daemon.java b/src/main/java/net/rfc3507/server/Daemon.java index 49298da..2786fd7 100644 --- a/src/main/java/net/rfc3507/server/Daemon.java +++ b/src/main/java/net/rfc3507/server/Daemon.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; +import java.util.logging.Logger; public class Daemon { @@ -16,10 +17,13 @@ private void start() throws IOException { ServerSocket server = new ServerSocket(1344); + Logger.getGlobal().info("[ICAP-SERVER] Listening on port 1344"); + while(true) { Socket client = null; try { client = server.accept(); + Logger.getGlobal().info("[ICAP-SERVER] Connection received!"); } catch(IOException e) { e.printStackTrace(); break;