For years now I’ve heard people reference API’s — sometimes this was in reference to libraries used with programming languages but really the kind I mean are web service API’s. These are the kind used by a developer/application to interact with a web service. I have never worked on a full blown web application so I haven’t used API’s one bit. Well, it’s about time — I am happy to report I have performed my first API call using Ruby!
I had the idea to write a script to check VirusTotal for a certain md5 (to see if it is a virus or malware) and then report the results back to me. As far as API calls go, I imagine this is basically as simple as it can get — which seemed like a perfectly acceptable place to start.
So I did some research and found the VirusTotal API 2.0 documentation. They provide some nice examples in Python to show how to interact with the API, no examples in Ruby but Python is close enough that it is pretty easy to get the gist of it. You need to sign up with the VirusTotal community in order to get a private API key so you can do do up to 4 API calls per minute. (Any more than that and you need to contact the VirusTotal folks to work out a special public key for you). The signup is quick and simple, and you have your private API-key in hand within minutes.
I knew I would need to install a few gems to get this operation underway, first since I knew from the VirusTotal API documentation that the message from their webservice comes in the form of JSON so I investigated a JSON gem and found that what I needed was installed as easily as : gem install json
I also knew I would need some kind of web client, a bit of googling and I learned that the easiest for me to use would be the Rest-Client gem. So I opened terminal and typed in gem install rest-client, and then we are all installed and ready to rock.
What I ended up with, was a Ruby script to ping the VirusTotal webservice for a certain md5 and to send back the VirusTotal report on whether or not any one of their 40+ antivirus programs detect that file as being malicious. All the hard-work is done by the libraries we are including, it’s really incredible how easy Ruby makes it to use other folks hard work. Here is what I ended up with for a basic md5 checker script for VT:
require 'rest-client'
api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
puts "What file would you like to evaluate?"
resource = gets
response = RestClient.post 'https://www.virustotal.com/vtapi/v2/file/report', { :resource => resource, :apikey => api_key }
results = JSON.parse(response)
puts results
For the sake of my own personal use I have blocked out my private API key from the script with all those X’s. In order to use the script for your self you will need to plug-in your own API key.
And that’s it! I never knew it was so easy to interact with webservice’s using Ruby. I will need to do a lot more investigating!
Until another time! Happy coding!
Posted: February 26th, 2012 under Ruby API Calls, Ruby Scripts.

