Class: Msf::Plugin::Alias::AliasCommandDispatcher

Inherits:
Object
  • Object
show all
Includes:
Ui::Console::CommandDispatcher
Defined in:
plugins/alias.rb

Constant Summary collapse

@@alias_opts =
Rex::Parser::Arguments.new(
  "-h" => [ false, "Help banner."                    ],
  "-c" => [ true, "Clear an alias (* to clear all)."],
  "-f" => [ true,  "Force an alias assignment."      ]
)

Instance Attribute Summary collapse

Attributes included from Ui::Console::CommandDispatcher

#driver

Attributes included from Rex::Ui::Text::DispatcherShell::CommandDispatcher

#shell, #tab_complete_items

Instance Method Summary collapse

Methods included from Ui::Console::CommandDispatcher

#active_module, #active_module=, #active_session, #active_session=, #build_range_array, #docs_dir, #framework, #log_error, #remove_lines

Methods included from Rex::Ui::Text::DispatcherShell::CommandDispatcher

#cmd_help, #cmd_help_help, #cmd_help_tabs, #deprecated_cmd, #deprecated_commands, #deprecated_help, #docs_dir, #help_to_s, #print, #print_error, #print_good, #print_line, #print_status, #print_warning, #tab_complete_directory, #tab_complete_filenames, #tab_complete_generic, #tab_complete_source_address, #update_prompt

Constructor Details

#initialize(driver) ⇒ AliasCommandDispatcher

Returns a new instance of AliasCommandDispatcher.


10
11
12
13
# File 'plugins/alias.rb', line 10

def initialize(driver)
  super(driver)
  @aliases = {}
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases


9
10
11
# File 'plugins/alias.rb', line 9

def aliases
  @aliases
end

Instance Method Details

#cmd_alias(*args) ⇒ Object

the main alias command handler

usage: alias [options] [name [value]]


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'plugins/alias.rb', line 39

def cmd_alias(*args)
  # we parse args manually instead of using @@alias.opts.parse to handle special cases
  case args.length
  when 0 # print the list of current aliases
    if @aliases.length == 0
      return print_status("No aliases currently defined")
    else
      tbl = Rex::Text::Table.new(
        'Header'  => "Current Aliases",
        'Prefix'  => "\n",
        'Postfix' => "\n",
        'Columns' => [ '', 'Alias Name', 'Alias Value' ]
      )
      # add 'alias' in front of each row so that the output can be copy pasted into an rc file if desired
      @aliases.each_pair do |key,val|
        tbl << ["alias",key,val]
      end
      return print(tbl.to_s)
    end
  when 1 # display the alias if one matches this name (or help)
    return cmd_alias_help if args[0] == "-h" or args[0] == "--help"
    if @aliases.keys.include?(args[0])
      print_status("\'#{args[0]}\' is aliased to \'#{@aliases[args[0]]}\'")
    else
      print_status("\'#{args[0]}\' is not currently aliased")
    end
  else # let's see if we can assign or clear the alias
    force = false
    clear = false
    # if using -f or -c, they must be the first arg, because -f/-c may also show up in the alias
    # value so we can't do something like if args.include("-f") or delete_if etc
    # we should never have to force and clear simultaneously.
    if args[0] == "-f"
      force = true
      args.shift
    elsif args[0] == "-c"
      clear = true
      args.shift
    end
    name = args.shift
    # alias name can NEVER be certain reserved words like 'alias', add any other reserved words here
    # We prevent the user from naming the alias "alias" cuz they could end up unable to clear the aliases,
    # for example you 'alias -f set unset and then 'alias -f alias sessions', now you're screwed.  The byproduct
    # of this is that it prevents you from aliasing 'alias' to 'alias -f' etc, but that's acceptable
    reserved_words = [/^alias$/i]
    reserved_words.each do |regex|
      if name =~ regex
        print_error "You cannot use #{name} as the name for an alias, sorry"
        return false
      end
    end

    if clear
      # clear all aliases if "*"
      if name == "*"
        @aliases.keys.each do |a|
          deregister_alias(a)
        end
        print_status "Cleared all aliases"
      else # clear the named alias if it exists
        if @aliases.keys.include?(name)
          deregister_alias(name)
          print_status "Cleared alias #{name}"
        else
          print_error("#{name} is not a currently active alias")
        end
      end
      return
    end
    # smash everything that's left together
    value = args.join(" ")
    value.strip!
    # value can NEVER be certain bad words like 'rm -rf /', add any other reserved words here
    # this is basic idiot protection, not meant to be impervious to subversive intentions
    reserved_words = [/^rm +(-rf|-r +-f|-f +-r) +\/.*$/]
    reserved_words.each do |regex|
      if value =~ regex
        print_error "You cannot use #{value} as the value for an alias, sorry"
        return false
      end
    end

    is_valid_alias = is_valid_alias?(name,value)
    #print_good "Alias validity = #{is_valid_alias.to_s}"
    is_sys_cmd = Rex::FileUtils.find_full_path(name)
    is_already_alias = @aliases.keys.include?(name)
    if is_valid_alias and not is_sys_cmd and not is_already_alias
      register_alias(name, value)
    elsif force
      if not is_valid_alias
        print_status "The alias failed validation, but force is set so we allow this.  This is often the case"
        print_status "when for instance 'exploit' is being overridden but msfconsole is not currently in the"
        print_status "exploit context (an exploit is not loaded), or you are overriding a system command"
      end
      register_alias(name, value)
    else
      print_error("#{name} already exists as a system command, use -f to force override") if is_sys_cmd
      print_error("#{name} is already an alias, use -f to force override") if is_already_alias
      if not is_valid_alias and not force
        print_error("\'#{name}\' is not a permitted name or \'#{value}\' is not valid/permitted")
        print_error("It's possible the responding dispatcher isn't loaded yet, try changing to the proper context or using -f to force")
      end
    end
  end
end

#cmd_alias_helpObject


145
146
147
148
149
# File 'plugins/alias.rb', line 145

def cmd_alias_help
  print_line "Usage: alias [options] [name [value]]"
  print_line
  print(@@alias_opts.usage())
end

#cmd_alias_tabs(str, words) ⇒ Object

Tab completion for the alias command


154
155
156
157
158
159
160
161
162
# File 'plugins/alias.rb', line 154

def cmd_alias_tabs(str, words)
  if words.length <= 1
    #puts "1 word or less"
    return @@alias_opts.fmt.keys + tab_complete_aliases_and_commands
  else
    #puts "more than 1 word"
    return tab_complete_aliases_and_commands
  end
end

#commandsObject

Returns the hash of commands supported by this dispatcher.


27
28
29
30
31
32
33
# File 'plugins/alias.rb', line 27

def commands # driver.dispatcher_stack[3].commands
  {
    "alias" => "create or view an alias."
  #			"alias_clear" => "clear an alias (or all aliases).",
  #			"alias_force" => "Force an alias (such as to override)"
  }.merge(aliases) # make aliased commands available as commands of their own
end

#nameObject


15
16
17
# File 'plugins/alias.rb', line 15

def name
  "Alias"
end