Wednesday, June 20, 2012

num_jenny - A Versatile Number Generator for the Security Mindset


A helpful ruby class for all sorts of security projects...this class is a member of my periodic table of classes.  Get it here:

https://github.com/ninp0/num_jenny 

It's pretty cool since this ruby class can be invoked to produce all types of numbers!

require './num_jenny'
# Only needed for last example
require 'creditcard'

n = NumJenny.new
puts n.range(:start_with => 0, :end_with => 10)

n.range(:start_with => 0, :end_with => 9999, :pad => true).each do |pin|
  puts "Attempting: #{pin}"
end

puts n.range(:start_with => 0, :end_with => 10, :random => true)

puts n.range(:start_with => 0, :end_with => 10, :pad => true, :random => true)

puts n.range(:start_with => 10, :end_with => 20, :pad => true)

n.range(:start_with => '1-493-555-0000', :end_with => '1-493-555-9999', :random => true).each do |phoneno|
  puts "ATDT#{phoneno}"
end

n.range(:start_with => "344543800000000", :end_with => "344543800999999", :random => true).each do |ccno|
  puts "Valid #{ccno.creditcard_type} Card Found: #{ccno}" if ccno.creditcard?
end


Cheers!

Thursday, June 14, 2012

Streamlining the Evolution of Application Development by Developing a Periodic Table of Classes


"The best designs are those that mimic life."

Remember the aforementioned statement, as it's critically important to reflect upon this statement as you read further.

Now, when we begin to imagine how the  building blocks of life interact and bond with one another at a sub-atomic >> atomic >> molecular level to form more self-aware and complex forms of life, it's easy to consider how those building blocks of life provide an excellent model to approach object oriented design within applications we write for ourselves and others.  "Phew, that was a long run-on sentence to consume..."  Run through it again.

Atoms, when bound with other atoms, produce various compounds (molecules).  For example, two Hydrogen atoms and one Oxygen atom when bound, form a single water molecule.  Now stay with me here, there's a point to all of this...that is if you haven't already started to understand why this is such a beautiful mindset to develop when designing applications.

