Class: Rex::Post::Meterpreter::Ui::Console::CommandDispatcher::NetworkPug

Inherits:
Object
  • Object
show all
Includes:
Rex::Post::Meterpreter::Ui::Console::CommandDispatcher
Defined in:
lib/rex/post/meterpreter/ui/console/command_dispatcher/networkpug.rb

Overview

Rex::Ui::Text::IrbShell.new(binding).run

Constant Summary collapse

Klass =
Console::CommandDispatcher::NetworkPug
@@options =
Rex::Parser::Arguments.new(
  "-i" => [ true, "Interface on remote machine to listen on" ],
  "-f" => [ true, "Additional pcap filtering mechanism" ],
  "-v" => [ false, "Virtual NIC (packets only for your TAP dev locally)" ]
)

Instance Attribute Summary collapse

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

#shell, #tab_complete_items

Instance Method Summary collapse

Methods included from Rex::Post::Meterpreter::Ui::Console::CommandDispatcher

check_hash, #client, #log_error, #msf_loaded?, set_hash

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

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

Constructor Details

#initialize(shell) ⇒ NetworkPug

Returns a new instance of NetworkPug.



23
24
25
26
27
28
29
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/networkpug.rb', line 23

def initialize(shell)
  @thread_stuff = nil
  @tapdev = nil
  @channel = nil

  super
end

Instance Attribute Details

#channelObject

Returns the value of attribute channel.



33
34
35
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/networkpug.rb', line 33

def channel
  @channel
end

#tapdevObject

Returns the value of attribute tapdev.



32
33
34
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/networkpug.rb', line 32

def tapdev
  @tapdev
end

#thread_stuffObject

Returns the value of attribute thread_stuff.



31
32
33
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/networkpug.rb', line 31

def thread_stuff
  @thread_stuff
end

Instance Method Details

#cmd_networkpug_start(*args) ⇒ Object



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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/networkpug.rb', line 118

def cmd_networkpug_start(*args)
  # PKS - I suck at ruby ;\

  virtual_nic = false
  filter = nil
  interface = nil

  if(args.length == 0)
    args.unshift("-h")
  end

  @@options.parse(args) { |opt, idx, val|
    # print_line("before: #{opt} #{idx} #{val} || virtual nic: #{virtual_nic}, filter: #{filter}, interface: #{interface}")
    case opt
      when "-v"
        virtual_nic = true

      when "-f"
        filter = val

      when "-i"
        interface = val

      when "-h"
        print_error("Usage: networkpug_start -i interface [options]")
        print_error("")
        print_error(@@options.usage)
    end
    # print_line("after: #{opt} #{idx} #{val} || virtual nic: #{virtual_nic}, filter: #{filter}, interface: #{interface}")

  }

  if (interface == nil)
    print_error("Usage: networkpug_start -i interface [options]")
    print_error("")
    print_error(@@options.usage)
    return
  end

  @tapdev, tapname, mac = setup_tapdev

  if(@tapdev == nil)
    print_status("Failed to create tapdev")
    return
  end

  # PKS, we should implement multiple filter strings and let the
  # remote host build it properly.
  # not (our conn) and (virtual nic filter) and (custom filter)
  # print_line("before virtual, filter is #{filter}")

  if(filter == nil and virtual_nic == true)
    filter = "ether host #{mac}"
  elsif(filter != nil and virtual_nic == true)
    filter += " and ether host #{mac}"
    #print_line("Adjusted filter is #{filter}")
  end

  print_line("#{tapname} created with a hwaddr of #{mac}, ctrl-c when done")

  response, @channel = client.networkpug.networkpug_start(interface, filter)

  if(@channel)
    @thread_stuff = Rex::ThreadFactory.spawn("MeterpreterNetworkPUGReceiver", false) {
      proxy_packets()
    }

    print_line("Packet slinger for #{interface} has a thread structure of #{@thread_stuff}")
  end

  return true
end

#cmd_networkpug_stop(*args) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/networkpug.rb', line 191

def cmd_networkpug_stop(*args)
  interface = args[0]
  if (interface == nil)
    print_error("Usage: networkpug_stop [interface]")
    return
  end

  client.networkpug.networkpug_stop(interface)

  #print_line("client.networkpug.networkpug_stop returned")

  if(@thread_stuff)
    # print_line("killing thread")
    @thread_stuff.kill

    #print_line("joining thread")
    #@thread_stuff.join
    # meterpreter dies if i try to join.. not sure why.

    @thread_stuff = nil

    #print_line("closing tapdev")
    @tapdev.close

    #print_line("closing channel")
    #@channel.close
  end

  print_status("Packet slinging stopped on #{interface}")
  return true
end

#commandsObject

List of supported commands.



39
40
41
42
43
44
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/networkpug.rb', line 39

def commands
  {
    "networkpug_start" => "Start slinging packets between hosts",
    "networkpug_stop"  => "Stop slinging packets between hosts",
  }
end

#nameObject



223
224
225
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/networkpug.rb', line 223

def name
  "NetworkPug"
end

#proxy_packetsObject



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
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/networkpug.rb', line 77

def proxy_packets()
  while 1
    # Ghetto :\

    sd = Rex::ThreadSafe.select([ @channel.lsock, @tapdev ], nil, nil)

    sd[0].each { |s|
      if(s == @channel.lsock)	# Packet from remote host to local TAP dev
        len = @channel.lsock.read(2)
        len = len.unpack('n')[0]

        #print_line("Got #{len} bytes from remote host's network")

        if(len > 1514 or len == 0)
          @tapdev.close()
          print_line("length is invalid .. #{len} ?, de-synchronized ? ")
        end

        packet = @channel.lsock.read(len)

        print_line("packet from remote host:\n" + Rex::Text.hexify(packet))

        @tapdev.syswrite(packet)

      elsif(s == @tapdev)
        # Packet from tapdev to remote host network

        packet = @tapdev.sysread(1514)

        print_line("packet to remote host:\n" + Rex::Text.hexify(packet))

        @channel.write(packet)
      end
    } if(sd)

    if(not sd)
      print_line("hmmm. ")
    end
  end
end

#setup_tapdevObject



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
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/networkpug.rb', line 46

def setup_tapdev
  # XXX, look at how to use windows equivilient and include

  tapdev = ::File.open("/dev/net/tun", "wb+")

  0.upto(16) { |idx|
    name = "npug#{idx}"

    ifreq = [ name, 0x1000 | 0x02, "" ].pack("a16va14")

    begin
      tapdev.ioctl(0x400454ca, ifreq)		# is there a better way than hex constant
    rescue Errno::EBUSY
      next
    end

    ifreq = [ name ].pack("a32")

    tapdev.ioctl(0x8927, ifreq)

    # print_line(Rex::Text.hexify(ifreq))

    mac = sprintf("%02x:%02x:%02x:%02x:%02x:%02x", ifreq[18], ifreq[19], ifreq[20], ifreq[21], ifreq[22], ifreq[23])

    return tapdev, name, mac
  }

  tapdev.close()
  return nil, nil, nil
end