rbkb 0.6.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/History.txt +74 -0
  2. data/README.rdoc +149 -0
  3. data/Rakefile +47 -0
  4. data/bin/b64 +5 -0
  5. data/bin/bgrep +5 -0
  6. data/bin/blit +5 -0
  7. data/bin/c +5 -0
  8. data/bin/crc32 +5 -0
  9. data/bin/d64 +5 -0
  10. data/bin/dedump +5 -0
  11. data/bin/feed +5 -0
  12. data/bin/hexify +5 -0
  13. data/bin/len +5 -0
  14. data/bin/plugsrv +271 -0
  15. data/bin/rex +10 -0
  16. data/bin/rstrings +5 -0
  17. data/bin/slice +5 -0
  18. data/bin/telson +5 -0
  19. data/bin/unhexify +5 -0
  20. data/bin/urldec +5 -0
  21. data/bin/urlenc +5 -0
  22. data/bin/xor +5 -0
  23. data/cli_usage.rdoc +285 -0
  24. data/doctor-bag.jpg +0 -0
  25. data/lib/rbkb.rb +51 -0
  26. data/lib/rbkb/cli.rb +219 -0
  27. data/lib/rbkb/cli/b64.rb +35 -0
  28. data/lib/rbkb/cli/bgrep.rb +86 -0
  29. data/lib/rbkb/cli/blit.rb +89 -0
  30. data/lib/rbkb/cli/chars.rb +24 -0
  31. data/lib/rbkb/cli/crc32.rb +35 -0
  32. data/lib/rbkb/cli/d64.rb +28 -0
  33. data/lib/rbkb/cli/dedump.rb +52 -0
  34. data/lib/rbkb/cli/feed.rb +229 -0
  35. data/lib/rbkb/cli/hexify.rb +65 -0
  36. data/lib/rbkb/cli/len.rb +76 -0
  37. data/lib/rbkb/cli/rstrings.rb +108 -0
  38. data/lib/rbkb/cli/slice.rb +47 -0
  39. data/lib/rbkb/cli/telson.rb +87 -0
  40. data/lib/rbkb/cli/unhexify.rb +50 -0
  41. data/lib/rbkb/cli/urldec.rb +35 -0
  42. data/lib/rbkb/cli/urlenc.rb +35 -0
  43. data/lib/rbkb/cli/xor.rb +43 -0
  44. data/lib/rbkb/extends.rb +725 -0
  45. data/lib/rbkb/http.rb +21 -0
  46. data/lib/rbkb/http/base.rb +172 -0
  47. data/lib/rbkb/http/body.rb +214 -0
  48. data/lib/rbkb/http/common.rb +74 -0
  49. data/lib/rbkb/http/headers.rb +370 -0
  50. data/lib/rbkb/http/parameters.rb +104 -0
  51. data/lib/rbkb/http/request.rb +58 -0
  52. data/lib/rbkb/http/response.rb +86 -0
  53. data/lib/rbkb/plug.rb +9 -0
  54. data/lib/rbkb/plug/blit.rb +222 -0
  55. data/lib/rbkb/plug/cli.rb +83 -0
  56. data/lib/rbkb/plug/feed_import.rb +74 -0
  57. data/lib/rbkb/plug/peer.rb +67 -0
  58. data/lib/rbkb/plug/plug.rb +215 -0
  59. data/lib/rbkb/plug/proxy.rb +26 -0
  60. data/lib/rbkb/plug/unix_domain.rb +75 -0
  61. data/lib_usage.rdoc +176 -0
  62. data/rbkb.gemspec +38 -0
  63. data/spec/rbkb_spec.rb +7 -0
  64. data/spec/spec_helper.rb +16 -0
  65. data/tasks/ann.rake +80 -0
  66. data/tasks/bones.rake +20 -0
  67. data/tasks/gem.rake +201 -0
  68. data/tasks/git.rake +40 -0
  69. data/tasks/notes.rake +27 -0
  70. data/tasks/post_load.rake +34 -0
  71. data/tasks/rdoc.rake +51 -0
  72. data/tasks/rubyforge.rake +55 -0
  73. data/tasks/setup.rb +292 -0
  74. data/tasks/spec.rake +54 -0
  75. data/tasks/svn.rake +47 -0
  76. data/tasks/test.rake +40 -0
  77. data/test/test_cli_b64.rb +35 -0
  78. data/test/test_cli_bgrep.rb +137 -0
  79. data/test/test_cli_blit.rb +11 -0
  80. data/test/test_cli_chars.rb +21 -0
  81. data/test/test_cli_crc32.rb +108 -0
  82. data/test/test_cli_d64.rb +22 -0
  83. data/test/test_cli_dedump.rb +118 -0
  84. data/test/test_cli_feed.rb +11 -0
  85. data/test/test_cli_helper.rb +96 -0
  86. data/test/test_cli_hexify.rb +63 -0
  87. data/test/test_cli_len.rb +96 -0
  88. data/test/test_cli_rstrings.rb +15 -0
  89. data/test/test_cli_slice.rb +73 -0
  90. data/test/test_cli_telson.rb +11 -0
  91. data/test/test_cli_unhexify.rb +43 -0
  92. data/test/test_cli_urldec.rb +50 -0
  93. data/test/test_cli_urlenc.rb +44 -0
  94. data/test/test_cli_xor.rb +71 -0
  95. data/test/test_helper.rb +5 -0
  96. data/test/test_http.rb +27 -0
  97. data/test/test_http_helper.rb +60 -0
  98. data/test/test_http_request.rb +136 -0
  99. data/test/test_http_response.rb +222 -0
  100. data/test/test_rbkb.rb +19 -0
  101. metadata +238 -0
