Skip to content

Commit

Permalink
debug clamd
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Bayr committed Nov 5, 2024
1 parent 6845d5c commit 8d4a8ee
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 80 deletions.
2 changes: 1 addition & 1 deletion sddi-urban/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ COPY supervisor/supervisord.conf /etc/supervisor
COPY supervisor/start-supervisor.sh /srv/app/docker-afterinit.d

# tmp debug clamav #####
#COPY clamav/__init__.py /usr/local/lib/python3.8/dist-packages/clamd/__init__.py
COPY clamav/__init__.py /usr/local/lib/python3.8/dist-packages/clamd/__init__.py

RUN set -ex && \
ckan config-tool "${CKAN_INI}" "ckan.plugins = ${CKAN__PLUGINS}" && \
Expand Down
84 changes: 5 additions & 79 deletions sddi-urban/clamav/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
import contextlib
import re
import base64
import logging

log = logging.getLogger(__name__)

scan_response = re.compile(r"^(?P<path>.*): ((?P<virus>.+) )?(?P<status>(FOUND|OK|ERROR))$")
EICAR = base64.b64decode(b'WDVPIVAlQEFQWzRcUFpYNTQoUF4pN0NDKTd9JEVJQ0FSLVNUQU5E' \
Expand Down Expand Up @@ -115,21 +112,12 @@ def shutdown(self):
self._close_socket()

def scan(self, file):
f = open('/srv/app/data/MB_clamd_init_01', 'w')
f.writelines('SCAN', file)
f.close()
return self._file_system_scan('SCAN', file)

def contscan(self, file):
f = open('/srv/app/data/MB_clamd_init_02', 'w')
f.writelines('CONTSCAN', file)
f.close()
return self._file_system_scan('CONTSCAN', file)

def multiscan(self, file):
f = open('/srv/app/data/MB_clamd_init_03', 'w')
f.writelines('MULTISCAN', file)
f.close()
return self._file_system_scan('MULTISCAN', file)

def _basic_command(self, command):
Expand Down Expand Up @@ -174,15 +162,6 @@ def _file_system_scan(self, command, file):
filename, reason, status = self._parse_response(result)
dr[filename] = (status, reason)

f = open('/srv/app/data/MB_clamd_init_04', 'w')
f.writelines(result)
f.writelines(filename)
f.writelines(dr[filename])
f.close()
f = open('/srv/app/data/MB_clamd_init_04_2', 'w')
f.writelines(dr)
f.close()

return dr

finally:
Expand All @@ -201,87 +180,34 @@ def instream(self, buff):
- BufferTooLongError: if the buffer size exceeds clamd limits
- ConnectionError: in case of communication problem
"""
f = open('/srv/app/data/MB_clamav_instream_01', 'w')
f.writelines("self\n")
f.writelines(str(self))
f.writelines("\n")
f.writelines("buff\n")
f.writelines(str(buff))
f.writelines("\n")
f.close()
print("MB_clamav_instream_01")
#f.close()

try:
self._init_socket()
f = open('/srv/app/data/MB_clamav_instream_01', 'a')
f.writelines("MB_clamav_instream_02\n")
f.close()
self._send_command('INSTREAM')
f = open('/srv/app/data/MB_clamav_instream_01', 'a')
f.writelines("MB_clamav_instream_03\n")
f.close()

max_chunk_size = 1024 # MUST be < StreamMaxLength in /etc/clamav/clamd.conf

chunk = buff.read(max_chunk_size)
f = open('/srv/app/data/MB_clamav_instream_01', 'a')
f.writelines("MB_clamav_instream_04\n")
f.writelines("chunk\n")
f.writelines(str(chunk))
f.writelines("\n")
f.close()

while chunk:
f = open('/srv/app/data/MB_clamav_instream_01', 'a')
f.writelines("MB_clamav_instream_05\n")
f.close()
print('XXXXXXXXXXX 1')
size = struct.pack(b'!L', len(chunk))
f = open('/srv/app/data/MB_clamav_instream_01', 'a')
f.writelines("size\n")
f.writelines(str(size))
f.writelines("\n")
f.close()
print('XXXXXXXXXXX 2')
print(size)
self.clamd_socket.send(size + chunk)
chunk = buff.read(max_chunk_size)
f = open('/srv/app/data/MB_clamav_instream_01', 'a')
f.writelines("chunk-2")
f.writelines(str(chunk))
f.writelines("\n")
f.close()
print('XXXXXXXXXXX 3')

self.clamd_socket.send(struct.pack(b'!L', 0))
f = open('/srv/app/data/MB_clamav_instream_01', 'a')
f.writelines("MB_clamav_instream_06\n")
f.close()

result = self._recv_response()
f = open('/srv/app/data/MB_clamav_instream_01', 'a')
f.writelines("MB_clamav_instream_07\n")
f.writelines("result\n")
f.writelines(str(result) + '\n')
f.close()

if len(result) > 0:
f = open('/srv/app/data/MB_clamav_instream_01', 'a')
f.writelines("MB_clamav_instream_08, len(result) > 0\n")
f.close()
if result == 'INSTREAM size limit exceeded. ERROR':
f = open('/srv/app/data/MB_clamav_instream_01', 'a')
f.writelines("MB_clamav_instream_08, INSTREAM size limit exceeded\n")
f.close()
raise BufferTooLongError(result)


filename, reason, status = self._parse_response(result)
f = open('/srv/app/data/MB_clamav_instream_01', 'a')
f.writelines("MB_clamav_instream_09\n")
f.writelines("filename\n")
f.writelines(str(filename) + '\n')
f.writelines("status\n")
f.writelines(str(status) + '\n')
f.writelines("reason\n")
f.writelines(str(reason) + '\n')
f.close()
return {filename: (status, reason)}

finally:
Expand Down

0 comments on commit 8d4a8ee

Please sign in to comment.