@@ -81,6 +81,17 @@ def cmd_alias(*args)
81
81
args . shift
82
82
end
83
83
name = args . shift
84
+ # alias name can NEVER be certain reserved words like 'alias', add any other reserved words here
85
+ # We prevent the user from naming the alias "alias" cuz they could end up unable to clear the aliases,
86
+ # for example you 'alias -f set unset and then 'alias -f alias sessions', now you're screwed. The byproduct
87
+ # of this is that it prevents you from aliasing 'alias' to 'alias -f' etc, but that's acceptable
88
+ reserved_words = [ /^alias/i ]
89
+ reserved_words . each do |regex |
90
+ if name =~ regex
91
+ print_error "You cannot use #{ name } as the name for an alias, sorry"
92
+ return false
93
+ end
94
+ end
84
95
85
96
if clear
86
97
# clear all aliases if "*"
@@ -101,15 +112,37 @@ def cmd_alias(*args)
101
112
end
102
113
# smash everything that's left together
103
114
value = args . join ( " " )
115
+ value . strip!
116
+ # valule can NEVER be certain bad words like 'rm -rf /', add any other reserved words here
117
+ # this is basic idiot protection, not meant to be impervious to subversive intentions
118
+ reserved_words = [ /^rm +(-rf|-r +-f|-f +-r) +\/ .*$/ ]
119
+ reserved_words . each do |regex |
120
+ if value =~ regex
121
+ print_error "You cannot use #{ value } as the value for an alias, sorry"
122
+ return false
123
+ end
124
+ end
104
125
105
- if is_valid_alias? ( name , value )
106
- if force or ( not Rex ::FileUtils . find_full_path ( name ) and not @aliases . keys . include? ( name ) )
107
- register_alias ( name , value )
108
- else
109
- print_error ( "#{ name } already exists as system command or current alias, use -f to force" )
126
+ is_valid_alias = is_valid_alias? ( name , value )
127
+ #print_good "Alias validity = #{is_valid_alias.to_s}"
128
+ is_sys_cmd = Rex ::FileUtils . find_full_path ( name )
129
+ is_already_alias = @aliases . keys . include? ( name )
130
+ if is_valid_alias and not is_sys_cmd and not is_already_alias
131
+ register_alias ( name , value )
132
+ elsif force
133
+ if not is_valid_alias
134
+ print_status "The alias failed validation, but force is set so we allow this. This is often the case"
135
+ print_status "when for instance 'exploit' is being overridden but msfconsole is not currently in the"
136
+ print_status "exploit context (an exploit is not loaded), or you are overriding a system command"
110
137
end
138
+ register_alias ( name , value )
111
139
else
112
- print_error ( "\' #{ name } \' is not a permitted name or \' #{ value } \' is not a valid/permitted console or system command" )
140
+ print_error ( "#{ name } already exists as a system command, use -f to force override" ) if is_sys_cmd
141
+ print_error ( "#{ name } is already an alias, use -f to force override" ) if is_already_alias
142
+ if not is_valid_alias and not force
143
+ print_error ( "\' #{ name } \' is not a permitted name or \' #{ value } \' is not valid/permitted" )
144
+ print_error ( "It's possible the responding dispatcher isn't loaded yet, try changing to the proper context or using -f to force" )
145
+ end
113
146
end
114
147
end
115
148
end
@@ -193,20 +226,42 @@ def deregister_alias(name)
193
226
# Validate a proposed alias
194
227
#
195
228
def is_valid_alias? ( name , value )
196
- # some "bad words" to avoid for the value. value would have to not match these regexes
197
- # this is just basic idiot protection, it's not meant to be "undefeatable"
229
+ #print_good "Assessing validay for #{name} and #{value}"
230
+ # we validate two things, the name and the value
231
+
232
+ ### name
233
+ # we don't check if this alias name exists or if it's a console command already etc as -f can override
234
+ # that so those need to be checked externally, we pretty much just check to see if the name is sane
235
+ name . strip!
236
+ bad_words = [ /\* / ] # add any additional "bad word" regexes here
237
+ bad_words . each do |regex |
238
+ # don't mess around, just return false in this case, prevents wasted processing
239
+ return false if name =~ regex
240
+ end
241
+
242
+ ### value
243
+ # value is considered valid if it's a ref to a valid console cmd, a system executable, or an existing
244
+ # alias AND isn't a "bad word"
245
+ # Here we check for "bad words" to avoid for the value...value would have to NOT match these regexes
246
+ # this is just basic idiot protection
198
247
value . strip!
199
- bad_words = [ /^rm +(-rf|-r +-f|-f +-r) + \/ +.*$/ , /^ msfconsole$/]
248
+ bad_words = [ /^msfconsole$/ ]
200
249
bad_words . each do |regex |
201
250
# don't mess around, just return false if we match
202
251
return false if value =~ regex
203
252
end
253
+
204
254
# we're only gonna validate the first part of the cmd, e.g. just ls from "ls -lh"
205
255
value = value . split ( " " ) . first
206
- valid_value = false
207
-
208
- # value is considered valid if it's a ref to a valid console command or
209
- # a system executable or existing alias
256
+ if @aliases . keys . include? ( value )
257
+ return true
258
+ else
259
+ [ value , value +".exe" ] . each do |cmd |
260
+ if Rex ::FileUtils . find_full_path ( cmd )
261
+ return true
262
+ end
263
+ end
264
+ end
210
265
211
266
# gather all the current commands the driver's dispatcher's have & check 'em
212
267
driver . dispatcher_stack . each do |dispatcher |
@@ -215,40 +270,14 @@ def is_valid_alias?(name,value)
215
270
next if ( dispatcher . commands . length == 0 )
216
271
217
272
if dispatcher . respond_to? ( "cmd_#{ value . split ( " " ) . first } " )
218
- valid_value = true
219
- break
220
- end
221
- end
222
- if not valid_value # then check elsewhere
223
- if @aliases . keys . include? ( value )
224
- valid_value = true
273
+ #print_status "Dispatcher (#{dispatcher.name}) responds to cmd_#{value.split(" ").first}"
274
+ return true
225
275
else
226
- [ value , value +".exe" ] . each do |cmd |
227
- if Rex ::FileUtils . find_full_path ( cmd )
228
- valid_value = true
229
- end
230
- end
276
+ #print_status "Dispatcher (#{dispatcher.name}) does not respond to cmd_#{value.split(" ").first}"
231
277
end
232
278
end
233
- # go ahead and return false at this point if the value isn't valid
234
- return false if not valid_value
235
-
236
- # we don't check if this alias name exists or if it's a console command already etc as
237
- # -f can override that so those need to be checked externally.
238
- # We pretty much just check to see if the name is sane
239
- valid_name = true
240
- name . strip!
241
- bad_words = [ /^alias$/ , /\* / ]
242
- # there are probably a bunch of others that need to be added here. We prevent the user
243
- # from naming the alias "alias" cuz they can end up unable to clear the aliases
244
- # for example you 'alias -f set unse't and then 'alias -f alias sessions', now you're
245
- # screwed. This prevents you from aliasing alias to alias -f etc, but no biggie.
246
- bad_words . each do |regex |
247
- # don't mess around, just return false in this case, prevents wasted processing
248
- return false if name =~ regex
249
- end
250
279
251
- return valid_name
280
+ return false
252
281
end
253
282
254
283
#
0 commit comments