Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# SECUREAUTH LABS. Copyright 2018 SecureAuth Corporation. All rights reserved. 

2# 

3# This software is provided under under a slightly modified version 

4# of the Apache Software License. See the accompanying LICENSE file 

5# for more information. 

6# 

7# A Socks Proxy for the IMAPS Protocol 

8# 

9# Author: 

10# Dirk-jan Mollema (@_dirkjan) / Fox-IT (https://www.fox-it.com) 

11# 

12# Description: 

13# A simple SOCKS server that proxies a connection to relayed IMAPS connections 

14# 

15# ToDo: 

16# 

17from impacket import LOG 

18from impacket.examples.ntlmrelayx.servers.socksplugins.imap import IMAPSocksRelay 

19from impacket.examples.ntlmrelayx.utils.ssl import SSLServerMixin 

20from OpenSSL import SSL 

21 

22# Besides using this base class you need to define one global variable when 

23# writing a plugin: 

24PLUGIN_CLASS = "IMAPSSocksRelay" 

25EOL = '\r\n' 

26 

27class IMAPSSocksRelay(SSLServerMixin, IMAPSocksRelay): 

28 PLUGIN_NAME = 'IMAPS Socks Plugin' 

29 PLUGIN_SCHEME = 'IMAPS' 

30 

31 def __init__(self, targetHost, targetPort, socksSocket, activeRelays): 

32 IMAPSocksRelay.__init__(self, targetHost, targetPort, socksSocket, activeRelays) 

33 

34 @staticmethod 

35 def getProtocolPort(): 

36 return 993 

37 

38 def skipAuthentication(self): 

39 LOG.debug('Wrapping IMAP client connection in TLS/SSL') 

40 self.wrapClientConnection() 

41 try: 

42 if not IMAPSocksRelay.skipAuthentication(self): 

43 # Shut down TLS connection 

44 self.socksSocket.shutdown() 

45 return False 

46 except Exception as e: 

47 LOG.debug('IMAPS: %s' % str(e)) 

48 return False 

49 # Change our outgoing socket to the SSL object of IMAP4_SSL 

50 self.relaySocket = self.session.sslobj 

51 return True 

52 

53 def tunnelConnection(self): 

54 keyword = '' 

55 tag = '' 

56 while True: 

57 try: 

58 data = self.socksSocket.recv(self.packetSize) 

59 except SSL.ZeroReturnError: 

60 # The SSL connection was closed, return 

61 break 

62 # Set the new keyword, unless it is false, then break out of the function 

63 result = self.processTunnelData(keyword, tag, data) 

64 if result is False: 

65 break 

66 # If its not false, it's a tuple with the keyword and tag 

67 keyword, tag = result 

68 

69 if tag != '': 

70 # Store the tag in the session so we can continue 

71 tag = int(tag) 

72 if self.idleState is True: 

73 self.relaySocket.sendall('DONE%s' % EOL) 

74 self.relaySocketFile.readline() 

75 

76 if self.shouldClose: 

77 tag += 1 

78 self.relaySocket.sendall('%s CLOSE%s' % (tag, EOL)) 

79 self.relaySocketFile.readline() 

80 

81 self.session.tagnum = tag + 1