data/History.txt ADDED
@@ -0,0 +1,74 @@
1
+ == 0.6.10 / 2009-09-30
2
+ * added -b/--blitsrv argument to plugsrv
3
+ * added --target-tls/--server-tls arguments to plugsrv for TLS support
4
+
5
+ == 0.6.9.1 / 2009-09-08
6
+ * fixed a typo in random_string()
7
+
8
+ == 0.6.9 / 2009-09-01
9
+ * Enhancements
10
+ * telson and feed now support -s/--source address:port using TCP with
11
+ bind_connect via eventmachine
12
+ * random_string and random_alphanum helper methods added
13
+ * Bug-Fix
14
+ * Plug's PeerList.find_peers was returning nil resulting in problems for
15
+ blit -l amongst other things.
16
+
17
+ == 0.6.8.1 / 2009-06-12
18
+ * Enhancements
19
+ * Added String.rotate_bytes per request for a rotation cypher by
20
+ wardwouts@github. I pilfered this implementation mostly from Timur Duehr.
21
+
22
+ == 0.6.8 / 2009-06-04
23
+ * Enhancements
24
+ * 0.6.8 is geared towards beginning to make rbkb compatible with ruby 1.9.x
25
+ (tested on 1.9.1). Had to sacrifice the 'uuid' helper method
26
+ since something happened to SHA1 in ruby 1.9. It may make a comeback if
27
+ anybody yells about this.
28
+ * Known Issues:
29
+ * There are problems with binary releases of eventmachine with 1.9.1 on win32
30
+ this does not appear to be caused by rbkb, however we're keeping an eye on
31
+ it. For now, this is causing the plug tools to crash and test cases to
32
+ fail on 1.9.1 win32.
33
+
34
+ == 0.6.7
35
+ * Enhancements
36
+ * Http::Parameters was given the same treatement as headers got in 0.6.6.
37
+ Technically params are supposed to be unique, but we try to stay flexible
38
+ in Rbkb.
39
+ * Bug-fix:
40
+ * Fixed a silly typing bug in Http::Parameters.set_param introduced
41
+ around 0.6.6
42
+
43
+ == 0.6.6
44
+ * Bug-fix:
45
+ * Http library improperly made headers unique by name. The library has
46
+ been changed so that headers no longer tries to pretend to be a hash.
47
+
48
+ == 0.6.5.2
49
+ * Enhancements
50
+ * d64 and b64 now support the -f option for input files as rdoc indicates
51
+ Thanks to Cory Scott for catching this doc error and for his patch.
52
+
53
+ == 0.6.5.1
54
+ * Minor bug-fix
55
+ * fixed a problem with the gem, version bump is for github visibility
56
+
57
+ == 0.6.5 / 2009-03-24
58
+
59
+ * Enhancements
60
+ * Added a http protocol library (Rbkb::Http)
61
+ * Added TLS support to telson (-S/--start-tls in cli)
62
+ * Added raw dump output to telson (-d raw/hex in cli : hex is default)
63
+
64
+ == 0.6.4 / 2009-03-20
65
+
66
+ * Enhancements
67
+ * Added unit tests for all cli utilities. Most are fairly complete.
68
+
69
+ == 0.6.3 / 2009-03-10
70
+
71
+ * Enhancements
72
+ * Started using bones to help manage this mess. History.txt is born!
73
+ * begun adding test cases for cli utils
74
+ * crc32 takes an optional filename argument as first arg
data/README.rdoc ADDED
@@ -0,0 +1,149 @@
1
+ = rbkb
2
+
3
+ * http://emonti.github.com/rbkb
4
+
5
+ == DESCRIPTION:
6
+
7
+ Ruby BlackBag (rbkb)
8
+
9
+ A miscellaneous collection of command-line tools and ruby library helpers
10
+ related to pen-testing and reversing.
11
+
12
+ === Rationale
13
+
14
+ Disclaimer:
15
+ Most of what's in the black bag came from a desire to do less typing.
16
+ But there might be a few clever things that were added by accident.
17
+
18
+ rbkb is inspired by Matasano BlackBag (a set of similar tools written in C).
19
+
20
+ See:
21
+ * http://www.matasano.com/log/1048/blackbag-091-new-link-and-minor-fixes/
22
+ * http://www.matasano.com/log/552/code-release-blackbag-09-binary-protocol-reversing-unix-thingies/
23
+
24
+ Things go into the black bag as they are stolen (as a compliment!) or dreamed
25
+ up, usually for simplifying some repetetive task or a desire for a new tool.
26
+
27
+ Along the way, some of tools in the blackbag spirit make their way into 'rbkb'
28
+ that may or may not make it to 'bkb' right away (if ever). Similarly some of
29
+ the things in 'bkb' have not yet made it to 'rbkb' (and may not).
30
+
31
+
32
+ == SYNOPSIS:
33
+
34
+ === Command Line Tools
35
+
36
+ The tools almost all support '-h', but I'll admit this only goes so far.
37
+ See cli_usage.rdoc for usage and a bit of extra info on the various tools.
38
+
39
+ When I get some spare time, I'll try and do up some examples of using all
40
+ the tools.
41
+
42
+
43
+ === Plug
44
+
45
+ Black Bag includes several tools for testing network protocols using plugboard
46
+ proxies. Users of the original Matasano BlackBag may be familiar with the
47
+ commands 'bkb replug', 'bkb telson', and 'bkb blit'.
48
+
49
+ Ruby BlackBag has a similar set of network tools:
50
+
51
+ * 'blit' : Uses a simple homegrown OOB IPC mechanism (local socket) to
52
+ communicate with 'blit-capable' tools like telson and plugsrv and send
53
+ data to network endpoints through them. Use 'blit' to send raw
54
+ messages to servers or clients then watch how they respond (see below).
55
+
56
+ * 'telson' : Similar to 'bkb telson'. Opens a TCP or UDP client connection
57
+ which is little more than a receiver for 'blit' messages. Use this to
58
+ pretend to be a client and send raw messages to some service while observing
59
+ raw replies.
60
+
61
+ * 'plugsrv' : Similar to 'bkb replug'. Sits as a reverse TCP proxy between
62
+ one or more clients and a server. Accepts 'blit' messages which can be
63
+ directed at client or server ends of a conversation. The original 'replug'
64
+ didn't do this, which makes plugsrv kindof neat.
65
+
66
+
67
+ === Monkey Patches
68
+
69
+ Much of rbkb is implemented as a bunch of monkeypatches to Array, String,
70
+ Numeric and other base classes. If this suits your fancy (some people despise
71
+ monkeypatches, this is not their fancy) then you can 'require "rbkb"' from
72
+ your irb sessions and own scripts. See 'lib_usage.rdoc' for more info.
73
+
74
+
75
+ == REQUIREMENTS:
76
+
77
+ * eventmachine >= 0.12.8
78
+
79
+
80
+ == INSTALL:
81
+
82
+ === Gem Installation
83
+
84
+ rbkb is available as a gem from github:
85
+
86
+ gem sources -a http://gems.github.com #(you only have to do this once)
87
+ gem install emonti-rbkb
88
+
89
+
90
+ ==== Gem Install Note
91
+
92
+ Installing the gem as root may be risky depending on your rubygems
93
+ configuration so I don't really recommend using 'sudo gem install'.
94
+ Worst case scenario I know of is I blew away my OSX-shipped '/usr/bin/crc32'
95
+ this way. It was written in perl, so I considered this providence and didn't
96
+ look back. But you may feel differently about 'rubygems' clobbering a file in
97
+ /usr/bin.
98
+
99
+ When installing as a regular user, however, rubygems may stick rbkb's
100
+ executable bin/* files somewhere unexpected. To find out where these are and
101
+ either add them to your PATH or copy/symlink them somewhere else like
102
+ /usr/local/bin/ do this:
103
+
104
+ gem contents emonti-rbkb
105
+
106
+
107
+ === Manual installation:
108
+
109
+ git clone git://github.com/emonti/rbkb.git
110
+ cd rbkb
111
+ rake gem:install
112
+
113
+
114
+ or ... you can also install manually without rubygems.
115
+
116
+ You can access the rbkb project at github. You'll want git installed:
117
+
118
+ cp -r rbkb/lib/* /usr/lib/ruby/1.8/site_ruby/1.8 # or another ruby libdir
119
+ cp bin/* ~/bin # or wherever else in your PATH
120
+
121
+ Run this to generate docs with rdoc the same way the gem would have:
122
+
123
+ rake doc:rdoc
124
+
125
+ == LICENSE:
126
+
127
+ (The MIT License)
128
+
129
+ Copyright (c) 2009 Eric Monti, Matasano Security
130
+
131
+ Permission is hereby granted, free of charge, to any person obtaining
132
+ a copy of this software and associated documentation files (the
133
+ 'Software'), to deal in the Software without restriction, including
134
+ without limitation the rights to use, copy, modify, merge, publish,
135
+ distribute, sublicense, and/or sell copies of the Software, and to
136
+ permit persons to whom the Software is furnished to do so, subject to
137
+ the following conditions:
138
+
139
+ The above copyright notice and this permission notice shall be
140
+ included in all copies or substantial portions of the Software.
141
+
142
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
143
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
144
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
145
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
146
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
147
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
148
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
149
+
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ # Look in the tasks/setup.rb file for the various options that can be
2
+ # configured in this Rakefile. The .rake files in the tasks directory
3
+ # are where the options are used.
4
+
5
+ begin
6
+ require 'bones'
7
+ Bones.setup
8
+ rescue LoadError
9
+ begin
10
+ load 'tasks/setup.rb'
11
+ rescue LoadError
12
+ raise RuntimeError, '### please install the "bones" gem ###'
13
+ end
14
+ end
15
+
16
+ ensure_in_path 'lib'
17
+ require 'rbkb'
18
+
19
+ task :default => 'test:run'
20
+
21
+ PROJ.name = 'rbkb'
22
+ PROJ.authors = 'Eric Monti'
23
+ PROJ.email = 'emonti@matasano.com'
24
+ PROJ.description = 'Rbkb is a collection of ruby-based pen-testing and reversing tools. Inspired by Matasano Blackbag.'
25
+ PROJ.url = 'http://emonti.github.com/rbkb'
26
+ PROJ.version = Rbkb::VERSION
27
+ PROJ.rubyforge.name = 'rbkb'
28
+ PROJ.readme_file = 'README.rdoc'
29
+
30
+ PROJ.spec.opts << '--color'
31
+
32
+ PROJ.rdoc.opts << '--line-numbers'
33
+
34
+ #PROJ.rdoc.opts << '--diagram'
35
+ PROJ.notes.tags << "X"+"XX" # muhah! so we don't note our-self
36
+
37
+ # exclude rcov.rb and external libs from rcov report
38
+ PROJ.rcov.opts += [
39
+ "--exclude", "rcov.rb",
40
+ "--exclude", "eventmachine",
41
+ "--exclude", "pcap_misc.rb",
42
+ "--exclude", "pcaplet.rb"
43
+ ]
44
+
45
+ depend_on 'eventmachine', '>= 0.12.8'
46
+
47
+ # EOF
data/bin/b64 ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rbkb/cli/b64"
4
+
5
+ Rbkb::Cli::B64.run()
data/bin/bgrep ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rbkb/cli/bgrep"
4
+
5
+ Rbkb::Cli::Bgrep.run()
data/bin/blit ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rbkb/cli/blit"
4
+
5
+ Rbkb::Cli::Blit.run()
data/bin/c ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rbkb/cli/chars"
4
+
5
+ Rbkb::Cli::Chars.run()
data/bin/crc32 ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rbkb/cli/crc32"
4
+
5
+ Rbkb::Cli::Crc32.run()
data/bin/d64 ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rbkb/cli/d64"
4
+
5
+ Rbkb::Cli::D64.run()
data/bin/dedump ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rbkb/cli/dedump"
4
+
5
+ Rbkb::Cli::Dedump.run()
data/bin/feed ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rbkb/cli/feed"
4
+
5
+ Rbkb::Cli::Feed.run()
data/bin/hexify ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rbkb/cli/hexify"
4
+
5
+ Rbkb::Cli::Hexify.run()
data/bin/len ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rbkb/cli/len"
4
+
5
+ Rbkb::Cli::Len.run()
data/bin/plugsrv ADDED
@@ -0,0 +1,271 @@
1
+ #!/usr/bin/env ruby
2
+ # Copyright 2009 emonti at matasano.com
3
+ # See README.rdoc for license information
4
+ #
5
+ # A blit-able reverse TCP proxy. Displays traffic hexdumps. Currently uses
6
+ # the default blit port for its blit receiver.
7
+ #
8
+ # XXX TODO - refactor me!
9
+
10
+ begin
11
+ require 'rubygems'
12
+ rescue LoadError
13
+ end
14
+ require 'eventmachine'
15
+ require 'socket'
16
+ require 'optparse'
17
+ require 'rbkb/plug'
18
+
19
+ class BlitPlug
20
+ module UI
21
+ def log( *msg )
22
+ unless PLUG_OPTS[:quiet]
23
+ PLUG_OPTS[:out].puts msg
24
+ end
25
+ end
26
+ module_function :log
27
+ end
28
+
29
+ class Controller
30
+ attr_accessor :tgtaddr, :tgtport, :tgtclient, :blit, :peers
31
+ @@controller = nil
32
+
33
+ def initialize(tgtaddr, tgtport, tgtclient)
34
+ @tgtaddr = tgtaddr
35
+ @tgtport = tgtport
36
+ @tgtclient = tgtclient
37
+
38
+ @@controller = self
39
+
40
+ @peers = Array.new
41
+
42
+ ## Just tack on a blit server???
43
+ @blit = EventMachine::start_server(
44
+ PLUG_OPTS[:blit_addr], PLUG_OPTS[:blit_port], Plug::Blit, :TCP, self
45
+ )
46
+
47
+ end
48
+
49
+ ##----------------------------------------
50
+
51
+ def dispatch_rcv(snder, data)
52
+ data # for now
53
+ end
54
+
55
+ ##----------------------------------------
56
+
57
+ def dispatch_close(snder)
58
+ nil # for now
59
+ end
60
+
61
+ ##----------------------------------------
62
+
63
+ def self.proxy(cli)
64
+ unless (ctrl = @@controller)
65
+ raise "No controller exists for this connection: #{cli.sock_peername}"
66
+ end
67
+
68
+ tgtaddr = ctrl.tgtaddr
69
+ tgtport = ctrl.tgtport
70
+ tgtclient = ctrl.tgtclient
71
+
72
+ srv = EventMachine::connect(tgtaddr, tgtport, tgtclient)
73
+ srv.plug_peers.push cli
74
+ cli.plug_peers.push srv
75
+
76
+ ctrl.peers.push srv
77
+ ctrl.peers.push cli ### I suppose this is probably useful too..
78
+
79
+ srv.controller = cli.controller = ctrl
80
+ end
81
+ end # class BlitPlug::Controller
82
+
83
+
84
+ module BaseTCP
85
+ include UI
86
+
87
+ attr_accessor :plug_peers, :controller, :kind
88
+ attr_reader :sock_peer, :sock_peername
89
+
90
+ def post_init
91
+ @plug_peers = Array.new
92
+ @kind = :conn # default
93
+ end
94
+
95
+ def name
96
+ @name
97
+ end
98
+
99
+ def say(data, sender)
100
+ log "%#{sender.kind.to_s.upcase}-SAYS", data.hexdump(:out => StringIO.new), "%"
101
+ send_data data
102
+ end
103
+
104
+ def receive_data data
105
+ log "%#{kind.to_s.upcase}-#{sock_peername}-SAYS", data.hexdump, "%"
106
+ if @controller and (data = @controller.dispatch_rcv(self, data)).nil?
107
+ return
108
+ end
109
+ @plug_peers.each {|p| p.send_data data}
110
+ end
111
+
112
+
113
+ def notify_connection
114
+ @name = "#{kind.to_s.upcase}-#{sock_peername}"
115
+ log "%#{@name}-CONNECTED"
116
+ end
117
+
118
+
119
+ def unbind
120
+ @name = "#{kind.to_s.upcase}-#{sock_peername}"
121
+ log "%#{@name}-CLOSED"
122
+
123
+ cret = (@controller and @controller.dispatch_close(self))
124
+
125
+ @plug_peers.each do |p|
126
+ p.plug_peers.delete(self)
127
+ p.close_connection unless cret
128
+ end
129
+ end
130
+ end
131
+
132
+
133
+ module TCPListener
134
+ include BlitPlug::BaseTCP
135
+ attr_accessor :tgtaddr, :tgtport
136
+
137
+ def post_init
138
+ super
139
+ @kind = :client
140
+ @sock_peer = Socket.unpack_sockaddr_in(get_peername).reverse
141
+ @sock_peername = @sock_peer.join(':')
142
+
143
+ @controller = BlitPlug::Controller.proxy(self)
144
+
145
+ start_tls if PLUG_OPTS[:svr_tls]
146
+
147
+ notify_connection
148
+ end
149
+
150
+ end # module TCPListener
151
+
152
+
153
+ module TCPClient
154
+ include BlitPlug::BaseTCP
155
+ attr_accessor :connected
156
+
157
+ def post_init
158
+ super
159
+ @kind = :server
160
+ end
161
+
162
+ def connection_completed
163
+ @sock_peer = Socket.unpack_sockaddr_in(get_peername).reverse
164
+ @sock_peername = @sock_peer.join(':')
165
+ notify_connection
166
+ start_tls if PLUG_OPTS[:tgt_tls]
167
+ end
168
+
169
+ end # module TCPClient
170
+
171
+ end # module BlitPlug
172
+
173
+ PLUG_OPTS={
174
+ :quiet => false,
175
+ :out => STDOUT,
176
+ :blit_addr => Plug::Blit::DEFAULT_IPADDR,
177
+ :blit_port => Plug::Blit::DEFAULT_PORT,
178
+ }
179
+
180
+ def bail(*msg)
181
+ STDERR.puts msg
182
+ exit 1
183
+ end
184
+
185
+
186
+
187
+ #############################################################################
188
+ ### MAIN
189
+ #############################################################################
190
+ #
191
+ # Get option arguments
192
+ opts = OptionParser.new do |opts|
193
+ opts.banner = "Usage: #{File.basename $0} [options] target:tport[@[laddr:]lport]\n",
194
+ " <target:tport> = the address of the target service\n",
195
+ " <@laddr:lport> = optional address and port to listen on\n"
196
+
197
+ opts.separator ""
198
+ opts.separator "Options:"
199
+
200
+ opts.on_tail("-h", "--help", "Show this message") do
201
+ puts opts
202
+ exit 1
203
+ end
204
+
205
+ opts.on("-o", "--output FILE", "send output to a file") do |o|
206
+ PLUG_OPTS[:out] = File.open(o, "w") rescue (bail $!)
207
+ end
208
+
209
+ opts.on("-l", "--listen ADDR:PORT",
210
+ "optional listener address:port",
211
+ "(default: 0.0.0.0:<tport>)"
212
+ ) do |addr|
213
+
214
+ unless m = /^([\w\.]+)?(?::(\d+))?$/.match(addr)
215
+ STDERR.puts "invalid listener address"
216
+ exit 1
217
+ end
218
+ PLUG_OPTS[:svraddr] = m[1]
219
+ PLUG_OPTS[:svrport] = (m[2])? m[2].to_i : nil
220
+ end
221
+
222
+ opts.on("-q", "--[no-]quiet", "Suppress/Enable conversation dumps.") do |q|
223
+ PLUG_OPTS[:quiet] = q
224
+ end
225
+
226
+ opts.on("-b", "--blitsrv ADDR:PORT",
227
+ "specify blit listener [address:]port",
228
+ "(default: #{PLUG_OPTS[:blit_addr]}:#{PLUG_OPTS[:blit_port]})"
229
+ ) do |addr|
230
+
231
+ unless m = /^(?:([\w\.]+):)?(\d+)$/.match(addr)
232
+ STDERR.puts "invalid blit listener argument"
233
+ exit 1
234
+ end
235
+ PLUG_OPTS[:blit_addr] = m[1] if m[1]
236
+ PLUG_OPTS[:blit_port] = m[2].to_i
237
+ end
238
+
239
+ opts.on("--[no-]target-tls", "enable/disable TLS to target") {|t| PLUG_OPTS[:tgt_tls] = t }
240
+ opts.on("--[no-]server-tls", "enable/disable TLS to clients") {|t| PLUG_OPTS[:svr_tls] = t }
241
+
242
+ end
243
+
244
+ opts.parse!(ARGV) rescue (STDERR.puts $!; exit 1)
245
+
246
+
247
+ # Get target/listen argument
248
+ rx = /^([\w\.]+):(\d+)(?:@(?:([\w\.]+):)?(\d+))?$/
249
+ unless (m = rx.match(ARGV.shift)) and ARGV.shift.nil?
250
+ $stderr.puts opts.banner
251
+ exit 1
252
+ end
253
+
254
+ PLUG_OPTS[:tgtaddr] = m[1]
255
+ PLUG_OPTS[:tgtport] = m[2].to_i
256
+ PLUG_OPTS[:svraddr] ||= (m[3] || "0.0.0.0")
257
+ PLUG_OPTS[:svrport] ||= (m[4] || PLUG_OPTS[:tgtport]).to_i
258
+
259
+
260
+ EventMachine::run {
261
+ # Instantiate controller
262
+ ctrl = BlitPlug::Controller.new(PLUG_OPTS[:tgtaddr], PLUG_OPTS[:tgtport], BlitPlug::TCPClient)
263
+
264
+ # Start event loop
265
+ BlitPlug::UI.log "%Starting TCP PlugServer #{PLUG_OPTS[:svraddr]}:#{PLUG_OPTS[:svrport]} -> #{PLUG_OPTS[:tgtaddr]}:#{PLUG_OPTS[:tgtport]}"
266
+
267
+
268
+ EventMachine::start_server(PLUG_OPTS[:svraddr], PLUG_OPTS[:svrport], BlitPlug::TCPListener)
269
+ }
270
+
271
+