-1
\$\begingroup\$

I have the following code that will skip certain URLs if needed:

SEARCH = "inurl:index.php?id="

def get_urls
  puts "Searching for possible SQL vulnerable sites, using search query #{SEARCH}"
  agent = Mechanize.new
  agent.user_agent = @user_agent
  page = agent.get('http://www.google.com/')
  google_form = page.form('f')
  google_form.q = "#{SEARCH}"
  url = agent.submit(google_form, google_form.buttons.first)
      url.links.each do |link|
        if link.href.to_s =~ /url.q/
          str = link.href.to_s
          str_list = str.split(%r{=|&})
          urls = str_list[1]
          next if str_list[1].split('/')[2] == 'webcache.googleusercontent.com'
          next if str_list[1].split('/')[2] == 'search.clearch.org'
          next if str_list[1].split('/')[2] == 'duckfm.net'
          next if str_list[1].split('/')[2] == 'search1.speedbit.com'
          next if str_list[1].split('/')[2] == 'yoursearch.me'
          next if str_list[1].split('/')[2] == 'search.speedbit.com'
          next if str_list[1].split('/')[1] == 'ads/preferences?hl=en'
          next if str_list[1].split('/')[2] == 'www.sa-k.net'
          next if str_list[1].split('/')[2] == 'github.com'
          next if str_list[1].split('/')[2] == 'stackoverflow.com'
          urls_to_log = URI.decode(urls)
          puts "Site found: #{urls_to_log}"
          sleep(1)
          File.open("#{PATH}/SQL_sites_to_check.txt", 'a+') { |s| s.puts("#{urls_to_log}'") }
        end
      end
  puts "Possible vulnerable sites dumped into #{PATH}/SQL_sites_to_check.txt"
end

Is there a better more readable, less smelly, way to write the next if's?

\$\endgroup\$
4
  • 2
    \$\begingroup\$ instead of str_list[1].split('/')[2] 100 times, just assign str_list[1].split('/')[2] to a variable. \$\endgroup\$ Apr 7, 2016 at 18:16
  • \$\begingroup\$ @BlueEyedBehemoth But then I'd still have to use the variable 100 times. \$\endgroup\$
    – 13aal
    Apr 7, 2016 at 18:23
  • 1
    \$\begingroup\$ that's true, but you won't be executing a split that many times. You can also create an array of websites and just make sure that the variable(your URL) isn't in that array. Unfortunately I don't know ruby so I can't give you a complete answer. \$\endgroup\$ Apr 7, 2016 at 19:25
  • \$\begingroup\$ @BlueEyedBehemoth Well, I guess I'll just stick with it as is for now, and figure something out, thanks for the ideas! \$\endgroup\$
    – 13aal
    Apr 7, 2016 at 19:39

1 Answer 1

2
\$\begingroup\$

You seem to be referring to the same information str_list[1].split('/')[2] several times. I highly suggest you simply do this one type and assign it to a variable so that your list can go

next if url == "github.com"
next if url == "google.com"

Not only is this clearer, but it should save time because splits are probably not the thing you wanna do 16 times in a loop.

Also right at the top you have urls = str_list[1] but then you don't use that variable??

\$\endgroup\$
7
  • \$\begingroup\$ Yeah, totally forgot the url variable while I was writing this haha. Should I save them to a variable or an array? \$\endgroup\$
    – 13aal
    Apr 7, 2016 at 18:11
  • \$\begingroup\$ Also if I didn't do the splits, I'd have to add the http:// to the beginning of the URL wouldn't I? \$\endgroup\$
    – 13aal
    Apr 7, 2016 at 18:16
  • 1
    \$\begingroup\$ And figure out if it was https or http \$\endgroup\$ Apr 7, 2016 at 18:17
  • \$\begingroup\$ You should probably be using a regular expression then \$\endgroup\$
    – Tom
    Apr 7, 2016 at 22:08
  • \$\begingroup\$ @Tom Elaborate please? \$\endgroup\$
    – 13aal
    Apr 8, 2016 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.