Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/pentex/core.rb

Overview

Hacking Extensions For The String Class

Instance Method Summary collapse

Instance Method Details

#decode_b64Object



43
44
45
# File 'lib/pentex/core.rb', line 43

def decode_b64
  Base64.decode64(self)
end

#decode_cisco7Object

decodes cisco 7 passwords which can be found in IOS/CatOS configs.
e.g., password 7 07362E590E1B1C041B1E124C0A2F2E206832752E1A01134D



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/pentex/core.rb', line 106

def decode_cisco7

  xlat = %w( 64 73 66 64 3b 6b 66 6f 41 2c 2e 69 79 65 77 72 6b 6c 64 4a 4b 44 48 53 55 42 73 67 76 63 )

  ep = self.strip
  # sample: "07362E590E1B1C041B1E124C0A2F2E206832752E1A01134D" # -> "You really need a life."
  dp = ""
  if ep =~ /^(..)(.*)/o
    s = $1.to_i
    e = $2
    i = 0
    0.step(e.length-1, 2) do |x|
      dp += ( e.slice(x,2).hex ^ xlat[s].hex ).chr;
      s+=1
      break if s >= xlat.length
    end

  end
  dp
end

#decode_hexObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pentex/core.rb', line 67

def decode_hex
# first normalize hex-string
s = self.gsub(/\\x/,'')
s.gsub!(/0x/,'')
s.gsub!(/\W/,'')
r = ''
0.step(s.length-1,2) do |x|
  r << s.slice(x,2).hex.chr
end
r
end

#decode_uriObject



63
64
65
# File 'lib/pentex/core.rb', line 63

def decode_uri
  URI.unescape(self)
end

#decode_urlObject



51
52
53
# File 'lib/pentex/core.rb', line 51

def decode_url
  CGI.unescape(self)
end

#encode_b64Object



47
48
49
# File 'lib/pentex/core.rb', line 47

def encode_b64
  Base64.encode64(self)
end

#encode_uriObject



59
60
61
# File 'lib/pentex/core.rb', line 59

def encode_uri
  URI.escape(self)
end

#encode_urlObject



55
56
57
# File 'lib/pentex/core.rb', line 55

def encode_url
  CGI.escape(self)
end

#hexdump(opts = {}) ⇒ Object

shows (binary) string in a readable format.
Options:

:bytes => 16;       the maximum number of bytes per line
:offset => 0;       not supported yet
:max_bytes => 0;    the max number of bytes to be processed
:chunksize => 8;    splitted by "-"
:range => nil;      use this to define the range to be displayes, e.g. "(10..20)" must be a Range-Object!
:format => :simple; this is the only format supported yet
:max_lines => 40;   number of lines before stopping output


Example 1:

>> "\x02:this is a string with some binary data\xff".hexdump
000000: 02 3A 74 68 69 73 20 69 - 73 20 61 20 73 74 72 69   :.:this is a stri
000010: 6E 67 20 77 69 74 68 20 - 73 6F 6D 65 20 62 69 6E   :ng with some bin
000020: 61 72 79 20 64 61 74 61 - FF                        :ary data.
=> true


Example 2: giving a range with int and hex value calculation

"\x02:this is a string with some binary data\xff".hexdump :range => (16...0x10+0x10)
000010: 6E 67 20 77 69 74 68 20 - 73 6F 6D 65 20 62 69      :ng with some bi
=> true


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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/pentex/core.rb', line 150

def hexdump( opts = {} )
  copts = {
    :bytes => 16,
    :offset => 0,
    :max_bytes => 0,
    :chunksize => 8,
    :range => nil,
    :format => :simple,
    :max_lines => 40
  }
  # lame kind of implementing a command help, anyway better than opening source file ;)
  unless opts.is_a? Hash
    puts "[options]"
    puts copts.to_yaml
    puts
    puts "[example]\n>>'this is a string'.hexdump :max_bytes => 8"
    puts
  return true
  end

  copts.update opts

  dump = ""
  offset = 0
  linecount = 0
  max_bytes = copts[:max_bytes] > 0 ? copts[:max_bytes] : self.length
  if copts[:range].is_a? Range
    offset = copts[:range].min
    max_bytes = copts[:range].max
  end

  counter = offset

  while counter < max_bytes

    pos = sprintf("%06X", counter)

    linelen = (counter < max_bytes - copts[:bytes] ) ? copts[:bytes] : (max_bytes - counter)
    bytes = ""
    linelen.times do |i|
      bytes << "%02X" % self[counter + i].ord
      bytes << " " unless (i+1) % copts[:bytes] == 0
      bytes << "- " if ( i+1) % copts[:chunksize]  == 0 && (i+1) < linelen
    end

    maxlen = ( copts[:bytes] * 2 + copts[:bytes] - 1 + copts[:bytes] / copts[:chunksize] )
    bytes << " " * (maxlen - bytes.length) if maxlen > bytes.length
    ascii = self[counter, linelen].printable
    line = "#{pos}: #{bytes}   :#{ascii}\n"
    puts line
    linecount += 1
    if linecount >= copts[:max_lines]
      puts "<Enter>, [qQ]uit"
      c = gets
      return false if c =~ /^q/i
    linecount = 0
    end
    dump << line
    counter += copts[:bytes]
  end

  return dump unless copts[:format] == :simple

  return true
end

#printable(non_print_char = ".") ⇒ Object



216
217
218
# File 'lib/pentex/core.rb', line 216

def printable( non_print_char = "." )
  x = self.gsub(/[^[:print:]]/, non_print_char )
end

#to_hex(style = :plain) ⇒ Object

converts string to hex format
style: let’s you choose between different formats

:plain (default)   -> "61624358"
:c  (c++ style)  -> "\\x61\\x62\\x43\\x58"
:r  (ruby style) -> "\\x61\\x62\\x43\\x58"


Example:

>> "abCX".to_hex :c
=> "\\x61\\x62\\x43\\x58"


90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pentex/core.rb', line 90

def to_hex( style = :plain )
   x = self.unpack("H*")[0]
 h = case style.to_s
   when /plain/i
   x
   when /^c/i
   x.gsub(/(..)/,'\x\1')
   when /^r/i
   x.gsub(/(..)/,'\x\1')
 end
 h
end

#to_md5(format = :hex) ⇒ Object



35
36
37
# File 'lib/pentex/core.rb', line 35

def to_md5(format = :hex)
  Digest::MD5.hexdigest(self)
end

#to_sha1(format = :hex) ⇒ Object



39
40
41
# File 'lib/pentex/core.rb', line 39

def to_sha1(format = :hex)
  Digest::SHA1.hexdigest(self)
end