When new programmers design code (and I've certainly been guilty of this as well), it's pretty common to write an application top-to-bottom...in one file.  Cool, it works...at least for the time being.  We do this because we have deadlines and we believe it's the fastest way to accomplish our goal or the goal of our employers.  It's time to think differently now that we're stepping back a bit further, thinking deeper, and really wrapping our minds around the bigger picture (all in the spirit of supporting maintainability, scalability, re-usability, interoperability, and my favorite collaboritive code to share with one another).  While we're here, let's ask ourselves...aren't we sick of copying and pasting code or worse yet, sick of writing the same code snippets over and over?  Me too.

An application can become harder and harder to manage over time as change presents itself.  Change is inevitable and this is especially true over longer periods of time - more folks depend on your application because hey, it makes their life easier right?  As a result, new requirements also present themselves - life never seems easy enough for most.  This is perfectly okay because it's this fundamental desire that can drive innovation and the progression within all of us.

Unfortunately, the top-to-bottom evolution process likely results in hundreds, if not thousands of lines of code all managed in a single file (or a small collections of files).  It's likely we can all agree that wrapping our minds around the logic contained within hundreds, if not thousands of lines of code in aggregate simply becomes...a huge pain in the ass.  In simpler terms this approach could be described as the, "Dodo Bird Approach" to development - a development design mindset that can't adapt to it's ever-changing and demanding environment - ultimately leading to its extinction.

Now, back to life's approach to design - particularly at the atomic/elemental level.  Looking at the periodic table of elements we start to notice a pattern - all elements/atoms contain certain properties that define how they behave.  For the context of this post, we will assert that the properties of an element/atom are synonymous to the methods/functions/subroutines found within a particular "elemental/atomic class."

What this means from the perspective of a developer is the need to create our own "periodic table of classes" - each with their own specific properties (public/private methods) small enough to share and use interchangeably to streamline the application development process.  The classes only focus on accomplishing a specific task and should only focus on that task.  An example of this may be an "element" designed to connect and interact with various types of databases.  We instantiate a collection of various "elemental/atomic classes" to create a complex organism (the application), capable of evolving in a manner that makes it more efficient through its evolution process.

In essence, when we think of object oriented design in this context we are actually thinking about how to design code in way that is meant to be shared, providing an opportunity to learn from one another.  This in turn promotes innovation, an efficient evolution process to support the ever-changing demands of our environment, and ultimately a means to present a better quality of life for one another.  Big things start really small and as a person smarter than Myself once said, simply put...the best designs are those that mimic life.

An excellent book around more design patterns is called, "Design Patterns: Elements of Reusable Object-Oriented Software"

I hope this mindset helps you as it's certainly helped me...

To see code using this design methodology check out this git repo:
https://github.com/ninp0/kore_kit

Cheers!


Thursday, May 31, 2012

Find all Plist Files in a Directory and Convert Contents into XML Files


Works on MacOSX Boxes:

#!/bin/bash
# Cheers!
find $1 -iname "*.plist" | while read f; do
  xml_file=`echo $f | sed "s/\.plist/\.xml/g"`
  echo "Found $f"

  echo -n "Converting to XML..."

  /usr/libexec/PlistBuddy -c 'Print' $f > $xml_file

  echo "complete."

  echo "XML file resides in: $xml_file"
done

Sunday, May 13, 2012

haxor_mailer.rb Class


# Send eMail from Many Different Mail Providers...cheers!

class HaxorMailer
  require 'pony'
  require 'highline/import'

  def populate_mail_password(user_name)
    mail_pass = HighLine.new.ask("#{user_name} password: ") {|q| q.echo = "\*" }
    return mail_pass.chomp
  end

  def pony_express(opts={})
    from = opts[:from]
    to = opts[:to]
    cc = opts[:cc]
    bcc = opts[:bcc]
    subject = opts[:subject]
    html_body = opts[:html_body]
    txt_body = opts[:txt_body] # If HTML is NOT supported or desired
    attachments_hash = {}
    opts[:attachments_hash].each do |attachment_name, attachment_path|
      attachments_hash[attachment_name] = File.binread(attachment_path)
    end

    debug = opts[:debug]

    address = opts[:address]
    port = opts[:port]
    user_name = opts[:user_name]
    if ! user_name.nil? && opts[:password].nil?
      password = self.populate_mail_password(user_name)
    else
      password = opts[:password]
    end

    if debug == true
      puts "DEBUG ENABLED: from=>#{from.inspect}, to=>#{to.inspect}, cc=>#{cc.inspect}, bcc=>#{bcc.inspect}, subject=>#{subject.inspect}, html_body=>#{html_body.inspect}, txt_body=>#{txt_body.inspect}, attachments=>#{attachments_hash.inspect}, address=>#{address.inspect}, port=>#{port.inspect}, user_name=>#{user_name.inspect}"
    end
    begin
      Pony.mail({
        :to => to,
        :cc => cc,
        :bcc => bcc,
        :from => from,
        :subject => subject,
        :html_body => html_body,
        :txt_body => txt_body,
        :attachments => attachments_hash,
        :via => :smtp,
        :via_options => {
          :address => address,
          :port => port,
          :enable_starttls_auto => true,
          :user_name => user_name,
          :password => password,
          :authentication => :plain,
          :domain => 'localhost.localdomain'
        }
      })
    rescue => e
      puts "ERROR!!! #{e.class} #{e} #{e.backtrace}"
    end
  end

  def gmail(opts={})
    opts[:address] = 'smtp.gmail.com'
    opts[:port] = 587
    self.pony_express(opts)
  end

  def hotmail(opts={})
    opts[:address] = 'smtp.live.com'
    opts[:port] = 587
    self.pony_express(opts)
  end

  def spoof(opts={})
    self.pony_express(opts)
  end

  def yahoo(opts={})
    opts[:address] = 'smtp.mail.yahoo.com'
    opts[:port] = 587
    self.pony_express(opts)
  end
end


Yeeesss! \o/ Here's how you instantiate the class:

require './haxor_mailer'
mail = HaxorMailer.new


# A quick way to use the supported/builtin methods to the class...
puts mail.gmail(:from => 'some.user@gmail.com', :to => 'some.other.user@some-email.com', :subject => 'Check this Out!', :html_body => '<!DOCTYPE HTML><html><head></head><body><div><a href="http://www.google.com">Google Rocks!</a></div></body></html>', :txt_body => 'Google Rocks! Navigate to: http://www.google.com with Your Internet Browser', :user_name => 'jake.hoopes@gmail.com', :attachments_hash => { 'mail1.txt' => './mail1.txt', 'mail2.txt' => './mail2.txt' }, :debug => true)

puts mail.hotmail(
:password=>'my_optional_passed_in_password', :from => 'some.user@hotmail.com', :to => 'some.other.user@some-email.com', :subject => 'Check this Out!', :html_body => '<!DOCTYPE HTML><html><head></head><body><div><a href="http://www.google.com">Google Rocks!</a></div></body></html>', :txt_body => 'Google Rocks! Navigate to: http://www.google.com with Your Internet Browser', :user_name => 'hoopes9@hotmail.com', :attachments_hash => { 'mail1.txt' => './mail1.txt', 'mail2.txt' => './mail2.txt' }, :debug => true)

puts mail.yahoo(:password=>'my_optional_passed_in_password', :from => '
some.user@yahoo.com', :to => 'some.other.user@some-email.com', :subject => 'Check this Out!', :html_body => '<!DOCTYPE HTML><html><head></head><body><div><a href="http://www.google.com">Google Rocks!</a></div></body></html>', :txt_body => 'Google Rocks! Navigate to: http://www.google.com with Your Internet Browser', :user_name => 'jake.hoopes@yahoo.com', :attachments_hash => { 'mail1.txt' => './mail1.txt', 'mail2.txt' => './mail2.txt' }, :debug => true)

puts mail.spoof(:from => 'spoof_mail_from@mailrelay.com', :to => '
some.other.user@some-email.com', :subject => 'Check this Out!', :html_body => '<!DOCTYPE HTML><html><head></head><body><div><a href="http://www.google.com">Google Rocks!</a></div></body></html>', :txt_body => 'Google Rocks! Navigate to: http://www.google.com with Your Internet Browser', :address => '127.0.0.1', :port => 25, :attachments_hash => { 'mail1.txt' => './mail1.txt', 'mail2.txt' => './mail2.txt' }, :debug => true)

Wednesday, March 28, 2012

Example: Simple Banner-Grabbing Modem Dialer Class Object (Using GNU Screen Wrapped in Ruby Goodness)


Here's an example of a class that can be leveraged to interact with modems in an effort to retrieve banners:

class AtHayesScreenDialer
  def initialize(screen_session_name, tty_dev_path, at_init_str="AT S7=45 S0=0 L1 V1 X4 &c1 E1 Q0")
    @screen_session_name = "#{screen_session_name}_#{rand(36**8).to_s(36)}"
    @screen_session_rc_file = "/tmp/screenrc_#{@screen_session_name}"
    @screen_session_log_file = "/tmp/screen_#{@screen_session_name}.log"
    @press_enter = sprintf("\r")
    @screen_cmd = "screen -p 0 -S #{@screen_session_name} -X stuff"

    begin
       # Setup GNU screen session's flushing of logfile buffer to be real-time
      File.open(@screen_session_rc_file, 'w') do |f|
        f.puts "logfile '#{@screen_session_log_file}'"
        f.puts "logfile flush 0"
      end
      # Custom rc file, log, and start in deteached mode with session name
      `screen -c #{@screen_session_rc_file} -L -d -m -S #{@screen_session_name} #{tty_dev_path}`
      sleep 3 # Let's wait a bit to ensure our screen session is ready...
      puts "\nIntializing Screen Session: #{@screen_session_name}"
      print "Sent '#{at_init_str}' Command..."
      `#{@screen_cmd} "#{at_init_str}#{@press_enter}"`
      sleep 1
      puts self.check_response
      return 0
    rescue => e
      puts "ERROR!!! #{e.class} #{e} #{e.backtrace}"
      self.close
      return 1
    end
  end

  def dump
    begin
      return File.read(@screen_session_log_file, :encoding=>"BINARY")
    rescue => e
      puts "ERROR!!! #{e.class} #{e} #{e.backtrace}"
      self.close
      return 1
    end
  end

  def close
    begin
      `screen -S #{@screen_session_name} -X quit`
      File.unlink(@screen_session_rc_file)
      File.unlink(@screen_session_log_file)
      return 0
    rescue => e
      puts "ERROR!!! #{e.class} #{e} #{e.backtrace}"
      return 1
    end
  end

  # Pass in 0 as param for key_delay_seconds if no key delay is desired...
  def press_key(key,times=1,key_delay_seconds=1)
    (1..times).each do
      printf("%s", key)
      sleep key_delay_seconds
    end
    return
  end

  def check_response
    serial_output = File.read(@screen_session_log_file, :encoding=>"BINARY")
    if serial_output.nil?
      response = nil
    else 
      response = serial_output.split(/\r\n/)[-1]
      unless response.nil?
        response = response.strip.chomp
      end
    end
    case response
      when "OK"
        return :OK
      when /CONNECT (9600|38400|115200)/
        return response
      when "ERROR"
        return :ERROR
      when /ATDT[0-9],[0-9]/
        return 0
      when "NO CARRIER"
        return :NO_CARRIER
      when "BUSY"
        return :BUSY
      when "VOICE"
        return :VOICE
    end
  end

  def command_mode
    begin
      `#{@screen_cmd} "+++"`
      sleep 1
      return 0
    rescue => e
      puts "ERROR!!! #{e.class} #{e} #{e.backtrace}"
      self.close
      return 1
    end
  end

  def data_mode
    begin
      `#{@screen_cmd} "ATO#{@press_enter}"`
      sleep 1
      return 0
    rescue => e
      puts "ERROR!!! #{e.class} #{e} #{e.backtrace}"
      self.close
      return 1
    end
  end

  def hangup
    begin
      print "\nHanging Up.  Sending 'ATH' Command..."
      `#{@screen_cmd} "ATH#{@press_enter}"`
      sleep 1
      puts self.check_response
      return 0
    rescue => e
      puts "ERROR!!! #{e.class} #{e} #{e.backtrace}"
      self.close
      return 1
    end
  end

  def reset
    begin
      print "\nHanging Up.  Sending 'ATH' Command..."
      `#{@screen_cmd} "ATZ#{@press_enter}"`
      sleep 1
      puts self.check_response
      return 0
    rescue => e
      puts "ERROR!!! #{e.class} #{e} #{e.backtrace}"
      self.close
      return 1
    end
  end

  def dial(phone_num, call_length_seconds, dial_prefix=nil, banner_grap=true)
    begin
      call_started = Time.now
      call_length_status = Time.now - call_started
      unless dial_prefix.nil?
        print "Dialing #{dial_prefix},#{phone_num} for a duration of #{call_length_seconds} seconds..."
        `#{@screen_cmd} "ATDT#{dial_prefix},#{phone_num}#{@press_enter}"`
      else
        print "Dialing #{phone_num} for a duration of #{call_length_seconds} seconds"
        `#{@screen_cmd} "ATDT#{phone_num}#{@press_enter}"`
      end
      until call_length_status > call_length_seconds
        current_response = self.check_response
        # If we get a CONNECT response, pass a few carriage returns to get a banner response...
        if current_response =~ /CONNECT (9600|38400|115200)/
          puts current_response
          `#{@screen_cmd} #{@press_enter}`
          sleep 1
          `#{@screen_cmd} #{@press_enter}`
          sleep 1
          `#{@screen_cmd} #{@press_enter}`
          `#{@screen_cmd} #{@press_enter}`
          `#{@screen_cmd} #{@press_enter}`
          sleep 3
          if banner_grap == true
            self.command_mode
            self.hangup 
            self.reset
            return 0
          #else
            #jack with the remote session
          end
        end
        print "."
        call_length_status = Time.now - call_started
        sleep 1
      end
      puts "CALL LENGTH MET (#{call_length_seconds} seconds)."
      self.command_mode
      self.hangup
      puts self.check_response
      return 0
    rescue => e
      puts "ERROR!!! #{e.class} #{e} #{e.backtrace}"
      self.close
      return 1
    end
  end
end

Friday, November 4, 2011

Colorize the Output of any Command from Bash!


# git clone git://github.com/ninp0/console_crayon.git
# gem install rainbow

I particularly like using it with tcpdump when monitoring pf in realtime via OpenBSD:

# cd console_crayon && ./console_crayon.rb -c 'tcpdump -nettt -i pflog0' -m 'pass,green,white|block,red,white'

but I'm sure you could use it for a million other things...enjoy!

Cheers!

Wednesday, October 12, 2011

Wiping Up the Web, One Tissue (i.e. Page) at a Time...Introducing, "hachoo.rb"

A helpful utility if you want to explore web pages in detail...should work with tor, socks, MITM proxies, etc.

* Features: Site automation, proxy support (including SOCKS), ability to change user agent, iframe support, etc...
*Coming soon: (crawling, bug-fixes, etc)

I welcome any suggestions for improvement.  Cheers!

Installation:

# git clone git://github.com/ninp0/hachoo.git
# gem install mechanize
# gem install addressable
# gem install socksify
# gem install rails
# cd hachoo
# ./hachoo.rb
Usage: ./hachoo.rb -u <uri> <optional_flags>
    -h, --help                       Help!
    -u, --uri URI                    Required: Target URI
    -P, --proxy-ip PROXY_IP          Optional: Proxy IP
    -p, --proxy-port PROXY_PORT      Optional: Proxy Port
    -S, --enable-socks-proxy         Optional: Soxy Proxy is Foxy ;)
    -a, --eval-all                   Optional: Evaluate All
    -b, --body-eval                  Optional: Evaluate Body Response
    -f, --forms-eval                 Optional: Evaluate Forms
    -l, --links-eval                 Optional: Evaluate Links
    -i, --images-eval                Optional: Evaluate Images
    -t, --title-eval                 Optional: Display Page Title
    -T, --timeout SECONDS            Optional: Timeout in Seconds
    -U, --user-agent AGENT           Optional: User Agent

Basic Example (Request will Timeout After 5 Seconds):
    ./hachoo.rb -u https://twitter.com/ninp0 -a -T 5

Intermediate Example:
    Start a MITM Proxy (e.g. BurpSuite, Paros, etc.)
        java -Xmx512m -jar burpsuite.jar
    Now perform a Request on the URL Below via the MITM Proxy:
        ./hachoo.rb -P '127.0.0.1' -p 8080 -u 'http://hang4r.blogspot.com'

Advanced Example:
    Start a SOCKS Server vis SSH:
        ssh -v -v -v -NCD 127.0.0.1:8443 user@remote_ssh_host
    Use hachoo.rb to Search for WordPress Sitesi
    through a SOCKS Proxy via Google Trickery:
        ./hachoo.rb -S -P '127.0.0.1' -p 8443 -u 'http://www.google.com/search?sclient=psy-ab&q=inurl:wp-content%20site:wordpress.org' -l

Kung-Fu Example:
    Use hachoo.rb to Follow ninp0 on Twitter via Pipe-Delimited Stacked Requests (Replace USERNAME & PASSWORD in Example Below):
    Despite the 404 Response for this Stacked Request ninp0 will be Followed...
        ./hachoo.rb -u 'https://mobile.twitter.com/login>>>submit>>>username=USERNAME&password=PASSWORD|https://mobile.twitter.com/ninp0>>>get|https://mobile.twitter.com/ninp0/follow>>>submit>>>last_url=/ninp0'