Friday, September 21, 2012

Having fun with yara and ruby

YARA is a tool aimed at helping malware researchers to identify and classify malware samples. With YARA you can create descriptions of malware families based on textual or binary patterns contained on samples of those families. Each description consists of a set of strings and a Boolean expression which determines its logic. (Source:


Initiate the installation process by installing ruby, gems, and pcre
yum install ruby ruby-devel rubygems pcre pcre-devel

Download and install YARA
tar -xvf yara-1.6.tar.gz
cd yara-1.6
sudo make install

Install yara-ruby
gem install yara

Note: If you got an error such as "yada yada cannot open shared object file: No such file or directory yada yada" when you try to run your yara ruby application, you should add the path /usr/local/lib to the loader configuration file
sudo echo "/usr/local/lib" >> /etc/
sudo ldconfig


This is a sample code from yara-ruby to test the PE rule.
#!/usr/bin/env ruby
# Usage example:
#   ruby ispe.rb /win_c/windows/system32/*.???
#    yara-ruby - Ruby bindings for the yara malware analysis library.
#    Eric Monti
#    Copyright (C) 2011 Trustwave Holdings
#    This program is free software: you can redistribute it and/or modify it
#    under the terms of the GNU General Public License as published by the
#    Free Software Foundation, either version 3 of the License, or (at your
#    option) any later version.
#    This program is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
#    or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
#    for more details.
#    You should have received a copy of the GNU General Public License along
#    with this program. If not, see <>.

$: << File.join(File.dirname(__FILE__), '..', 'lib')
require 'rubygems'
require 'yara'

ctx =

ctx.compile_string "rule IsPE { condition: uint16(0) == 0x5A4D and uint32(uint32(0x3C)) == 0x00004550 }"

ARGV.each do |fname|
  ctx.scan_file(fname).each {|match| puts ">> #{fname} matched #{match.rule} rule" }


Below are the sample shellcode that we'll use to test the above PE rule. You can convert the shellcode to exe at this website,


With the code and shellcode above, we can test the IsPE rule as a proof-of-concept on how brilliant YARA detect a file based on a rule given
[azizan@lab69 yara]# ruby test.rb shellcode.exe_
>> shellcode.exe_ matched IsPE rule
[azizan@lab69 yara]#


No comments:

Post a Comment