Class: Ronin::CLI::Commands::Grep Private

Inherits:
FileProcessorCommand show all
Includes:
CommandKit::Colors, PatternOptions
Defined in:
lib/ronin/cli/commands/grep.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Greps for common patterns in a file/stream.

Usage

ronin grep [options] [FILE ...]

Options

-N, --number                     Searches for all numbers
-X, --hex-number                 Searches for all hexadecimal numbers
-V, --version-number             Searches for all version numbers
-w, --word                       Searches for all words
    --mac-addr                   Searches for all MAC addresses
-4, --ipv4-addr                  Searches for all IPv4 addresses
-6, --ipv6-addr                  Searches for all IPv6 addresses
-I, --ip                         Searches for all IP addresses
-H, --host                       Searches for all host names
-D, --domain                     Searches for all domain names
    --uri                        Searches for all URIs
-U, --url                        Searches for all URLs
    --user-name                  Searches for all user names
-E, --email-addr                 Searches for all email addresses
    --obfuscated-email-addr      Searches for all obfuscated email addresses
    --phone-number               Searches for all phone numbers
    --ssn                        Searches for all Social Security Numbers (SSNs)
    --amex-cc                    Searches for all AMEX Credit Card numbers
    --discover-cc                Searches for all Discover Card numbers
    --mastercard-cc              Searches for all MasterCard numbers
    --visa-cc                    Searches for all VISA Credit Card numbers
    --visa-mastercard-cc         Searches for all VISA MasterCard numbers
    --cc                         Searches for all Credit Card numbers
    --file-name                  Searches for all file names
    --dir-name                   Searches for all directory names
    --relative-unix-path         Searches for all relative UNIX paths
    --absolute-unix-path         Searches for all absolute UNIX paths
    --unix-path                  Searches for all UNIX paths
    --relative-windows-path      Searches for all relative Windows paths
    --absolute-windows-path      Searches for all absolute Windows paths
    --windows-path               Searches for all Windows paths
    --relative-path              Searches for all relative paths
    --absolute-path              Searches for all absolute paths
-P, --path                       Searches for all paths
    --identifier                 Searches for all identifier names
    --variable-name              Searches for all variable names
    --variable-assignment        Searches for all variable assignments
    --function-name              Searches for all function names
    --md5                        Searches for all MD5 hashes
    --sha1                       Searches for all SHA1 hashes
    --sha256                     Searches for all SHA256 hashes
    --sha512                     Searches for all SHA512 hashes
    --hash                       Searches for all hashes
    --ssh-private-key            Searches for all SSH private key data
    --dsa-private-key            Searches for all DSA private key data
    --ec-private-key             Searches for all EC private key data
    --rsa-private-key            Searches for all RSA private key data
-K, --private-key                Searches for all private key data
    --ssh-public-key             Searches for all SSH public key data
    --public-key                 Searches for all public key data
    --aws-access-key-id          Searches for all AWS access key IDs
    --aws-secret-access-key      Searches for all AWS secret access keys
-A, --api-key                    Searches for all API keys
    --single-quoted-string       Searches for all single-quoted strings
    --double-quoted-string       Searches for all double-quoted strings
-S, --string                     Searches for all quoted strings
-B, --base64                     Searches for all Base64 strings
    --c-comment                  Searches for all C comments
    --cpp-comment                Searches for all C++ comments
    --java-comment               Searches for all Java comments
    --javascript-comment         Searches for all JavaScript comments
    --shell-comment              Searches for all Shell comments
    --ruby-comment               Searches for all Ruby comments
    --python-comment             Searches for all Python comments
    --comment                    Searches for all comments
-e, --regexp /REGEXP/            Custom regular expression to search for
-o, --only-matching              Only print the matching data
-n, --line-number                Print the line number for each line
    --with-filename              Print the file name for each match
-h, --help                       Print help information

Arguments

[FILE ...]                       Optional input file(s)

Since:

  • 2.0.0

Instance Attribute Summary

Attributes included from PatternOptions

#pattern

Instance Method Summary collapse

Methods included from PatternOptions

define_credentials_options, define_crypto_options, define_file_system_options, define_language_options, define_network_options, define_numeric_options, define_pii_options, define_source_code_options, included

Methods inherited from FileProcessorCommand

#open_file, #process_file

Instance Method Details

#filename_of(io) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the file name for the IO stream.

Parameters:

  • io (File, IO)

Returns:

  • (String)

Since:

  • 2.0.0



166
167
168
169
170
171
# File 'lib/ronin/cli/commands/grep.rb', line 166

def filename_of(io)
  case io
  when File then io.path
  else           '[stdin]'
  end
end

#match_line(line, **kwargs) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Attempts to match a line of text.

Parameters:

  • line (String)
  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.

Options Hash (**kwargs):

  • :filename (String)
  • :line_number (Integer)

Since:

  • 2.0.0



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
# File 'lib/ronin/cli/commands/grep.rb', line 185

def match_line(line,**kwargs)
  index = 0

  printed_prefix = false
  only_matching  = options[:only_matching]

  while (match = line.match(@pattern,index))
    unless printed_prefix
      print_line_prefix(**kwargs)
      printed_prefix = true
    end

    match_start, match_stop = match.offset(0)

    # print the text before the match, unless --only-matching is enabled
    print(line[index...match_start]) unless only_matching
    print_match(match)

    index = match_stop
  end

  unless only_matching
    # print the rest of the line, if we've had at least one match
    puts(line[index..]) if index > 0
  end
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Optionally prints the filename or line-number prefix for a line.

Parameters:

  • filename (String)
  • line_number (Integer)

Since:

  • 2.0.0



219
220
221
222
223
224
225
226
227
228
229
# File 'lib/ronin/cli/commands/grep.rb', line 219

def print_line_prefix(filename: , line_number: )
  if options[:with_filename]
    print colors.magenta(filename)
    print colors.cyan(':')
  end

  if options[:line_numbers]
    print colors.green(line_number)
    print colors.cyan(':')
  end
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prints the matched string w/o ANSI highlighting.

Parameters:

  • match (String)

Since:

  • 2.0.0



236
237
238
239
240
241
242
243
244
245
# File 'lib/ronin/cli/commands/grep.rb', line 236

def print_match(match)
  match_string = match[0]
  highlighted  = colors.bold(colors.red(match_string))

  if options[:only_matching]
    puts highlighted
  else
    print highlighted
  end
end

#process_input(input) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Greps the input stream.

Parameters:

  • input (IO, StringIO)

    The input stream to grep.

Since:

  • 2.0.0



151
152
153
154
155
156
157
# File 'lib/ronin/cli/commands/grep.rb', line 151

def process_input(input)
  filename = filename_of(input)

  input.each_line(chomp: true).with_index do |line,index|
    match_line(line, filename: filename, line_number: index + 1)
  end
end

#run(*files) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the ronin grep command.

Parameters:

  • files (Array<String>)

    Additional file arguments to grep.

Since:

  • 2.0.0



136
137
138
139
140
141
142
143
# File 'lib/ronin/cli/commands/grep.rb', line 136

def run(*files)
  unless @pattern
    print_error "must specify a pattern to search for"
    exit(-1)
  end

  super(*files)
end