forked from ChrisTruncer/PenTestScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit used to trace 302 redirects
- Loading branch information
1 parent
61118be
commit 2a20a0a
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require 'net/http' | ||
require 'net/https' | ||
require 'uri' | ||
|
||
# The fetch function was based off of the function at the following URL | ||
# http://stackoverflow.com/questions/6934185/ruby-net-http-following-redirects | ||
|
||
def fetch(uri_str, url_list, limit = 10) | ||
# You should choose better exception. | ||
raise ArgumentError, 'HTTP redirect too deep' if limit == 0 | ||
|
||
uri = URI.parse(uri_str) | ||
|
||
if uri_str.start_with?("http://") | ||
# code came from - http://www.rubyinside.com/nethttp-cheat-sheet-2940.html | ||
http = Net::HTTP.new(uri.host, uri.port) | ||
request = Net::HTTP::Get.new(uri.request_uri) | ||
elsif uri_str.start_with?("https://") | ||
http = Net::HTTP.new(uri.host, uri.port) | ||
http.use_ssl = true | ||
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | ||
request = Net::HTTP::Get.new(uri.request_uri) | ||
end | ||
|
||
response = http.request(request) | ||
case response | ||
when Net::HTTPSuccess | ||
url_list.push("#{uri_str} <- Final URL") | ||
when Net::HTTPRedirection | ||
url_list.push("#{uri_str} redirects to...") | ||
fetch(response['location'], url_list, limit - 1) | ||
else | ||
response.error! | ||
end | ||
end | ||
|
||
# Check to make sure we have only one argument, the URL | ||
if ARGV.length != 1 | ||
puts "[*] Error: Please provide a URL to check for redirects!" | ||
puts "[*] Usage: ./WebTrace.rb <URL>" | ||
exit | ||
end | ||
|
||
# Check to make sure it's a valid URL | ||
if ARGV[0] =~ URI::regexp | ||
else | ||
puts "[*] Error: Please provide a valid URL!" | ||
puts "[*] Usage: ./WebTrace.rb <URL>" | ||
exit | ||
end | ||
|
||
# Array which will store all redirects | ||
all_urls = [] | ||
|
||
# Function that checks for redirects | ||
fetch(ARGV[0], all_urls) | ||
|
||
# If no redirects, say so. Otherwise, list all redirects | ||
if all_urls.length == 1 | ||
puts "No Redirection" | ||
else | ||
all_urls.each do |ind_url| | ||
puts ind_url | ||
end | ||
end |