emonti-rbkb 0.6.1.3 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/unhexify CHANGED
@@ -1,65 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- # Author Eric Monti (emonti at matasano)
3
- #
4
- # unhexify converts a string of hex bytes back to raw data. Input can be
5
- # supplied via stdin, a hex-string argument, or a file containing hex (use -f).
6
- #
7
- # Usage: unhexify [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
- # -d, --delim DELIMITER DELIMITER regex between hex chunks
12
- #
13
2
 
14
- require 'rbkb'
15
- require 'rbkb/command_line'
16
-
17
- include RBkB::CommandLine
18
-
19
- #-------------------------------------------------------------------------------
20
- # Init options and arg parsing
21
- OPTS = {}
22
- arg = bkb_stdargs(nil, OPTS)
23
-
24
- arg.banner += " <data | blank for stdin>"
25
-
26
- arg = bkb_inputargs(arg, OPTS)
27
-
28
- #------------------------------------------------------------------------------
29
- # Add local options
30
-
31
- arg.on("-d", "--delim DELIMITER",
32
- "DELIMITER regex between hex chunks") do |d|
33
- OPTS[:delim] = Regexp.new(d.gsub('\\\\', '\\'))
34
- end
35
-
36
- #------------------------------------------------------------------------------
37
- # Parse arguments
38
- arg.parse!(ARGV) rescue bail "Error: #{$!}\n#{arg}"
39
-
40
- # default string arg
41
- if OPTS[:indat].nil? and a=ARGV.shift
42
- OPTS[:indat] = a.dup
43
- end
44
-
45
- # catchall
46
- if ARGV.length != 0
47
- bail "Error: bad arguments - #{ARGV.join(' ')}\n#{arg}"
48
- end
49
-
50
- OPTS[:indat] ||= STDIN.read()
51
-
52
- #------------------------------------------------------------------------------
53
- # Do stuff
54
-
55
- OPTS[:indat].delete!("\r\n")
56
- OPTS[:indat]
57
-
58
- OPTS[:delim] ||= /\s*/
59
-
60
- unless out = OPTS[:indat].unhexify(OPTS[:delim])
61
- bail "Error: Failed parsing as hex"
62
- end
63
-
64
- STDOUT.write(out)
3
+ require "rbkb/cli/unhexify"
65
4
 
5
+ Rbkb::Cli::Unhexify.run()
data/bin/urldec CHANGED
@@ -1,56 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- # Author Eric Monti (emonti at matasano)
3
- #
4
- # urldec converts a url-encoded string back to its raw form.
5
- # (url encoding is really just fancy hex encoding)
6
- #
7
- # Usage: urldec [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
- # -p, --[no-]plus Convert '+' to space (default is true)
12
- #
13
2
 
14
- require 'rbkb'
15
- require 'rbkb/command_line'
16
-
17
- include RBkB::CommandLine
18
-
19
- #-------------------------------------------------------------------------------
20
- # Init options and arg parsing
21
- OPTS = {}
22
- arg = bkb_stdargs(nil, OPTS)
23
-
24
- arg.banner += " <data | blank for stdin>"
25
-
26
- arg = bkb_inputargs(arg, OPTS)
27
-
28
- arg.on("-p", "--[no-]plus", "Convert '+' to space (default is true)") do |p|
29
- OPTS[:noplus] = (not p)
30
- end
31
-
32
-
33
- #------------------------------------------------------------------------------
34
- # Add local options here
35
-
36
- #------------------------------------------------------------------------------
37
- # Parse arguments
38
- arg.parse!(ARGV) rescue bail "Error: #{$!}\n#{arg}"
39
-
40
- # default string arg
41
- if OPTS[:indat].nil? and a=ARGV.shift
42
- OPTS[:indat] = a
43
- end
44
-
45
- # catchall
46
- if ARGV.length != 0
47
- bail "Error: bad arguments - #{ARGV.join(' ')}\n#{arg}"
48
- end
49
-
50
- OPTS[:indat] ||= STDIN.read()
51
-
52
- #------------------------------------------------------------------------------
53
- # Do stuff
54
-
55
- STDOUT.write(OPTS[:indat].urldec(:noplus => OPTS[:noplus]))
3
+ require "rbkb/cli/urldec"
56
4
 
5
+ Rbkb::Cli::Urldec.run()
data/bin/urlenc CHANGED
@@ -1,55 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- # Author Eric Monti (emonti at matasano)
3
- #
4
- # urlenc converts a string or raw data to a url-encoded string
5
- # (url encoding is really just fancy hex encoding)
6
- #
7
- # Usage: urlenc [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
- # -p, --[no-]plus Convert spaces to '+' (default is false)
12
- #
13
2
 
14
- require 'rbkb'
15
- require 'rbkb/command_line'
16
-
17
- include RBkB::CommandLine
18
-
19
- #------------------------------------------------------------------------------
20
- # Init options and arg parsing
21
- OPTS = {}
22
- arg = bkb_stdargs(nil, OPTS)
23
- arg = bkb_inputargs(arg, OPTS)
24
-
25
- arg.banner += " <data | blank for stdin>"
26
-
27
- arg.on("-p", "--[no-]plus", "Convert spaces to '+' (default is false)") do |p|
28
- OPTS[:plus] = p
29
- end
30
-
31
- #------------------------------------------------------------------------------
32
- # Add local options here
33
-
34
- #------------------------------------------------------------------------------
35
- # Parse arguments
36
- arg.parse!(ARGV) rescue bail "Error: #{$!}\nUse -h|--help for more info."
37
-
38
- # default string arg
39
- if OPTS[:indat].nil? and a=ARGV.shift
40
- OPTS[:indat] = a.dup
41
- end
42
-
43
- # catchall
44
- if ARGV.length != 0
45
- bail "Error: bad arguments - #{ARGV.join(' ')}\n-h|--help for more info."
46
- end
47
-
48
- # Default to standard input
49
- OPTS[:indat] ||= STDIN.read()
50
-
51
- #------------------------------------------------------------------------------
52
- # Do stuff
53
-
54
- puts OPTS[:indat].urlenc(:plus => OPTS[:plus])
3
+ require "rbkb/cli/urlenc"
55
4
 
5
+ Rbkb::Cli::Urlenc.run()
data/bin/xor CHANGED
@@ -1,60 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- # Author Eric Monti (emonti at matasano)
3
- #
4
- # repeating string xor
5
- #
6
- # Usage: xor [options] -k|-s <key> <data | stdin>
7
- # -h, --help Show this message
8
- # -v, --version Show version and exit
9
- # -f, --file FILENAME Input from FILENAME
10
- #
11
- # Key options (one of the following is required):
12
- # -s, --strkey STRING xor against bare STRING
13
- # -x, --hexkey HEXSTR xor against decoded HEXSTR
14
- #
15
2
 
16
- require 'rbkb'
17
- require 'rbkb/command_line'
18
-
19
- include RBkB::CommandLine
20
-
21
- #-------------------------------------------------------------------------------
22
- # Init options and arg parsing
23
- OPTS = {}
24
- arg = bkb_stdargs(nil, OPTS)
25
- arg = bkb_inputargs(arg, OPTS)
26
-
27
- arg.banner += " -k|-s <key> <data | stdin>"
28
-
29
- #------------------------------------------------------------------------------
30
- # Add local options
31
- arg.separator ""
32
- arg.separator " Key options (one of the following is required):"
33
-
34
- arg.on("-s", "--strkey STRING", "xor against bare STRING") do |s|
35
- bail "only one key option can be specified with -s or -x" if OPTS[:key]
36
- OPTS[:key] = s
37
- end
38
-
39
- arg.on("-x", "--hexkey HEXSTR", "xor against decoded HEXSTR") do |x|
40
- bail "only one key option can be specified with -s or -x" if OPTS[:key]
41
- x.sub!(/^0[xX]/, '')
42
- bail "Unable to parse hex string" unless OPTS[:key] = x.unhexify
43
- end
44
-
45
- #------------------------------------------------------------------------------
46
- # Parse arguments
47
- arg.parse!(ARGV) rescue bail "Error: #{$!}\n#{arg}"
48
-
49
- bail "You must specify a key with -s or -x\n#{arg}" unless OPTS[:key]
50
-
51
- OPTS[:indat] ||= ARGV.shift
52
-
53
- if ARGV.length != 0
54
- bail "Error: bad arguments - #{ARGV.join(' ')}\n-h|--help for more info."
55
- end
56
-
57
- OPTS[:indat] ||= STDIN.read()
58
-
59
- STDOUT.write OPTS[:indat].xor(OPTS[:key])
3
+ require "rbkb/cli/xor"
60
4
 
