Created
April 23, 2015 15:10
-
-
Save dguido/153dc5db306afde1ad2e to your computer and use it in GitHub Desktop.
My first Ruby script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'rubygems' | |
require 'nokogiri' | |
require 'open-uri' | |
require 'uri' | |
# Chrome on Win7 | |
USERAGENT= "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.195.10 Safari/532.0" | |
#todo | |
# || and ||= to clean up nil checking code | |
# #{@name} to clean up .to_s | |
# mechanize has user-agent aliases | |
# This was my first Ruby script ever. It attempts to identify | |
# Wordpress versions from a variety of sources. It was used to generate | |
# statistics about the percentage of Security Twits that regularly | |
# follow their own advice and patch. | |
class Blog | |
attr_accessor :name, :major, :minor, :revision | |
def initialize(name=nil, major=nil, minor=nil, revision=nil) | |
if name == nil then @name = "unknown" | |
else @name = name end | |
if major == nil then @major = 0 | |
else @major = major end | |
if minor == nil then @minor = 0 | |
else @minor = minor end | |
if revision == nil then @revision = 0 | |
else @revision = revision end | |
end | |
def to_s | |
if @name === "WordPress" | |
@name.to_s + " " + @major.to_s + "." + @minor.to_s + "." + @revision.to_s | |
else | |
@name | |
end | |
end | |
def version() | |
@major.to_s + "." + @minor.to_s + "." + @revision.to_s | |
end | |
# if its not one of these values, chances are its modified | |
def legit() | |
if @major === 2 && (0..9) === @minor && (0..9) === @revision | |
return true #legit | |
else | |
return false #not legit | |
end | |
end | |
#def <, >, ==, etc | |
end | |
def use_metatag(url) | |
begin | |
html = open(url, "User-Agent" => USERAGENT) | |
rescue | |
return Blog.new("the website is down") | |
rescue Timeout::Error | |
return Blog.new("the website is down") | |
end | |
doc = Nokogiri::HTML(html) | |
node = doc.search('//meta[@name=\'generator\']') | |
if node.empty? #no meta generator tag | |
return Blog.new("unknown") | |
else #we got a meta tag | |
version_string = node.attr('content') | |
if version_string === "WordPress.com" #it said wordpress.com | |
return Blog.new("WordPress.com") | |
else | |
(text, num) = version_string.split(' ') #try splitting it | |
if text === "WordPress" && num != nil | |
return Blog.new(text, *num.split('.').map { |s| s.to_i } ) #it said wordpress | |
else | |
return Blog.new(version_string) #it said something else | |
end | |
end | |
end | |
end | |
def use_readme(url) | |
begin | |
doc = Nokogiri::HTML(open(url + "/readme.html", "User-Agent" => USERAGENT)) | |
rescue | |
return Blog.new("readme removed") | |
rescue Timeout::Error | |
return Blog.new("timeout") | |
end | |
node = doc.search('//h1[@id=\'logo\']') | |
if node.empty? | |
return Blog.new("readme modified") | |
end | |
(text, num) = node.inner_text.strip().split() # "Version 2.8" | |
return Blog.new("WordPress", *num.split('.').map { |s| s.to_i } ) | |
end | |
#def wp_version(url="http://192.168.1.103/wordpress-2.8.1/") | |
#metatag_says = use_metatag(url) | |
#puts metatag_says | |
#if metatag_says.name != "WordPress" | |
# return metatag_says #stop here | |
#else | |
# readme_says = use_readme(url) | |
#puts "Got " + metatag_says.to_s + " from meta tag" | |
#puts "Got " + readme_says.to_s + " from readme.html" | |
#puts "Major: " + readme_says.major.to_s | |
#puts "Minor: " + readme_says.minor.to_s | |
#puts "Revision: " + readme_says.revision.to_s | |
#if metatag_says.legit() and metatag_says.version === readme_says.version | |
# #puts "both answers match" | |
# return metatag_says | |
#elsif readme_says.legit() | |
# #puts "readme is legit" | |
# return readme_says | |
#else | |
# #puts "readme is not legit" | |
# return metatag_says | |
#end | |
#end | |
#end | |
#if ARGV[0] then | |
#puts wp_version(ARGV[0]) | |
#else | |
doc = Nokogiri::HTML(open("http://www.security-twits.com/")) | |
links = doc.css('a').map { |link| link['href'] } | |
for link in links | |
if link.chomp().empty? or link =~ /twitter/i or link =~ /freenode/ | |
links.delete(link) | |
end | |
end | |
puts links.count.to_s + " blogs to identify" | |
puts "url,metatag,readme" | |
for link in links | |
puts link + "," + use_metatag(link).to_s + "," + use_readme(link).to_s | |
end | |
#end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment