olefy.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Copyright (c) 2020, Dennis Kalbhen <d.kalbhen@heinlein-support.de>
  4. # Copyright (c) 2020, Carsten Rosenberg <c.rosenberg@heinlein-support.de>
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. ###
  18. #
  19. # olefy is a little helper socket to use oletools with rspamd. (https://rspamd.com)
  20. # Please find updates and issues here: https://github.com/HeinleinSupport/olefy
  21. #
  22. ###
  23. from subprocess import Popen, PIPE
  24. import sys
  25. import os
  26. import logging
  27. import asyncio
  28. import time
  29. import magic
  30. import re
  31. # merge variables from /etc/olefy.conf and the defaults
  32. olefy_listen_addr_string = os.getenv('OLEFY_BINDADDRESS', '127.0.0.1,::1')
  33. olefy_listen_port = int(os.getenv('OLEFY_BINDPORT', '10050'))
  34. olefy_tmp_dir = os.getenv('OLEFY_TMPDIR', '/tmp')
  35. olefy_python_path = os.getenv('OLEFY_PYTHON_PATH', '/usr/bin/python3')
  36. olefy_olevba_path = os.getenv('OLEFY_OLEVBA_PATH', '/usr/local/bin/olevba3')
  37. # 10:DEBUG, 20:INFO, 30:WARNING, 40:ERROR, 50:CRITICAL
  38. olefy_loglvl = int(os.getenv('OLEFY_LOGLVL', 20))
  39. olefy_min_length = int(os.getenv('OLEFY_MINLENGTH', 500))
  40. olefy_del_tmp = int(os.getenv('OLEFY_DEL_TMP', 1))
  41. olefy_del_tmp_failed = int(os.getenv('OLEFY_DEL_TMP_FAILED', 1))
  42. # internal used variables
  43. request_time = '0000000000.000000'
  44. olefy_protocol = 'OLEFY'
  45. olefy_ping = 'PING'
  46. olefy_protocol_sep = '\n\n'
  47. olefy_headers = {}
  48. # init logging
  49. logger = logging.getLogger('olefy')
  50. logging.basicConfig(stream=sys.stdout, level=olefy_loglvl, format='olefy %(levelname)s %(funcName)s %(message)s')
  51. logger.debug('olefy listen address string: {} (type {})'.format(olefy_listen_addr_string, type(olefy_listen_addr_string)))
  52. if not olefy_listen_addr_string:
  53. olefy_listen_addr = ""
  54. else:
  55. addr_re = re.compile('[\[" \]]')
  56. olefy_listen_addr = addr_re.sub('', olefy_listen_addr_string.replace("'", "")).split(',')
  57. # log runtime variables
  58. logger.info('olefy listen address: {} (type: {})'.format(olefy_listen_addr, type(olefy_listen_addr)))
  59. logger.info('olefy listen port: {}'.format(olefy_listen_port))
  60. logger.info('olefy tmp dir: {}'.format(olefy_tmp_dir))
  61. logger.info('olefy python path: {}'.format(olefy_python_path))
  62. logger.info('olefy olvba path: {}'.format(olefy_olevba_path))
  63. logger.info('olefy log level: {}'.format(olefy_loglvl))
  64. logger.info('olefy min file length: {}'.format(olefy_min_length))
  65. logger.info('olefy delete tmp file: {}'.format(olefy_del_tmp))
  66. logger.info('olefy delete tmp file when failed: {}'.format(olefy_del_tmp_failed))
  67. if not os.path.isfile(olefy_python_path):
  68. logger.critical('python path not found: {}'.format(olefy_python_path))
  69. exit(1)
  70. if not os.path.isfile(olefy_olevba_path):
  71. logger.critical('olevba path not found: {}'.format(olefy_olevba_path))
  72. exit(1)
  73. # olefy protocol function
  74. def protocol_split( olefy_line ):
  75. header_lines = olefy_line.split('\n')
  76. for line in header_lines:
  77. if line == 'OLEFY/1.0':
  78. olefy_headers['olefy'] = line
  79. elif line != '':
  80. kv = line.split(': ')
  81. if kv[0] != '' and kv[1] != '':
  82. olefy_headers[kv[0]] = kv[1]
  83. logger.debug('olefy_headers: {}'.format(olefy_headers))
  84. # calling oletools
  85. def oletools( stream, tmp_file_name, lid ):
  86. if olefy_min_length > stream.__len__():
  87. logger.error('{} {} bytes (Not Scanning! File smaller than {!r})'.format(lid, stream.__len__(), olefy_min_length))
  88. out = b'[ { "error": "File too small" } ]'
  89. else:
  90. tmp_file = open(tmp_file_name, 'wb')
  91. tmp_file.write(stream)
  92. tmp_file.close()
  93. file_magic = magic.Magic(mime=True, uncompress=True)
  94. file_mime = file_magic.from_file(tmp_file_name)
  95. logger.info('{} {} (libmagic output)'.format(lid, file_mime))
  96. # do the olefy
  97. cmd_tmp = Popen([olefy_python_path, olefy_olevba_path, '-a', '-j' , '-l', 'error', tmp_file_name], stdout=PIPE, stderr=PIPE)
  98. out, err = cmd_tmp.communicate()
  99. out = bytes(out.decode('utf-8', 'ignore').replace(' ', ' ').replace('\t', '').replace('\n', '').replace('XLMMacroDeobfuscator: pywin32 is not installed (only is required if you want to use MS Excel)', ''), encoding="utf-8")
  100. failed = False
  101. if out.__len__() < 30:
  102. logger.error('{} olevba returned <30 chars - rc: {!r}, response: {!r}, error: {!r}'.format(lid,cmd_tmp.returncode,
  103. out.decode('utf-8', 'ignore'), err.decode('utf-8', 'ignore')))
  104. out = b'[ { "error": "Unhandled error - too short olevba response" } ]'
  105. failed = True
  106. elif err.__len__() > 10 and cmd_tmp.returncode == 9:
  107. logger.error("{} olevba stderr >10 chars - rc: {!r}, response: {!r}".format(lid, cmd_tmp.returncode, err.decode("utf-8", "ignore")))
  108. out = b'[ { "error": "Decrypt failed" } ]'
  109. failed = True
  110. elif err.__len__() > 10 and cmd_tmp.returncode > 9:
  111. logger.error('{} olevba stderr >10 chars - rc: {!r}, response: {!r}'.format(lid, cmd_tmp.returncode, err.decode('utf-8', 'ignore')))
  112. out = b'[ { "error": "Unhandled oletools error" } ]'
  113. failed = True
  114. elif cmd_tmp.returncode != 0:
  115. logger.error('{} olevba exited with code {!r}; err: {!r}'.format(lid, cmd_tmp.returncode, err.decode('utf-8', 'ignore')))
  116. failed = True
  117. if failed and olefy_del_tmp_failed == 0:
  118. logger.debug('{} {} FAILED: not deleting tmp file'.format(lid, tmp_file_name))
  119. elif olefy_del_tmp == 1:
  120. logger.debug('{} {} deleting tmp file'.format(lid, tmp_file_name))
  121. os.remove(tmp_file_name)
  122. logger.debug('{} response: {}'.format(lid, out.decode('utf-8', 'ignore')))
  123. return out + b'\t\n\n\t'
  124. # Asyncio data handling, default AIO-Functions
  125. class AIO(asyncio.Protocol):
  126. def __init__(self):
  127. self.extra = bytearray()
  128. def connection_made(self, transport):
  129. global request_time
  130. peer = transport.get_extra_info('peername')
  131. logger.debug('{} new connection was made'.format(peer))
  132. self.transport = transport
  133. request_time = str(time.time())
  134. def data_received(self, request, msgid=1):
  135. peer = self.transport.get_extra_info('peername')
  136. logger.debug('{} data received from new connection'.format(peer))
  137. self.extra.extend(request)
  138. def eof_received(self):
  139. peer = self.transport.get_extra_info('peername')
  140. olefy_protocol_err = False
  141. proto_ck = self.extra[0:2000].decode('utf-8', 'ignore')
  142. headers = proto_ck[0:proto_ck.find(olefy_protocol_sep)]
  143. if olefy_protocol == headers[0:5]:
  144. self.extra = bytearray(self.extra[len(headers)+2:len(self.extra)])
  145. protocol_split(headers)
  146. else:
  147. olefy_protocol_err = True
  148. if olefy_ping == headers[0:4]:
  149. is_ping = True
  150. else:
  151. is_ping = False
  152. rspamd_id = olefy_headers['Rspamd-ID'][:6] or ''
  153. lid = 'Rspamd-ID' in olefy_headers and '<'+rspamd_id+'>'
  154. tmp_file_name = olefy_tmp_dir+'/'+request_time+'.'+str(peer[1])+'.'+rspamd_id
  155. logger.debug('{} {} choosen as tmp filename'.format(lid, tmp_file_name))
  156. if not is_ping or olefy_loglvl == 10:
  157. logger.info('{} {} bytes (stream size)'.format(lid, self.extra.__len__()))
  158. if olefy_ping == headers[0:4]:
  159. logger.debug('{} PING request'.format(peer))
  160. out = b'PONG'
  161. elif olefy_protocol_err == True or olefy_headers['olefy'] != 'OLEFY/1.0':
  162. logger.error('{} Protocol ERROR: no OLEFY/1.0 found'.format(lid))
  163. out = b'[ { "error": "Protocol error" } ]'
  164. elif 'Method' in olefy_headers:
  165. if olefy_headers['Method'] == 'oletools':
  166. out = oletools(self.extra, tmp_file_name, lid)
  167. else:
  168. logger.error('Protocol ERROR: Method header not found')
  169. out = b'[ { "error": "Protocol error: Method header not found" } ]'
  170. self.transport.write(out)
  171. if not is_ping or olefy_loglvl == 10:
  172. logger.info('{} {} response send: {!r}'.format(lid, peer, out))
  173. self.transport.close()
  174. # start the listeners
  175. loop = asyncio.get_event_loop()
  176. # each client connection will create a new protocol instance
  177. coro = loop.create_server(AIO, olefy_listen_addr, olefy_listen_port)
  178. server = loop.run_until_complete(coro)
  179. for sockets in server.sockets:
  180. logger.info('serving on {}'.format(sockets.getsockname()))
  181. # XXX serve requests until KeyboardInterrupt, not needed for production
  182. try:
  183. loop.run_forever()
  184. except KeyboardInterrupt:
  185. pass
  186. # graceful shutdown/reload
  187. server.close()
  188. loop.run_until_complete(server.wait_closed())
  189. loop.close()
  190. logger.info('stopped serving')