olefy.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. skip_olefy = os.getenv('SKIP_OLEFY', '')
  32. if skip_olefy.lower() in ['yes', 'y']:
  33. print("SKIP_OLEFY=y, skipping Olefy...")
  34. time.sleep(365 * 24 * 60 * 60)
  35. sys.exit(0)
  36. # merge variables from /etc/olefy.conf and the defaults
  37. olefy_listen_addr_string = os.getenv('OLEFY_BINDADDRESS', '127.0.0.1,::1')
  38. olefy_listen_port = int(os.getenv('OLEFY_BINDPORT', '10050'))
  39. olefy_tmp_dir = os.getenv('OLEFY_TMPDIR', '/tmp')
  40. olefy_python_path = os.getenv('OLEFY_PYTHON_PATH', '/usr/bin/python3')
  41. olefy_olevba_path = os.getenv('OLEFY_OLEVBA_PATH', '/usr/local/bin/olevba3')
  42. # 10:DEBUG, 20:INFO, 30:WARNING, 40:ERROR, 50:CRITICAL
  43. olefy_loglvl = int(os.getenv('OLEFY_LOGLVL', 20))
  44. olefy_min_length = int(os.getenv('OLEFY_MINLENGTH', 500))
  45. olefy_del_tmp = int(os.getenv('OLEFY_DEL_TMP', 1))
  46. olefy_del_tmp_failed = int(os.getenv('OLEFY_DEL_TMP_FAILED', 1))
  47. # internal used variables
  48. request_time = '0000000000.000000'
  49. olefy_protocol = 'OLEFY'
  50. olefy_ping = 'PING'
  51. olefy_protocol_sep = '\n\n'
  52. olefy_headers = {}
  53. # init logging
  54. logger = logging.getLogger('olefy')
  55. logging.basicConfig(stream=sys.stdout, level=olefy_loglvl, format='olefy %(levelname)s %(funcName)s %(message)s')
  56. logger.debug('olefy listen address string: {} (type {})'.format(olefy_listen_addr_string, type(olefy_listen_addr_string)))
  57. if not olefy_listen_addr_string:
  58. olefy_listen_addr = ""
  59. else:
  60. addr_re = re.compile('[\[" \]]')
  61. olefy_listen_addr = addr_re.sub('', olefy_listen_addr_string.replace("'", "")).split(',')
  62. # log runtime variables
  63. logger.info('olefy listen address: {} (type: {})'.format(olefy_listen_addr, type(olefy_listen_addr)))
  64. logger.info('olefy listen port: {}'.format(olefy_listen_port))
  65. logger.info('olefy tmp dir: {}'.format(olefy_tmp_dir))
  66. logger.info('olefy python path: {}'.format(olefy_python_path))
  67. logger.info('olefy olvba path: {}'.format(olefy_olevba_path))
  68. logger.info('olefy log level: {}'.format(olefy_loglvl))
  69. logger.info('olefy min file length: {}'.format(olefy_min_length))
  70. logger.info('olefy delete tmp file: {}'.format(olefy_del_tmp))
  71. logger.info('olefy delete tmp file when failed: {}'.format(olefy_del_tmp_failed))
  72. if not os.path.isfile(olefy_python_path):
  73. logger.critical('python path not found: {}'.format(olefy_python_path))
  74. exit(1)
  75. if not os.path.isfile(olefy_olevba_path):
  76. logger.critical('olevba path not found: {}'.format(olefy_olevba_path))
  77. exit(1)
  78. # olefy protocol function
  79. def protocol_split( olefy_line ):
  80. header_lines = olefy_line.split('\n')
  81. for line in header_lines:
  82. if line == 'OLEFY/1.0':
  83. olefy_headers['olefy'] = line
  84. elif line != '':
  85. kv = line.split(': ')
  86. if kv[0] != '' and kv[1] != '':
  87. olefy_headers[kv[0]] = kv[1]
  88. logger.debug('olefy_headers: {}'.format(olefy_headers))
  89. # calling oletools
  90. def oletools( stream, tmp_file_name, lid ):
  91. if olefy_min_length > stream.__len__():
  92. logger.error('{} {} bytes (Not Scanning! File smaller than {!r})'.format(lid, stream.__len__(), olefy_min_length))
  93. out = b'[ { "error": "File too small" } ]'
  94. else:
  95. tmp_file = open(tmp_file_name, 'wb')
  96. tmp_file.write(stream)
  97. tmp_file.close()
  98. file_magic = magic.Magic(mime=True, uncompress=True)
  99. file_mime = file_magic.from_file(tmp_file_name)
  100. logger.info('{} {} (libmagic output)'.format(lid, file_mime))
  101. # do the olefy
  102. cmd_tmp = Popen([olefy_python_path, olefy_olevba_path, '-a', '-j' , '-l', 'error', tmp_file_name], stdout=PIPE, stderr=PIPE)
  103. out, err = cmd_tmp.communicate()
  104. 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")
  105. failed = False
  106. if out.__len__() < 30:
  107. logger.error('{} olevba returned <30 chars - rc: {!r}, response: {!r}, error: {!r}'.format(lid,cmd_tmp.returncode,
  108. out.decode('utf-8', 'ignore'), err.decode('utf-8', 'ignore')))
  109. out = b'[ { "error": "Unhandled error - too short olevba response" } ]'
  110. failed = True
  111. elif err.__len__() > 10 and cmd_tmp.returncode == 9:
  112. logger.error("{} olevba stderr >10 chars - rc: {!r}, response: {!r}".format(lid, cmd_tmp.returncode, err.decode("utf-8", "ignore")))
  113. out = b'[ { "error": "Decrypt failed" } ]'
  114. failed = True
  115. elif err.__len__() > 10 and cmd_tmp.returncode > 9:
  116. logger.error('{} olevba stderr >10 chars - rc: {!r}, response: {!r}'.format(lid, cmd_tmp.returncode, err.decode('utf-8', 'ignore')))
  117. out = b'[ { "error": "Unhandled oletools error" } ]'
  118. failed = True
  119. elif cmd_tmp.returncode != 0:
  120. logger.error('{} olevba exited with code {!r}; err: {!r}'.format(lid, cmd_tmp.returncode, err.decode('utf-8', 'ignore')))
  121. failed = True
  122. if failed and olefy_del_tmp_failed == 0:
  123. logger.debug('{} {} FAILED: not deleting tmp file'.format(lid, tmp_file_name))
  124. elif olefy_del_tmp == 1:
  125. logger.debug('{} {} deleting tmp file'.format(lid, tmp_file_name))
  126. os.remove(tmp_file_name)
  127. logger.debug('{} response: {}'.format(lid, out.decode('utf-8', 'ignore')))
  128. return out + b'\t\n\n\t'
  129. # Asyncio data handling, default AIO-Functions
  130. class AIO(asyncio.Protocol):
  131. def __init__(self):
  132. self.extra = bytearray()
  133. def connection_made(self, transport):
  134. global request_time
  135. peer = transport.get_extra_info('peername')
  136. logger.debug('{} new connection was made'.format(peer))
  137. self.transport = transport
  138. request_time = str(time.time())
  139. def data_received(self, request, msgid=1):
  140. peer = self.transport.get_extra_info('peername')
  141. logger.debug('{} data received from new connection'.format(peer))
  142. self.extra.extend(request)
  143. def eof_received(self):
  144. peer = self.transport.get_extra_info('peername')
  145. olefy_protocol_err = False
  146. proto_ck = self.extra[0:2000].decode('utf-8', 'ignore')
  147. headers = proto_ck[0:proto_ck.find(olefy_protocol_sep)]
  148. if olefy_protocol == headers[0:5]:
  149. self.extra = bytearray(self.extra[len(headers)+2:len(self.extra)])
  150. protocol_split(headers)
  151. else:
  152. olefy_protocol_err = True
  153. if olefy_ping == headers[0:4]:
  154. is_ping = True
  155. else:
  156. is_ping = False
  157. rspamd_id = olefy_headers['Rspamd-ID'][:6] or ''
  158. lid = 'Rspamd-ID' in olefy_headers and '<'+rspamd_id+'>'
  159. tmp_file_name = olefy_tmp_dir+'/'+request_time+'.'+str(peer[1])+'.'+rspamd_id
  160. logger.debug('{} {} choosen as tmp filename'.format(lid, tmp_file_name))
  161. if not is_ping or olefy_loglvl == 10:
  162. logger.info('{} {} bytes (stream size)'.format(lid, self.extra.__len__()))
  163. if olefy_ping == headers[0:4]:
  164. logger.debug('{} PING request'.format(peer))
  165. out = b'PONG'
  166. elif olefy_protocol_err == True or olefy_headers['olefy'] != 'OLEFY/1.0':
  167. logger.error('{} Protocol ERROR: no OLEFY/1.0 found'.format(lid))
  168. out = b'[ { "error": "Protocol error" } ]'
  169. elif 'Method' in olefy_headers:
  170. if olefy_headers['Method'] == 'oletools':
  171. out = oletools(self.extra, tmp_file_name, lid)
  172. else:
  173. logger.error('Protocol ERROR: Method header not found')
  174. out = b'[ { "error": "Protocol error: Method header not found" } ]'
  175. self.transport.write(out)
  176. if not is_ping or olefy_loglvl == 10:
  177. logger.info('{} {} response send: {!r}'.format(lid, peer, out))
  178. self.transport.close()
  179. # start the listeners
  180. loop = asyncio.get_event_loop()
  181. # each client connection will create a new protocol instance
  182. coro = loop.create_server(AIO, olefy_listen_addr, olefy_listen_port)
  183. server = loop.run_until_complete(coro)
  184. for sockets in server.sockets:
  185. logger.info('serving on {}'.format(sockets.getsockname()))
  186. # XXX serve requests until KeyboardInterrupt, not needed for production
  187. try:
  188. loop.run_forever()
  189. except KeyboardInterrupt:
  190. pass
  191. # graceful shutdown/reload
  192. server.close()
  193. loop.run_until_complete(server.wait_closed())
  194. loop.close()
  195. logger.info('stopped serving')