Skip to content

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
claudineyns committed Jun 30, 2019
1 parent 0ad5ebc commit a0027f4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 50 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@

<developers>
<developer>
<id>claudineynascimento</id>
<id>claudineyns</id>
<name>Claudiney Nascimento</name>
<email>[email protected]</email>
<url>https://github.com/claudineynascimento/</url>
<url>https://github.com/claudineyns/</url>
</developer>
</developers>

<url>https://github.com/claudineynascimento/icap-server</url>
<url>https://github.com/claudineyns/icap-server</url>

<licenses>
<license>
<name>MIT License</name>
<url>https://raw.githubusercontent.com/claudineynascimento/icap-server/master/LICENSE</url>
<url>https://raw.githubusercontent.com/claudineyns/icap-server/master/LICENSE</url>
</license>
</licenses>

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/rfc3507/av/clamav/ClamAVCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> daemonExec = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,8 +17,6 @@

public class WindowsDefenderAntivirus {

private static String checkResult = "";

public WindowsDefenderResponse checkThreat(byte[] content)
throws WindowsDefenderException {

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
46 changes: 5 additions & 41 deletions src/main/java/net/rfc3507/server/ClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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()));
}

}
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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());
}

Expand All @@ -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());

}
Expand Down Expand Up @@ -727,33 +711,27 @@ private void completeHandleEcho() throws Exception {
encapsulatedHeaderEcho.append("null-body=").append(offset);
}

info("### (SERVER: SEND) ### ICAP RESPONSE HEADER\n<Encapsulated>: " + 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());
}

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -834,33 +812,27 @@ private void completeHandleVirusScan() throws Exception {
encapsulatedHeaderEcho.append("null-body=").append(offset);
}

info("### (SERVER: SEND) ### ICAP RESPONSE HEADER\n<Encapsulated>: " + 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());
}

Expand All @@ -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();
}

Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/rfc3507/server/Daemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Logger;

public class Daemon {

Expand All @@ -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;
Expand Down

0 comments on commit a0027f4

Please sign in to comment.