I am using the python's socket module to make an ICAP REQMOD request for scanning the file for viruses.
I am able to establish the connection to the ICAP server and able to send a POST request to using the following code.
print("----- REQMOD - POST -----")
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as msg:
sys.stderr.write("[ERROR] %s\n" % msg[1])
sys.exit(1)
try:
sock.connect((HOST, PORT))
except socket.error as msg:
sys.stderr.write("[ERROR] %s\n" % msg[1])
sys.exit(2)
sock.send( "REQMOD %s ICAP/1.0\r\n".encode() % ( SERVICE.encode() ) )
sock.send( "Host: %s\r\n".encode() % ( HOST.encode() ) )
sock.send( "Encapsulated: req-hdr=0, req-body=147\r\n".encode() )
sock.send( "\r\n".encode() )
sock.send( "POST /origin-resource/form.pl HTTP/1.1\r\n".encode() )
sock.send( "Host: www.origin-server.com\r\n".encode() )
sock.send( "Accept: text/html, text/plain\r\n".encode() )
sock.send( "Accept-Encoding: compress zip\r\n".encode() )
sock.send( "Pragma: no-cache\r\n".encode() )
sock.send( "\r\n".encode() )
sock.send( "1e\r\n".encode() )
sock.send( "I am posting this information.\r\n".encode() )
sock.send( "0\r\n".encode() )
sock.send( "\r\n".encode() )
file_name = "sample_file.txt"
file = open (file_name, "rb") # read bytes
sock.send(bytearray(file.read()))
file.close()
print ("Completed sock.send()")
data = sock.recv(1024)
sock.close()
print ("Completed sock.recv()")
print()
print(data)
The response from the ICAP server contains all the headers and identifies the traffic containing the "posting" word in it.
However, the file that is sent to the ICAP server is not getting identified by it and hence it is not getting scanned.
I looked over the internet to find a solution to attaching a file in REQMOD but could not get any working example.
What is the syntax for attaching a file to the request?