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# Copyright (c) 2013-2017 CORE Security Technologies 

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# Protocol Attack Base Class definition 

8# 

9# Authors: 

10# Alberto Solino (@agsolino) 

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

12# 

13# Description: 

14# Defines a base class for all attacks + loads all available modules 

15# 

16# ToDo: 

17# 

18import os, sys 

19import pkg_resources 

20from impacket import LOG 

21from threading import Thread 

22 

23PROTOCOL_ATTACKS = {} 

24 

25# Base class for Protocol Attacks for different protocols (SMB, MSSQL, etc) 

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

27# writing a plugin for protocol clients: 

28# PROTOCOL_ATTACK_CLASS = "<name of the class for the plugin>" 

29# or (to support multiple classes in one file) 

30# PROTOCOL_ATTACK_CLASSES = ["<name of the class for the plugin>", "<another class>"] 

31# These classes must have the attribute PLUGIN_NAMES which is a list of protocol names 

32# that will be matched later with the relay targets (e.g. SMB, LDAP, etc) 

33class ProtocolAttack(Thread): 

34 PLUGIN_NAMES = ['PROTOCOL'] 

35 def __init__(self, config, client, username): 

36 Thread.__init__(self) 

37 # Set threads as daemon 

38 self.daemon = True 

39 self.config = config 

40 self.client = client 

41 # By default we only use the username and remove the domain 

42 self.username = username.split('/')[1] 

43 

44 def run(self): 

45 raise RuntimeError('Virtual Function') 

46 

47for file in pkg_resources.resource_listdir('impacket.examples.ntlmrelayx', 'attacks'): 

48 if file.find('__') >= 0 or file.endswith('.py') is False: 

49 continue 

50 # This seems to be None in some case (py3 only) 

51 # __spec__ is py3 only though, but I haven't seen this being None on py2 

52 # so it should cover all cases. 

53 try: 

54 package = __spec__.name # Python 3 

55 except NameError: 

56 package = __package__ # Python 2 

57 __import__(package + '.' + os.path.splitext(file)[0]) 

58 module = sys.modules[package + '.' + os.path.splitext(file)[0]] 

59 try: 

60 pluginClasses = set() 

61 try: 

62 if hasattr(module, 'PROTOCOL_ATTACK_CLASSES'): 62 ↛ 64line 62 didn't jump to line 64, because the condition on line 62 was never true

63 # Multiple classes 

64 for pluginClass in module.PROTOCOL_ATTACK_CLASSES: 

65 pluginClasses.add(getattr(module, pluginClass)) 

66 else: 

67 # Single class 

68 pluginClasses.add(getattr(module, getattr(module, 'PROTOCOL_ATTACK_CLASS'))) 

69 except Exception as e: 

70 LOG.debug(e) 

71 pass 

72 

73 for pluginClass in pluginClasses: 

74 for pluginName in pluginClass.PLUGIN_NAMES: 

75 LOG.debug('Protocol Attack %s loaded..' % pluginName) 

76 PROTOCOL_ATTACKS[pluginName] = pluginClass 

77 except Exception as e: 

78 LOG.debug(str(e))