emonti-rbkb 0.6.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/hexify ADDED
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/env ruby
2
+ # Author Eric Monti (emonti at matasano)
3
+ #
4
+ # hexify converts a string or raw data to hex characters. Input can be
5
+ # supplied via stdin, a string argument, or a file (with -f).
6
+ #
7
+ # Usage: hexify [options] <data | blank for stdin>
8
+ # -h, --help Show this message
9
+ # -v, --version Show version and exit
10
+ # -f, --file FILENAME Input from FILENAME
11
+ # -l, --length LEN Hexify in lines of LEN bytes
12
+ # -d, --delim=DELIMITER DELIMITER between each byte
13
+ # -p, --prefix=PREFIX PREFIX before each byte
14
+ # -s, --suffix=SUFFIX SUFFIX after each byte
15
+ #
16
+
17
+ require 'rbkb'
18
+ require 'rbkb/command_line'
19
+
20
+ include RBkB::CommandLine
21
+
22
+ #------------------------------------------------------------------------------
23
+ # Init options and arg parsing
24
+ OPTS = {}
25
+ arg = bkb_stdargs(nil, OPTS)
26
+ arg = bkb_inputargs(arg, OPTS)
27
+
28
+ arg.banner += " <data | blank for stdin>"
29
+
30
+ #------------------------------------------------------------------------------
31
+ # Add local options
32
+
33
+ arg.on("-l", "--length LEN", Numeric, "Hexify in lines of LEN bytes") do |l|
34
+ bail("Length must be greater than zero") unless (OPTS[:len] = l) > 0
35
+ end
36
+
37
+ arg.on("-d", "--delim=DELIMITER", "DELIMITER between each byte") do |d|
38
+ OPTS[:delim] = d
39
+ end
40
+
41
+ arg.on("-p", "--prefix=PREFIX", "PREFIX before each byte") do |p|
42
+ OPTS[:prefix] = p
43
+ end
44
+
45
+ arg.on("-s", "--suffix=SUFFIX", "SUFFIX after each byte") do |s|
46
+ OPTS[:suffix] = s
47
+ end
48
+
49
+
50
+ #------------------------------------------------------------------------------
51
+ # Parse arguments
52
+ arg.parse!(ARGV) rescue bail "Error: #{$!}\nUse -h|--help for more info."
53
+
54
+ # original-style spacing compatability
55
+ if ARGV[0] == "+" and OPTS[:delim].nil?
56
+ OPTS[:delim]=" "
57
+ ARGV.shift
58
+ end
59
+
60
+ # default string arg
61
+ if OPTS[:indat].nil? and a=ARGV.shift
62
+ OPTS[:indat] = a.dup
63
+ end
64
+
65
+ # catchall
66
+ if ARGV.length != 0
67
+ bail "Error: bad arguments - #{ARGV.join(' ')}\n-h|--help for more info."
68
+ end
69
+
70
+ # Default to standard input
71
+ OPTS[:indat] ||= STDIN.read()
72
+
73
+ #------------------------------------------------------------------------------
74
+ # Do stuff
75
+
76
+ indat = OPTS.delete(:indat)
77
+ len = OPTS.delete(:len)
78
+
79
+ exit 1 unless((len ||= indat.length) > 0)
80
+
81
+ until (m = indat.slice!(0..len-1)).empty?
82
+ print m.hexify(OPTS)
83
+ puts (OPTS[:delim] and not indat.empty?)? OPTS[:delim] : "\n"
84
+ end
85
+
data/bin/len ADDED
@@ -0,0 +1,74 @@
1
+ #!/usr/bin/env ruby
2
+ # Author Eric Monti (emonti at matasano)
3
+ #
4
+ # len prepends a binary length number in front of its input and writes it
5
+ # raw on STDOUT
6
+ #
7
+ # Usage: len [options] <data | blank for stdin>
8
+ # -h, --help Show this message
9
+ # -v, --version Show version and exit
10
+ # -f, --file FILENAME Input from FILENAME
11
+ # -n, --nudge INT Add integer to length
12
+ # -s, --size=SIZE Size of length field in bytes
13
+ # -x, --[no-]swap Swap endianness. Default=big
14
+ # -t, --[no-]total Include size word in size
15
+ # -l, --length=LEN Ignore all else and use LEN
16
+ #
17
+
18
+ require 'rbkb'
19
+ require 'rbkb/command_line'
20
+
21
+ include RBkB::CommandLine
22
+
23
+ #-------------------------------------------------------------------------------
24
+ # Init options and arg parsing
25
+
26
+ # endianness pair. index 0 is always the default
27
+ endpair = [:big, :little]
28
+
29
+ OPTS = {:nudge => 0, :size => 4, :endian => endpair[0]}
30
+
31
+ arg = bkb_stdargs(nil, OPTS)
32
+ arg = bkb_inputargs(arg, OPTS)
33
+
34
+ arg.banner += " <data | blank for stdin>"
35
+
36
+ #------------------------------------------------------------------------------
37
+ # Add local options
38
+ arg.on("-n", "--nudge INT", Numeric, "Add integer to length") do |n|
39
+ OPTS[:nudge] += n
40
+ end
41
+
42
+ arg.on("-s", "--size=SIZE", Numeric, "Size of length field in bytes") do |s|
43
+ bail("Size must be greater than 0") unless (OPTS[:size] = s) > 0
44
+ end
45
+
46
+ arg.on("-x", "--[no-]swap", "Swap endianness. Default=#{OPTS[:endian]}") do |x|
47
+ OPTS[:endian] = endpair[(x)? 1 : 0]
48
+ end
49
+
50
+ arg.on("-t", "--[no-]total", "Include size word in size") do |t|
51
+ OPTS[:tot]=t
52
+ end
53
+
54
+ arg.on("-l", "--length=LEN", Numeric, "Ignore all else and use LEN") do |l|
55
+ OPTS[:static]=l
56
+ end
57
+
58
+ #------------------------------------------------------------------------------
59
+ # Parse arguments
60
+ arg.parse!(ARGV) rescue bail "Error: #{$!}\n#{arg}"
61
+
62
+ OPTS[:indat] ||= ARGV.shift
63
+ OPTS[:indat] ||= STDIN.read
64
+
65
+ bail "Error: Too many arguments\n##{arg}" if ARGV.shift
66
+
67
+ unless len=OPTS[:static]
68
+ len = OPTS[:indat].size
69
+ len += OPTS[:size] if OPTS[:tot]
70
+ len += OPTS[:nudge]
71
+ end
72
+
73
+ STDOUT.write len.to_bytes(OPTS[:endian], OPTS[:size]) << OPTS[:indat]
74
+
data/bin/rex ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # lazy form of ruby -e "xxxx". All commandline arguments get smeared to
4
+ # pure ruby.
5
+
6
+ require 'pp'
7
+ require 'rbkb'
8
+
9
+ eval ARGV.join(' ');
10
+
data/bin/rstrings ADDED
@@ -0,0 +1,123 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # rstrings is "strings"... in ruby... with extra stuff
4
+ #
5
+ # Usage: rstrings [options] <file | blank for stdin>
6
+ # -h, --help Show this message
7
+ # -v, --version Show version and exit
8
+ # -s, --start=OFFSET Start at offset
9
+ # -e, --end=OFFSET End at offset
10
+ # -t, --encoding-type=TYPE Encoding: ascii|unicode|both (default=both)
11
+ # -l, --min-length=NUM Minimum length of strings (default=6)
12
+ # -a, --align=ALIGNMENT Match only on alignment (default=none)
13
+ #
14
+
15
+ require 'rbkb'
16
+ require 'rbkb/command_line'
17
+
18
+ include RBkB::CommandLine
19
+
20
+ #-------------------------------------------------------------------------------
21
+ # Init options and arg parsing
22
+ OPTS = {
23
+ :start_off => 0,
24
+ :end_off => -1,
25
+ :encoding => :both,
26
+ :minimum => 6,
27
+ :align => nil,
28
+ :indat => Array.new,
29
+ :fnames => Array.new,
30
+ }
31
+
32
+ arg = bkb_stdargs(nil, OPTS)
33
+
34
+ arg.banner += " <file | blank for stdin>"
35
+
36
+ #------------------------------------------------------------------------------
37
+ # Add local options
38
+
39
+ arg.on("-s", "--start=OFFSET", "Start at offset") do |s|
40
+ unless m=/^(?:(\d+)|0x([A-Fa-f0-9]+))$/.match(s)
41
+ raise "invalid offset '#{s}'"
42
+ end
43
+ OPTS[:start_off] = (m[2])? m[0].hex : m[0].to_i
44
+ end
45
+
46
+ arg.on("-e", "--end=OFFSET", "End at offset") do |e|
47
+ unless m=/^(?:(\d+)|0x([A-Fa-f0-9]+))$/.match(e)
48
+ raise "invalid offset '#{e}'"
49
+ end
50
+ OPTS[:end_off] = (m[2])? m[0].hex : m[0].to_i
51
+ end
52
+
53
+ arg.on("-t", "--encoding-type=TYPE",
54
+ "Encoding: ascii/unicode/both (default=#{OPTS[:encoding]})") do |t|
55
+ OPTS[:encoding] = t.to_sym
56
+ end
57
+
58
+ arg.on("-l", "--min-length=NUM", Numeric,
59
+ "Minimum length of strings (default=#{OPTS[:minimum]})") do |l|
60
+ OPTS[:minimum] = l
61
+ end
62
+
63
+ arg.on("-a", "--align=ALIGNMENT", Numeric,
64
+ "Match only on alignment (default=none)") do |a|
65
+ (OPTS[:align] = a) > 0 or raise "bad alignment '#{a}'"
66
+ end
67
+
68
+
69
+ #------------------------------------------------------------------------------
70
+ # Parse arguments
71
+ arg.parse!(ARGV) rescue bail "Error: #{$!}\n#{arg}"
72
+
73
+ # default string arg
74
+ if OPTS[:indat].empty? and not ARGV.empty?
75
+ while a=ARGV.shift
76
+ OPTS[:indat] << File.read(a) rescue(bail "Error: Can't open '#{a}'")
77
+ OPTS[:fnames] << a
78
+ end
79
+ end
80
+
81
+ # catchall
82
+ if ARGV.length != 0
83
+ bail "Error: bad arguments - #{ARGV.join(' ')}\n#{arg}"
84
+ end
85
+
86
+ if OPTS[:indat].empty?
87
+ OPTS[:indat] << STDIN.read() if OPTS[:indat].empty?
88
+ OPTS[:fnames] << "[STDIN]"
89
+ end
90
+
91
+ #------------------------------------------------------------------------------
92
+ # Do stuff
93
+
94
+ start_off = OPTS[:start_off]
95
+ end_off = OPTS[:end_off]
96
+ enc = OPTS[:encoding]
97
+ min = OPTS[:minimum]
98
+ align = OPTS[:align]
99
+
100
+ OPTS[:pr_fnames]=true if OPTS[:fnames].size > 1
101
+
102
+ begin
103
+ i=0
104
+ while buf=OPTS[:indat].shift
105
+ buf[start_off..end_off].strings(
106
+ :encoding => enc,
107
+ :minimum => min,
108
+ :align => align
109
+ ) do |off, len, type, str|
110
+ if OPTS[:pr_fnames]
111
+ print "#{OPTS[:fnames][i]}:"
112
+ end
113
+ puts "#{(off+start_off).to_hex.rjust(8,"0")}:"+
114
+ "#{(len+start_off).to_hex.rjust(8,"0")}:"+
115
+ "#{type.to_s[0,1]}:#{str.delete("\000").inspect}"
116
+ end
117
+ i+=1
118
+ end
119
+ rescue
120
+ # STDERR.puts $!
121
+ # exit 1
122
+ end
123
+
data/bin/slice ADDED
@@ -0,0 +1,74 @@
1
+ #!/usr/bin/env ruby
2
+ # (emonti at matasano) Matasano Security LLC
3
+ #
4
+ # slice.rb : returns a slice from input
5
+ # just a shell interface to a string slice operation
6
+ #
7
+ # Usage: slice [options] start (no args when using -r|-x)
8
+ # -h, --help Show this message
9
+ # -v, --version Show version and exit
10
+ # -f, --file FILENAME Input from FILENAME
11
+ # -r, --range=START[:END] Start and optional end range
12
+ # -x, --hexrange=START[:END] same, but in hex
13
+ #
14
+
15
+ require 'rbkb'
16
+ require 'rbkb/command_line'
17
+
18
+ include RBkB::CommandLine
19
+
20
+ OPTS = {:last => -1}
21
+ arg = bkb_stdargs(nil, OPTS)
22
+ arg = bkb_inputargs(arg, OPTS)
23
+
24
+ arg.banner += " start (no args when using -r|-x|)"
25
+
26
+ arg.on("-r", "--range=START[:END]", "Start and optional end range") do |r|
27
+
28
+ raise "-x and -r are mutually exclusive" if OPTS[:first]
29
+
30
+ unless m=/^(-?[0-9]+)(?::(-?[0-9]+))?$/.match(r)
31
+ raise "invalid range #{r.inspect}"
32
+ end
33
+
34
+ OPTS[:first] = $1.to_i
35
+ OPTS[:last] = $2.to_i if $2
36
+ end
37
+
38
+ arg.on("-x", "--hexrange=START[:END]", "same, but in hex") do |r|
39
+
40
+ raise "-x and -r are mutually exclusive" if OPTS[:first]
41
+
42
+ unless m=/^(-?[0-9a-f]+)(?::(-?[0-9a-f]+))?$/i.match(r)
43
+ raise "invalid range #{r.inspect}"
44
+ end
45
+
46
+ OPTS[:first]=($1[0,1] == '-')? ($1[1..-1]).hex_to_num * -1 : $1.hex_to_num
47
+ if $2
48
+ OPTS[:last]=($2[0,1] == '-')? ($2[1..-1]).hex_to_num * -1 : $2.hex_to_num
49
+ end
50
+ end
51
+
52
+ begin
53
+ arg.parse!
54
+
55
+ OPTS[:first] ||= ARGV.shift
56
+
57
+ unless( ARGV.length != 1 and (
58
+ OPTS[:first].kind_of?(Numeric) or
59
+ /^-?\d+$/.match(OPTS[:first]) )
60
+ )
61
+ raise "bad arguments"
62
+ end
63
+
64
+ OPTS[:first] = OPTS[:first].to_i
65
+
66
+ OPTS[:indat] ||= STDIN.read()
67
+
68
+ # drumroll... and substring:
69
+ STDOUT.write OPTS[:indat][ OPTS[:first] .. OPTS[:last] ]
70
+
71
+ rescue
72
+ bail "Error: #{$!}\n#{arg}"
73
+ end
74
+
data/bin/telson ADDED
@@ -0,0 +1,109 @@
1
+ #!/usr/bin/env ruby
2
+ # telson <emonti at matasano> 3/15/2008
3
+ #----------------------------------------------------------------------
4
+ #
5
+ # This is an implementation of the original blackbag "telson" around
6
+ # ruby and eventmachine.
7
+ #
8
+ # Telson can do the following things with minimum fuss:
9
+ # - Run as a server or client using UDP or TCP
10
+ # - Debugging network protocols
11
+ # - Observe client/server behaviors using different messages at
12
+ # various phases of a conversation.
13
+ #
14
+ #----------------------------------------------------------------------
15
+ #
16
+ # Usage: telson -h
17
+ #
18
+
19
+ require 'rbkb'
20
+ require 'rbkb/plug'
21
+ require 'rbkb/command_line'
22
+
23
+ include RBkB::CommandLine
24
+
25
+ Plug::UI::LOGCFG[:verbose] = true
26
+ Plug::UI::LOGCFG[:dump] = :hex
27
+
28
+
29
+ b_addr = Plug::Blit::DEFAULT_IPADDR
30
+ b_port = Plug::Blit::DEFAULT_PORT
31
+
32
+ srced = persist = false
33
+
34
+ s_addr = "0.0.0.0"
35
+ s_port = 0
36
+
37
+ proto = :TCP
38
+
39
+ arg = bkb_stdargs(nil, {})
40
+ arg.banner += " host:port"
41
+
42
+ arg.on("-u", "--udp", "UDP mode") do
43
+ proto=:UDP
44
+ end
45
+
46
+ arg.on("-b", "--blit=ADDR:PORT", "Where to listen for blit") do |b|
47
+ unless m=/^(?:([\w\.]+):)?(\d+)$/.match(b)
48
+ raise "invalid blit address/port"
49
+ end
50
+ b_port = m[2].to_i
51
+ b_addr = m[1] if m[1]
52
+ end
53
+
54
+ arg.on("-o", "--output=FILE", "Output to file instead of screen") do |f|
55
+ Plug::UI::LOGCFG[:out] = File.open(f, "w")
56
+ end
57
+
58
+ arg.on("-q", "--quiet", "Turn off verbose logging") do
59
+ Plug::UI::LOGCFG[:verbose] = false
60
+ end
61
+
62
+ arg.on("-r", "--reconnect", "Attempt to reconnect endlessly.") do
63
+ persist=true
64
+ end
65
+
66
+ arg.on("-s", "--source=(ADDR:?)PORT", "Bind on port (and addr?)") do |p|
67
+ if m=/^(?:([\w\.]+):)?(\d+)$/.match(p)
68
+ s_addr = $1 if $1
69
+ s_port = $2.to_i
70
+ srced = true
71
+ else
72
+ raise "Invalid listen argument: #{p.inspect}"
73
+ end
74
+ end
75
+
76
+
77
+ arg.parse!(ARGV) rescue bail "Error: #{$!}\nUse -h|--help for more info."
78
+
79
+ # Get target argument
80
+ unless (m = /^([\w\.]+):(\d+)$/.match(ARGV.shift)) and ARGV.shift.nil?
81
+ bail arg
82
+ end
83
+
84
+ t_addr = m[1]
85
+ t_port = m[2].to_i
86
+
87
+ loop do
88
+ EventMachine.run {
89
+ if proto == :TCP
90
+ bail "Sorry: --source only works with UDP. Blame EventMachine!" if srced
91
+
92
+ c=EventMachine.connect(t_addr, t_port, Plug::Telson, proto)
93
+
94
+ elsif proto == :UDP
95
+ c=EventMachine.open_datagram_socket(s_addr, s_port, Plug::Telson, proto)
96
+ c.peers.add_peer_manually(t_addr, t_port)
97
+
98
+ ### someday maybe raw or others?
99
+ else
100
+ raise "bad protocol"
101
+ end
102
+
103
+ EventMachine.start_server(b_addr, b_port, Plug::Blit, :TCP, c)
104
+ Plug::UI::verbose("** BLITSRV-#{b_addr}:#{b_port}(TCP) Started")
105
+ }
106
+ break unless persist
107
+ Plug::UI::verbose("** RECONNECTING")
108
+ end
109
+