SecureBrain / ruby_apk

analyzing android apk library for ruby
MIT License
83 stars 52 forks source link

Check if apk is signed #14

Closed DavidRagone closed 11 years ago

DavidRagone commented 11 years ago

Is there a way in ruby_apk to see if the apk is properly signed (or signed at all)?

masatanish commented 11 years ago

There is no method about sign and certificate in ruby_apk for now.

You can get signs or certificate by using below sample. Does it solve your problem? I'll merge it into next version's ruby_apk, if you wish it.

require 'ruby_apk'
require 'openssl'

module Android
  class Apk
    # @return [Hash] key: sign file path, value: OpenSSL::PKCS7 instance
    def signs
      signs = {}
      self.each_file do |path, data|
        # find META-INF/xxx.{RSA|DSA}
        next unless path =~ /^META-INF\// && data.unpack("CC") == [0x30, 0x82]
        signs[path] = OpenSSL::PKCS7.new(data)
      end
      signs
    end

    # @return [Array<String, OpenSSL::X509::Certificate>] certificate that is found at first
    def certificate
      path, pkcs = signs.first
      return path, pkcs.certificates.first
    end
  end
end
apk_path = ARGV[0]
apk = Android::Apk.new(apk_path)

path, cert = apk.certificate
puts "path: #{path}"
puts "subject: #{cert.subject}"
puts "issuer: #{cert.issuer}"
puts "not before: #{cert.not_before}"
puts "not after: #{cert.not_after}"
masatanish commented 11 years ago

I will add methods for sign and certificate to ruby_apk.

DavidRagone commented 11 years ago

Awesome - looking forward to it. Let me know if I can help (happy to do a code review or try out what you come up with).

masatanish commented 11 years ago

I've implemented Apk#signs and Apk#certificates. Apk#certificates method has little difference from above sample code. Please refer usage on README file.