<commit_before>##
# ui_session_command.rb
# By <NAME>
# Created May, 2014
#
# See LICENSE.txt
##

require 'command_packet_stream'
require 'command_packet'
require 'parser'
require 'ui_handler'
require 'ui_interface_with_id'

class UiSessionCommand < UiInterfaceWithId
  attr_reader :session
  attr_reader :id

  include Parser
  include UiHandler

  def kill_me()
    puts()
    puts("Are you sure you want to kill this session? [Y/n]")
    if($stdin.gets[0].downcase == 'n')
      puts("You might want to use ^z or the 'suspend' command")
    else
      @ui.kill_session(@id)
      @ui.detach_session()
    end
  end

  def register_commands()
    register_alias('q',       'quit')
    register_alias('exit',    'quit')
    register_alias('run',     'exec')
    register_alias('execute', 'exec')
    register_alias('up',      'suspend')
    register_alias('back',    'suspend')
    register_alias('pause',   'suspend')

    register_command("quit",
      Trollop::Parser.new do
        banner("Closes and kills this command session")
      end,

      Proc.new do |opts|
        kill_me()
      end,
    )

    register_command("help",
      Trollop::Parser.new do
        banner("Shows a help menu")
      end,

      Proc.new do |opts|
        puts()
        puts("Available session commands:")
        @commands.keys.sort.each do |name|
          # Don't display the empty command
          if(name != "")
            puts("- #{name}")
          end
        end

        puts("For more information, --help can be passed to any command")
      end,
    )

    register_command("clear",
      Trollop::Parser.new do
        banner("Clears the display")
      end,
      Proc.new do |opts|
        0.upto(100) do puts() end
      end,
    )

    register_command("ping",
      Trollop::Parser.new do
        banner("Sends a 'ping' to the remote host to make sure it's still alive)")
        opt :length, "length", :type => :integer, :required => false, :default => 256
      end,
      Proc.new do |opts|
        data = "A" * opts[:length]
        id = request_id()
        @pings[id] = data
        packet = CommandPacket.create_ping_request(id, data)
        @session.queue_outgoing(packet)
        puts("Ping request 0x%x sent! (0x%x bytes)" % [id, opts[:length]])
      end,
    )

    register_command("ping2",
      Trollop::Parser.new do
        banner("Sends a 'ping' to the remote host to make sure it's still alive)")
        opt :length, "length", :type => :integer, :required => false, :default => 256
      end,
      Proc.new do |opts|
        data = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" * 8
        data2 = "abcdefghijklmnopqrstuvwxyz" * 8

        id = request_id()
        @pings[id]   = data
        @pings[id+1] = data2

        packet = CommandPacket.create_ping_request(id, data)
        packet += CommandPacket.create_ping_request(id+1, data2)

        @session.queue_outgoing(packet)

        puts("Ping request 0x%x sent! (0x%x bytes)" % [id, opts[:length]])
      end,
    )

    register_command("shell",
      Trollop::Parser.new do
        banner("Spawn a shell on the remote host")
        opt :name, "Name", :type => :string, :required => false, :default => nil
      end,

      Proc.new do |opts|<commit_msg>Removed the test 'ping2' command I wrote<commit_after>##
# ui_session_command.rb
# By <NAME>
# Created May, 2014
#
# See LICENSE.txt
##

require 'command_packet_stream'
require 'command_packet'
require 'parser'
require 'ui_handler'
require 'ui_interface_with_id'

class UiSessionCommand < UiInterfaceWithId
  attr_reader :session
  attr_reader :id

  include Parser
  include UiHandler

  def kill_me()
    puts()
    puts("Are you sure you want to kill this session? [Y/n]")
    if($stdin.gets[0].downcase == 'n')
      puts("You might want to use ^z or the 'suspend' command")
    else
      @ui.kill_session(@id)
      @ui.detach_session()
    end
  end

  def register_commands()
    register_alias('q',       'quit')
    register_alias('exit',    'quit')
    register_alias('run',     'exec')
    register_alias('execute', 'exec')
    register_alias('up',      'suspend')
    register_alias('back',    'suspend')
    register_alias('pause',   'suspend')

    register_command("quit",
      Trollop::Parser.new do
        banner("Closes and kills this command session")
      end,

      Proc.new do |opts|
        kill_me()
      end,
    )

    register_command("help",
      Trollop::Parser.new do
        banner("Shows a help menu")
      end,

      Proc.new do |opts|
        puts()
        puts("Available session commands:")
        @commands.keys.sort.each do |name|
          # Don't display the empty command
          if(name != "")
            puts("- #{name}")
          end
        end

        puts("For more information, --help can be passed to any command")
      end,
    )

    register_command("clear",
      Trollop::Parser.new do
        banner("Clears the display")
      end,
      Proc.new do |opts|
        0.upto(100) do puts() end
      end,
    )

    register_command("ping",
      Trollop::Parser.new do
        banner("Sends a 'ping' to the remote host to make sure it's still alive)")
        opt :length, "length", :type => :integer, :required => false, :default => 256
      end,
      Proc.new do |opts|
        data = "A" * opts[:length]
        id = request_id()
        @pings[id] = data
        packet = CommandPacket.create_ping_request(id, data)
        @session.queue_outgoing(packet)
        puts("Ping request 0x%x sent! (0x%x bytes)" % [id, opts[:length]])
      end,
    )

    register_command("shell",
      Trollop::Parser.new do
        banner("Spawn a shell on the remote host")
        opt :name, "Name", :type => :string, :required => false, :default => nil
      end,

      Proc.new do |opts|
{'fraction_non_alphanumeric': 0.12754925682682336, 'fraction_numerical': 0.007258900795022468, 'mean_word_length': 2.4323843416370106, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1527034', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1852, 'n_words': 496}