Class: Decompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/decompiler/decompiler.rb

Overview

This class uses apktool and dex2jar to perform the de-compilation of APKs and outputs the results into their respective directories

Class Method Summary collapse

Class Method Details

.convert_apk_to_jarObject

This method takes the file name of the .apk and converts it to a .jar



73
74
75
# File 'lib/decompiler/decompiler.rb', line 73

def self.convert_apk_to_jar
	file_name = File.basename(@apk).sub(/.apk/, ".jar")
end

.convert_apkname_to_underscoreObject

This method converts periods “.” within filenames to underscore



67
68
69
# File 'lib/decompiler/decompiler.rb', line 67

def self.convert_apkname_to_underscore
	file_name = File.basename(@apk).underscore.tr(".", "_")
end

.executeObject

This method executes the run_apktool and run_dex2jar methods



61
62
63
64
# File 'lib/decompiler/decompiler.rb', line 61

def self.execute
	run_apktool
	run_dex2jar
end

.exists(data_type, entity) ⇒ Object

This method validates that the files and directories being referenced exist



32
33
34
35
36
37
38
39
40
# File 'lib/decompiler/decompiler.rb', line 32

def self.exists(data_type, entity)  
	val = instance_variable_get ("@#{entity}".underscore)
	case data_type
	when "dir"
		Dir.exists?(val)
	when "file"
		File.exists?(val)
	end 
end

.my_hashObject

This method creates a hash of the 4 inputs for the tool



6
7
8
9
10
11
12
13
# File 'lib/decompiler/decompiler.rb', line 6

def self.my_hash
 {
"apk" => "file",
"apktool" => "file",
"output directory" => "dir",
"d2j-dex2jar .sh or .bat" => "file"
}
end

.runObject

This method creates instance variable @k and fills up the array my_hash with the 4 values its expecting



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/decompiler/decompiler.rb', line 17

def self.run
 my_hash.each do |k,v|
		puts "Please specify the absolute location of the #{k}"
		instance_variable_set("@#{k}".underscore, gets.chomp)
unless self.exists(v,k)
	redo
end
 end
 execute
 rescue ::Interrupt
 	puts "\nGoodbye"
end

.run_apktoolObject

This method runs apktool and outputs the results into its own directory



79
80
81
# File 'lib/decompiler/decompiler.rb', line 79

def self.run_apktool
 	system "java", "-jar", @apktool, "d", "-f", @apk, "#{@output_directory}/apktool_output/#{self.convert_apkname_to_underscore}" 
end

.run_dex2jarObject

This method executes dex2jar and outputs the file under its own dex2jar directory



84
85
86
87
88
# File 'lib/decompiler/decompiler.rb', line 84

def self.run_dex2jar
   	path = "#{@output_directory}/dex2jar_output"
   	Dir.mkdir path if not Dir.exist? path	
   	system @d2j_dex2jar_sh_or_bat, @apk, "-f", "-o", "#{path}/#{convert_apk_to_jar}" 
end

.set_apk_var(apk_loc = "") ⇒ Object

This method defines the instance variable @apk as the location

of the APK file that the user has provided.



54
55
56
57
58
# File 'lib/decompiler/decompiler.rb', line 54

def self.set_apk_var(apk_loc = "")
  raise "NotAValidApkLocationValue" if !(apk_loc.kind_of?(String))
  raise "NoAPKProvidedFromConfigFile" if apk_loc.empty?
  @apk = apk_loc
end

.set_static_vars(opts = {}) ⇒ Object

This method has the tool revert to having the user manually enter the 4 parameters needed to run the tool in the case the YAML config file does not exist



45
46
47
48
49
50
# File 'lib/decompiler/decompiler.rb', line 45

def self.set_static_vars(opts={})
  raise "NoOptionsProvidedFromConfigFile" if opts.empty?
  @apktool = opts[:apktool]
  @output_directory = opts[:output_directory]
  @d2j_dex2jar_sh_or_bat = opts[:d2j]
end