#!/usr/bin/env ruby

$:.unshift("/msf3/lib")
require "rex"

host = "go.microsoft.com"
host_ip = "207.46.250.101"

urlb = "/fwlink/?LinkId="

s_beg = ARGV.shift() || '0'
s_end = ARGV.shift() || '99999'

s_beg = s_beg.to_i
s_end = s_end.to_i

tcnt  = 10
lidx  = s_beg

while (lidx < s_end)
	t = []
	1.upto(tcnt) do |i|
		break if lidx >= s_end
		t << Thread.new do 
			url  = urlb + ("%.8d" % lidx.to_s)
			rcnt = 0
			
			begin	
				s = Rex::Socket.create_tcp(
					'PeerHost' => host_ip,
					'PeerPort' => 80
				)

				r = "GET #{url} HTTP/1.1\r\nHost: #{host}\r\n\r\n"
				s.put(r)
				d = s.get_once
				s.close

				if (d =~ /^Location:\s*([^\r]+)\r\n/ms)
					$stdout.puts "http://#{host}#{url}\t#{$1}"
					$stdout.flush
				end
				
			rescue ::Rex::ConnectionTimeout => e
				if (rcnt < 5)
					$stdout.puts "http://#{host}#{url}\tERROR: #{e.class} #{e.to_s} (RETRY ##{rcnt})"				
					rcnt += 1
					select(nil, nil, nil, 2.25 + (rcnt * 2) + (rand(100) * 0.001))
					retry
				end
				$stdout.puts "http://#{host}#{url}\tERROR: #{e.class} #{e.to_s} TERMINAL"
				
			rescue ::Interrupt => e
				exit(0)
				
			rescue ::Exception => e
				$stdout.puts "http://#{host}#{url}\tERROR: #{e.class} #{e.to_s} UNKNOWN"
			end
		end
		lidx += 1
	end
	
	# Wait on threads
	t.each do |i|
		t.join
	end
end

