KUNAI Static Analyzer
Kunai is a library for doing static binary analysis of Dalvik.
Loading...
Searching...
No Matches
classes.hpp
1//--------------------------------------------------------------------*- C++ -*-
2// Kunai-static-analyzer: library for doing analysis of dalvik files
3// @author Farenain <kunai.static.analysis@gmail.com>
4// @author Ernesto Java <javaernesto@gmail.com>
5//
6// @file classes.hpp
7// @brief This is an important file, since classes manage all the magic
8// behind the DEX files. Classes will contain all the encoded data
9// (methods, fields, etc), as well as all the information about them
10// two Classes are important here: `ClassDefsStruct` and `ClassDataItem`.
11// `ClassDefsStruct` contains ids and offsets of the other information.
12// while `ClassDataItem` contains all the items.
13
14#ifndef KUNAI_DEX_PARSER_CLASSES_HPP
15#define KUNAI_DEX_PARSER_CLASSES_HPP
16
17#include "Kunai/DEX/parser/encoded.hpp"
18#include "Kunai/DEX/parser/annotations.hpp"
19
20#include <iostream>
21#include <vector>
22#include <unordered_map>
23
24namespace KUNAI
25{
26namespace DEX
27{
31 {
33 std::vector<encodedfield_t> static_fields;
34 std::unordered_map<std::uint64_t, EncodedField*> static_fields_by_id;
35
37 std::vector<encodedfield_t> instance_fields;
38 std::unordered_map<std::uint64_t, EncodedField*> instance_fields_by_id;
39
41 std::vector<encodedmethod_t> direct_methods;
42 std::unordered_map<std::uint64_t, EncodedMethod*> direct_methods_by_id;
43
45 std::vector<encodedmethod_t> virtual_methods;
46 std::unordered_map<std::uint64_t, EncodedMethod*> virtual_methods_by_id;
47
49 std::vector<EncodedField*> fields;
51 std::vector<EncodedMethod*> methods;
52
53
54 public:
56 ClassDataItem() = default;
58 ~ClassDataItem() = default;
59
66 stream::KunaiStream* stream,
67 Fields* fields,
68 Methods* methods,
69 Types* types
70 );
71
74 std::size_t get_number_of_static_fields() const
75 {
76 return static_fields.size();
77 }
78
82 {
83 return instance_fields.size();
84 }
85
88 std::size_t get_number_of_direct_methods() const
89 {
90 return direct_methods.size();
91 }
92
96 {
97 return virtual_methods.size();
98 }
99
104
109
114
119
124
129
134
139
142 std::vector<EncodedField*>& get_fields();
143
146 std::vector<EncodedMethod*>& get_methods();
147 };
148
152 {
153 public:
154#pragma pack(1)
157 {
158 std::uint32_t class_idx;
159 std::uint32_t access_flags;
160 std::uint32_t superclass_idx;
161 std::uint32_t interfaces_off;
162 std::uint32_t source_file_idx;
163 std::uint32_t annotations_off;
164 std::uint32_t class_data_off;
165 std::uint32_t static_values_off;
166 };
167#pragma pack()
168
169 private:
171 classdefstruct_t classdefstruct;
173 DVMClass* class_idx;
175 DVMClass* superclass_idx;
177 std::string source_file;
178
180 std::vector<DVMClass*> interfaces;
181
183 AnnotationDirectoryItem annotation_directory;
184
186 ClassDataItem class_data_item;
187
189 std::vector<encodedarray_t> static_values;
190 public:
192 ClassDef() = default;
194 ~ClassDef() = default;
195
204 Strings* strings,
205 Types* types,
206 Fields* fields,
207 Methods* methods);
208
214 {
215 return classdefstruct;
216 }
217
223 {
224 return classdefstruct;
225 }
226
230 {
231 return class_idx;
232 }
233
236 TYPES::access_flags get_access_flags() const
237 {
238 return static_cast<TYPES::access_flags>(classdefstruct.access_flags);
239 }
240
244 {
245 return superclass_idx;
246 }
247
250 const std::string& get_source_file() const
251 {
252 return source_file;
253 }
254
257 const std::vector<DVMClass*>& get_interfaces() const
258 {
259 return interfaces;
260 }
261
264 std::vector<DVMClass*>& get_interfaces()
265 {
266 return interfaces;
267 }
268
272 {
273 return class_data_item;
274 }
275
279 {
280 return class_data_item;
281 }
282
283
284 };
285
286 using classdef_t = std::unique_ptr<ClassDef>;
287
290 {
293 std::vector<classdef_t> class_defs;
295 std::uint32_t number_of_classes;
296 public:
298 Classes() = default;
300 ~Classes() = default;
310 stream::KunaiStream* stream,
311 std::uint32_t number_of_classes,
312 std::uint32_t offset,
313 Strings* strings,
314 Types* types,
315 Fields* fields,
316 Methods* methods
317 );
318
321 std::uint32_t get_number_of_classes() const
322 {
323 return number_of_classes;
324 }
325
328 const std::vector<classdef_t>& get_classdefs() const
329 {
330 return class_defs;
331 }
332
335 std::vector<classdef_t>& get_classdefs()
336 {
337 return class_defs;
338 }
339
340 friend std::ostream& operator<<(std::ostream& os, const Classes& entry);
341 };
342} // namespace DEX
343} // namespace KUNAI
344
345
346#endif
Class with all the previos annotations.
Definition annotations.hpp:130
Class that contains the fields and methods from a class in a DEX file.
Definition classes.hpp:31
void parse_class_data_item(stream::KunaiStream *stream, Fields *fields, Methods *methods, Types *types)
Method to parse the ClassDataItem.
ClassDataItem()=default
Constructor of ClassDataItem.
EncodedField * get_static_field_by_order(std::uint32_t ord)
Get a pointer to static field from the reading order (recommended)
~ClassDataItem()=default
Destructor of ClassDataItem.
EncodedMethod * get_direct_method_by_id(std::uint32_t id)
Get a direct method by the id of the MethodID.
EncodedField * get_instance_field_by_order(std::uint32_t ord)
Get an instance field from the reading order (recommended)
std::vector< EncodedField * > & get_fields()
Get all the fields from the class.
EncodedField * get_static_field_by_id(std::uint32_t id)
Get a pointer to static field by the id of the FieldID.
EncodedMethod * get_virtual_method_by_order(std::uint32_t ord)
Get a virtual method from the reading order (recommended)
std::size_t get_number_of_instance_fields() const
Get number of instance fields.
Definition classes.hpp:81
EncodedField * get_instance_field_by_id(std::uint32_t id)
Get an instance field by the id of the FieldID.
std::size_t get_number_of_direct_methods() const
Get number of direct methods.
Definition classes.hpp:88
std::size_t get_number_of_static_fields() const
Get the number of the static fields.
Definition classes.hpp:74
EncodedMethod * get_direct_method_by_order(std::uint32_t ord)
Get a direct method from the reading order (recommended)
std::size_t get_number_of_virtual_methods() const
Get number of virtual methods.
Definition classes.hpp:95
EncodedMethod * get_virtual_method_by_id(std::uint32_t id)
Get a virtual method by the id of the MethodID.
std::vector< EncodedMethod * > & get_methods()
Get all the methods from the class.
Definition of class with all the ids and offsets for all the other data.
Definition classes.hpp:152
const classdefstruct_t & get_class_def_struct() const
Get a constant reference to the classdefstruct_t of the class, this structure contains information ab...
Definition classes.hpp:213
void parse_class_def(stream::KunaiStream *stream, Strings *strings, Types *types, Fields *fields, Methods *methods)
Parse the current ClassDef for that we will parse the classdef_t structure, and then all the other fi...
std::vector< DVMClass * > & get_interfaces()
Get a reference to the vector with the interfaces implemented.
Definition classes.hpp:264
~ClassDef()=default
Destructor of ClassDef.
const std::string & get_source_file() const
Get a constant reference to the string with the source file.
Definition classes.hpp:250
DVMClass * get_superclass()
Get a pointer to the DVMClass of the super class of the current one.
Definition classes.hpp:243
classdefstruct_t & get_class_def_struct()
Get a reference to the classdefstruct_t of the class, this structure contains information about the c...
Definition classes.hpp:222
ClassDataItem & get_class_data_item()
Get a reference to the class data item.
Definition classes.hpp:278
TYPES::access_flags get_access_flags() const
Get the access flags of the current class.
Definition classes.hpp:236
ClassDef()=default
Constructor of ClassDef.
DVMClass * get_class_idx()
Get a pointer to the DVMClass of the current class.
Definition classes.hpp:229
const std::vector< DVMClass * > & get_interfaces() const
Get a constant reference to the vector with the interfaces implemented.
Definition classes.hpp:257
const ClassDataItem & get_class_data_item() const
Get a constant reference to the class data item.
Definition classes.hpp:271
All classes from the DEX files.
Definition classes.hpp:290
Classes()=default
Constructor from Classes.
std::vector< classdef_t > & get_classdefs()
Get a reference to all the class defs from the DEX.
Definition classes.hpp:335
void parse_classes(stream::KunaiStream *stream, std::uint32_t number_of_classes, std::uint32_t offset, Strings *strings, Types *types, Fields *fields, Methods *methods)
Parse all the classes from the DEX files.
std::uint32_t get_number_of_classes() const
Get the number of the classes from the DEX file.
Definition classes.hpp:321
~Classes()=default
Destructor from Classes.
const std::vector< classdef_t > & get_classdefs() const
Get a constant reference to all the class defs from the DEX.
Definition classes.hpp:328
Classes of the DVM.
Definition types.hpp:174
Class that represent field information it contains a FieldID and also the access flags.
Definition encoded.hpp:266
Class that represent the information from a Method.
Definition encoded.hpp:635
Fields will contain all the FieldID from the DEX file.
Definition fields.hpp:114
Methods contains all the MethodIDs from the DEX file.
Definition methods.hpp:118
Storage class for all the strings of the DEX file.
Definition strings.hpp:34
Definition types.hpp:305
Class to manage an input file stream given for the analysis.
Definition kunaistream.hpp:20
utilities
Definition analysis.hpp:23
Definition of offsets and IDs.
Definition classes.hpp:157
std::uint32_t static_values_off
offset to class data item
Definition classes.hpp:165
std::uint32_t class_data_off
debugging information and other data
Definition classes.hpp:164
std::uint32_t superclass_idx
flags for this class
Definition classes.hpp:160
std::uint32_t source_file_idx
interfaces implemented by class
Definition classes.hpp:162
std::uint32_t access_flags
idx for the current class
Definition classes.hpp:159
std::uint32_t annotations_off
idx to a string with source file
Definition classes.hpp:163
std::uint32_t interfaces_off
parent class id
Definition classes.hpp:161