5
+ Rbkb::Cli::Xor.run()
@@ -0,0 +1,27 @@
1
+ require 'rbkb/cli'
2
+
3
+ # b64 converts strings or raw data to base-64 encoding.
4
+ class Rbkb::Cli::B64 < Rbkb::Cli::Executable
5
+ def make_parser
6
+ super()
7
+ arg = @oparse
8
+ arg.banner += " <data | blank for stdin>"
9
+
10
+ arg.on("-l", "--length LEN", Numeric, "Output LEN chars per line") do |l|
11
+ bail("length must be > 0") unless l > 0
12
+ @opts[:len] = l
13
+ end
14
+ end
15
+
16
+ def parse(*args)
17
+ super(*args)
18
+ parse_string_argument(:indat)
19
+ parse_catchall()
20
+ end
21
+
22
+ def go(*args)
23
+ super(*args)
24
+ @stdout << @opts[:indat].b64(opts[:len]).chomp + "\n"
25
+ end
26
+ end
27
+
@@ -0,0 +1,78 @@
1
+ require 'rbkb/cli'
2
+
3
+ # searches for a binary string in input. string can be provided 'hexified'
4
+ class Rbkb::Cli::Bgrep < Rbkb::Cli::Executable
5
+ def initialize(*args)
6
+ super(*args)
7
+ @opts[:start_off] ||= 0
8
+ @opts[:end_off] ||= -1
9
+ end
10
+
11
+ def make_parser
12
+ arg = super()
13
+ arg.banner += " <search> <file | blank for stdin>"
14
+
15
+ arg.on("-x", "--[no-]hex", "Search for hex (default: false)") do |x|
16
+ @opts[:hex] = x
17
+ end
18
+
19
+ arg.on("-r", "--[no-]regex", "Search for regex (default: false)") do |r|
20
+ @opts[:rx] = r
21
+ end
22
+
23
+ arg.on("-a", "--align=BYTES", Numeric,
24
+ "Only match on alignment boundary") do |a|
25
+ @opts[:align] = a
26
+ end
27
+
28
+ arg.on("-n", "--[no-]filename", "Suppress prefixing of filenames.") do |n|
29
+ @opts[:suppress_fname] = n
30
+ end
31
+ return arg
32
+ end
33
+
34
+
35
+ def parse(*args)
36
+ super(*args)
37
+
38
+ bail "need search argument" unless @find = @argv.shift
39
+
40
+ if @opts[:hex] and @opts[:rx]
41
+ bail "-r and -x are mutually exclusive"
42
+ end
43
+
44
+ # ... filenames vs. stdin will be parsed in 'go'
45
+ end
46
+
47
+ def go(*args)
48
+ super(*args)
49
+
50
+ if @opts[:hex]
51
+ bail "you specified -x for hex and the subject isn't" unless @find.ishex?
52
+ @find = @find.unhexify
53
+ elsif @opts[:rx]
54
+ @find = Regexp.new(@find, Regexp::MULTILINE)
55
+ end
56
+
57
+ if fname = @argv.shift
58
+ dat = do_file_read(fname)
59
+ fname = nil unless @argv[0] # only print filenames for multiple files
60
+ else
61
+ dat = @stdin.read
62
+ end
63
+
64
+ loop do
65
+ dat.bgrep(@find, @opts[:align]) do |hit_start, hit_end, match|
66
+ print "#{fname}:" if fname and not @opts[:suppress_fname]
67
+
68
+ puts("#{(hit_start).to_hex.rjust(8,"0")}:"+
69
+ "#{(hit_end).to_hex.rjust(8,"0")}:b:"+
70
+ "#{match.inspect}")
71
+ end
72
+
73
+ break unless fname=@argv.shift
74
+ dat = do_file_read(fname)
75
+ end
76
+ end
77
+ end
78
+
@@ -0,0 +1,86 @@
1
+ require 'rbkb/cli'
2
+ require 'rbkb/plug'
3
+
4
+ # blit is for use with any of the "plug" tools such as telson, feed, blitplug.
5
+ # It is used to send data over a socket via their OOB blit listener.
6
+ class Rbkb::Cli::Blit < Rbkb::Cli::Executable
7
+ attr_accessor :blit_msg
8
+
9
+ def initialize(*args)
10
+ super(*args)
11
+ {
12
+ :b_addr => Plug::Blit::DEFAULT_IPADDR,
13
+ :b_port => Plug::Blit::DEFAULT_PORT,
14
+ :bp_proto => :TCP,
15
+ :b_peeridx => 0,
16
+ }.each {|k, v| @opts[k] ||= v}
17
+ end
18
+
19
+ def make_parser()
20
+ super()
21
+ add_std_file_opt(:indat)
22
+ arg = @oparse
23
+
24
+ arg.banner += " <data | blank for stdin>"
25
+
26
+ arg.on("-t", "--trans-protocol=PROTO",
27
+ "Blit transport protocol TCP/UDP") do |t|
28
+ @opts[:b_proto] = t.upcase.to_sym
29
+ end
30
+
31
+ arg.on("-b", "--blitsrv=ADDR:PORT",
32
+ "Where to send blit messages") do |b|
33
+
34
+ unless(m=/^(?:([\w\.]+):)?(\d+)$/.match(b))
35
+ bail "invalid blit address/port"
36
+ end
37
+ @opts[:b_port] = m[2].to_i
38
+ @opts[:b_port] = m[1] if m[1]
39
+ end
40
+
41
+ arg.on("-i", "--peer-index=IDX", Numeric,
42
+ "Index for remote peer to receive") do |i|
43
+ @opts[:b_peeridx] = i
44
+ end
45
+
46
+ arg.on("-l", "--list-peers", "Lists the peer array for the target") do
47
+ @blit_msg = Plug::Blit.make_list_peers
48
+ end
49
+
50
+ arg.on("-k", "--kill", "Stops the remote event loop.") do
51
+ @blit_msg = Plug::Blit.make_kill
52
+ end
53
+
54
+ return arg
55
+ end
56
+
57
+ def parse(*args)
58
+ super(*args)
59
+
60
+ unless @blit_msg
61
+ if @opts[:indat].nil?
62
+ @opts[:indat] = (@argv.length > 0)? @argv.join(" ") : @stdin.read()
63
+ end
64
+ @blit_msg = Plug::Blit.make_sendmsg(@opts[:b_peeridx], @opts[:indat])
65
+ end
66
+ end
67
+
68
+ def go(*args)
69
+ super(*args)
70
+
71
+ begin
72
+ Plug::Blit.blit_init(
73
+ :addr => @opts[:b_addr],
74
+ :port => @opts[:b_port],
75
+ :protocol => @opts[:b_proto]
76
+ )
77
+
78
+ Plug::Blit.blit_raw(@blit_msg)
79
+ rescue
80
+ bail($!)
81
+ end
82
+
83
+ self.exit(0)
84
+ end
85
+
86
+ end
@@ -0,0 +1,20 @@
1
+ require 'rbkb/cli'
2
+
3
+ # Repeats an argument N times
4
+ class Rbkb::Cli::Chars < Rbkb::Cli::Executable
5
+ def make_parser
6
+ super()
7
+ @oparse.banner += " 100 A; # print 100 A's"
8
+ end
9
+
10
+ def parse(*args)
11
+ super(*args)
12
+ bail_args @argv.join unless @argv.size == 2
13
+ end
14
+
15
+ def go(*args)
16
+ super(*args)
17
+ @stdout << @argv[1] * @argv[0].to_i
18
+ end
19
+ end
20
+
@@ -0,0 +1,29 @@
1
+ require 'rbkb/cli'
2
+
3
+ # crc32 returns a crc32 checksum in hex from stdin or a file
4
+ class Rbkb::Cli::Crc32 < Rbkb::Cli::Executable
5
+ def initialize(*args)
6
+ super(*args)
7
+ @opts[:first] ||= 0
8
+ @opts[:last] ||= -1
9
+ end
10
+
11
+ def make_parser()
12
+ super()
13
+ add_std_file_opt(:indat)
14
+ add_range_opts(:first, :last)
15
+ end
16
+
17
+ def parse(*args)
18
+ super(*args)
19
+ parse_catchall()
20
+ end
21
+
22
+ def go(*args)
23
+ super(*args)
24
+ @opts[:indat] ||= @stdin.read()
25
+ @stdout.puts @opts[:indat][ @opts[:first] .. @opts[:last] ].crc32.to_hex
26
+ end
27
+ end
28
+
29
+
@@ -0,0 +1,22 @@
1
+ require 'rbkb/cli'
2
+
3
+ # d64 converts a base-64 encoded string back to its orginal form.
4
+ class Rbkb::Cli::D64 < Rbkb::Cli::Executable
5
+ def make_parser
6
+ super()
7
+ @oparse.banner += " <data | blank for stdin>"
8
+ end
9
+
10
+ def parse(*args)
11
+ super(*args)
12
+ parse_string_argument(:indat)
13
+ parse_catchall()
14
+ end
15
+
16
+ def go(*args)
17
+ super(*args)
18
+ @stdout << @opts[:indat].d64
19
+ self.exit(0)
20
+ end
21
+ end
22
+
@@ -0,0 +1,47 @@
1
+ require 'rbkb/cli'
2
+
3
+ # Reverses a hexdump back to raw data. Designed to work with hexdumps created
4
+ # by Unix utilities like 'xxd' as well as 'hexdump -C'.
5
+ class Rbkb::Cli::Dedump < Rbkb::Cli::Executable
6
+ def initialize(*args)
7
+ super(*args)
8
+ @opts[:len] ||= 16
9
+ end
10
+
11
+ def make_parser()
12
+ arg = super()
13
+ arg.banner += " <input-file | blank for stdin>"
14
+
15
+ arg.on("-l", "--length LEN", Numeric,
16
+ "Bytes per line in hexdump (default: #{@opts[:len]})") do |l|
17
+ bail("Length must be greater than zero") unless (@opts[:len] = l) > 0
18
+ end
19
+ return arg
20
+ end
21
+
22
+ def parse(*args)
23
+ super(*args)
24
+ parse_file_argument(:indat)
25
+ parse_catchall()
26
+ end
27
+
28
+ def go(*args)
29
+ super(*args)
30
+
31
+ # Default to standard input
32
+ @opts[:indat] ||= @stdin.read()
33
+
34
+ self.exit(1) unless((@opts[:len] ||= @opts[:indat].length) > 0)
35
+
36
+ begin
37
+ @opts[:indat].dehexdump( :len => @opts[:len], :out => @stdout)
38
+ rescue
39
+ bail "Error: #{$!}"
40
+ end
41
+
42
+ self.exit(0)
43
+ end
44
+ end
45
+
46
+
47
+
@@ -0,0 +1,64 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rbkb/cli'
4
+
5
+ # The hexify command converts a string or raw data to hex characters.
6
+ # Input can be supplied via stdin, a string argument, or a file (with -f).
7
+ class Rbkb::Cli::Hexify < Rbkb::Cli::Executable
8
+ def make_parser
9
+ super()
10
+ add_std_file_opt(:indat)
11
+ arg = @oparse
12
+
13
+ # Add local options
14
+ arg.banner += " <data | blank for stdin>"
15
+
16
+ arg.on("-l", "--length LEN", Numeric, "Output lines of LEN bytes") do |l|
17
+ bail("Length must be greater than zero") unless (@opts[:len] = l) > 0
18
+ end
19
+
20
+ arg.on("-d", "--delim=DELIMITER", "DELIMITER between each byte") do |d|
21
+ @opts[:delim] = d
22
+ end
23
+
24
+ arg.on("-p", "--prefix=PREFIX", "PREFIX before each byte") do |p|
25
+ @opts[:prefix] = p
26
+ end
27
+
28
+ arg.on("-s", "--suffix=SUFFIX", "SUFFIX after each byte") do |s|
29
+ @opts[:suffix] = s
30
+ end
31
+ end
32
+
33
+ def parse(*args)
34
+ super(*args)
35
+
36
+ # blackbag-style space delimiter compatability
37
+ if @argv[0] == "+" and @opts[:delim].nil?
38
+ @opts[:delim]=" "
39
+ @argv.shift
40
+ end
41
+
42
+ parse_string_argument(:indat)
43
+ parse_catchall()
44
+ end
45
+
46
+ def go(*args)
47
+ super(*args)
48
+
49
+ # Default to standard input
50
+ @opts[:indat] ||= @stdin.read()
51
+
52
+ indat = @opts.delete(:indat)
53
+ len = @opts.delete(:len)
54
+
55
+ self.exit(1) unless((len ||= indat.length) > 0)
56
+
57
+ until (m = indat.slice!(0..len-1)).empty?
58
+ @stdout << m.hexify(@opts)
59
+ @stdout.puts((opts[:delim] and ! indat.empty?)? opts[:delim] : "\n")
60
+ end
61
+ self.exit(0)
62
+ end
63
+ end
64
+
@@ -0,0 +1,70 @@
1
+ require 'rbkb/cli'
2
+
3
+ # len prepends a binary length number in front of its input and outputs
4
+ # raw on STDOUT
5
+ class Rbkb::Cli::Len < Rbkb::Cli::Executable
6
+
7
+ def initialize(*args)
8
+ super(*args)
9
+
10
+ # endianness pair. index 0 is always the default
11
+ @endpair = [:big, :little]
12
+ {
13
+ :nudge => 0,
14
+ :size => 4,
15
+ :endian => @endpair[0],
16
+ }.each {|k,v| @opts[k] ||= v}
17
+ end
18
+
19
+
20
+ def make_parser()
21
+ super()
22
+ add_std_file_opt(:indat)
23
+ arg = @oparse
24
+ arg.banner += " <data | blank for stdin>"
25
+
26
+ arg.on("-n", "--nudge INT", Numeric, "Add integer to length") do |n|
27
+ @opts[:nudge] += n
28
+ end
29
+
30
+ arg.on("-s", "--size=SIZE", Numeric,
31
+ "Size of length field in bytes") do |s|
32
+ bail("Size must be greater than 0") unless (@opts[:size] = s) > 0
33
+ end
34
+
35
+ arg.on("-x", "--[no-]swap",
36
+ "Swap endianness. Default=#{@opts[:endian]}") do |x|
37
+ @opts[:endian] = @endpair[(x)? 1 : 0]
38
+ end
39
+
40
+ arg.on("-t", "--[no-]total", "Include size word in size") do |t|
41
+ @opts[:tot]=t
42
+ end
43
+
44
+ arg.on("-l", "--length=LEN", Numeric,
45
+ "Ignore all other flags and use static LEN") do |l|
46
+ @opts[:static]=l
47
+ end
48
+ end
49
+
50
+
51
+ def parse(*args)
52
+ super(*args)
53
+ @opts[:indat] ||= @argv.shift
54
+ parse_catchall()
55
+ @opts[:indat] ||= @stdin.read
56
+ end
57
+
58
+
59
+ def go(*args)
60
+ super(*args)
61
+ unless len=@opts[:static]
62
+ len = @opts[:indat].size
63
+ len += @opts[:size] if @opts[:tot]
64
+ len += @opts[:nudge]
65
+ end
66
+ @stdout << len.to_bytes(@opts[:endian], @opts[:size]) << @opts[:indat]
67
+ end
68
+
69
+ end
70
+