Add a basic command history to MsfConsole (shell and meterpreter)

git-svn-id: file:///home/svn/framework3/trunk@5012 4d416f70-5f16-0410-b530-b9f4589650da
unstable
fab 2007-07-02 18:37:04 +00:00
parent 7ebcf6413a
commit 664b15237b
1 changed files with 121 additions and 8 deletions

View File

@ -15,6 +15,61 @@ module Msf
@@offset = 0 @@offset = 0
###
#
# Basic command history class
#
###
class History
def initialize
@history = [""]
@position = @history.length - 1
end
#
# Get previous command in history array
#
def prev(current)
l = current
l = l[0,1] if (l.length > 0 and l[0,1] == '\n')
l = l[-1,1] if (l.length > 0 and l[-1,1] == '\n')
if (@position > 0)
if (@position == (@history.length - 1))
@history[@history.length - 1] = l
end
@position = @position - 1
return @history[@position]
end
return current
end
#
# Get next command in history array
#
def next(current)
if (@position < @history.length - 1)
@position = @position + 1
return @history[@position]
end
return current
end
#
# Append a new command to history
#
def append(cmd)
@position = @history.length - 1
return if cmd.length == 0
if ( (@position == 0) or (@position > 0 and cmd != @history[@position - 1]) )
@history[@position] = cmd
@position = @position + 1
@history.push('')
end
end
end
def initialize(iter) def initialize(iter)
# Style # Style
console_style = File.join(driver.resource_directory, 'style', 'console.rc') console_style = File.join(driver.resource_directory, 'style', 'console.rc')
@ -81,6 +136,8 @@ module Msf
insert_text(Rex::Text.to_utf8(data)) insert_text(Rex::Text.to_utf8(data))
end end
@historic = History.new()
# Display all # Display all
self.show_all self.show_all
@ -114,18 +171,61 @@ module Msf
protected # protected #
########### ###########
#
# Get the current line
#
def current_line
# get the actual offset
start = @buffer.get_iter_at_offset(@@offset)
# get the command
line = @buffer.get_text(start, @buffer.end_iter)
return line
end
#
# Replace the current active line with another line
#
def replace(line)
# get the actual offset
start = @buffer.get_iter_at_offset(@@offset)
# Delete all
@buffer.delete(start, @buffer.end_iter)
# Save the new offset
@@offset = @buffer.end_iter.offset
# insert the old command
@buffer.insert(@buffer.end_iter, line)
end
# #
# Catch the text from the textview # Catch the text from the textview
# #
def catch_text def catch_text
# get the actual offset
start = @buffer.get_iter_at_offset(@@offset) start = @buffer.get_iter_at_offset(@@offset)
# get the command
cmd = @buffer.get_text(start, @buffer.end_iter) cmd = @buffer.get_text(start, @buffer.end_iter)
# Save the command to the history object
@historic.append(cmd)
# Write the command to our pipe
send_cmd(cmd) send_cmd(cmd)
# Add a return line to our buffer
insert_text("\n") insert_text("\n")
# Create the mark tag if not exist
if (not @buffer.get_mark('end_mark')) if (not @buffer.get_mark('end_mark'))
@buffer.create_mark('end_mark', @buffer.end_iter, false) @buffer.create_mark('end_mark', @buffer.end_iter, false)
end end
# Save our offset
@@offset = @buffer.end_iter.offset @@offset = @buffer.end_iter.offset
end end
@ -133,14 +233,16 @@ module Msf
# Insert the text into the buffer # Insert the text into the buffer
# #
def insert_text(text) def insert_text(text)
# get the actual offset # Create the mark tag if not exist
start = @buffer.get_iter_at_offset(@@offset)
@buffer.insert(@buffer.end_iter, text) @buffer.insert(@buffer.end_iter, text)
if (not @buffer.get_mark('end_mark')) if (not @buffer.get_mark('end_mark'))
@buffer.create_mark('end_mark', @buffer.end_iter, false) @buffer.create_mark('end_mark', @buffer.end_iter, false)
end end
# Save our offset
@@offset = @buffer.end_iter.offset @@offset = @buffer.end_iter.offset
# Scrolled the view until the end of the buffer
@textview.scroll_mark_onscreen(@buffer.get_mark('end_mark')) @textview.scroll_mark_onscreen(@buffer.get_mark('end_mark'))
end end
@ -150,11 +252,11 @@ module Msf
def on_key_pressed(event) def on_key_pressed(event)
# Enter key # Enter key
if event.keyval == Gdk::Keyval::GDK_Return if (event.keyval == Gdk::Keyval::GDK_Return)
catch_text() catch_text()
# Backspace key # Backspace key
elsif event.keyval == Gdk::Keyval::GDK_BackSpace elsif (event.keyval == Gdk::Keyval::GDK_BackSpace)
iter = @buffer.end_iter iter = @buffer.end_iter
if iter.offset == @@offset if iter.offset == @@offset
return true return true
@ -163,7 +265,7 @@ module Msf
end end
# Delete key # Delete key
elsif event.keyval == Gdk::Keyval::GDK_Delete elsif (event.keyval == Gdk::Keyval::GDK_Delete)
iter = @buffer.end_iter iter = @buffer.end_iter
if iter.offset == @@offset if iter.offset == @@offset
return true return true
@ -171,6 +273,17 @@ module Msf
return false return false
end end
# Previous command
elsif (event.keyval == Gdk::Keyval::GDK_Up)
cmd = @historic.prev(current_line())
replace(cmd)
return true
# Next command
elsif (event.keyval == Gdk::Keyval::GDK_Down)
cmd = @historic.next(current_line())
replace(cmd)
return true
end end
end end