frida modify static param as ArrayList - frida

public class OBDSportsModelManager {
public static ArrayList<DataArray> mDiagnosisCommand;
public boolean getData() {
mDiagnosisCommand = new ArrayList<>();
for (String dataArray : this.commandIDs) {
mDiagnosisCommand.add(new DataArray(dataArray));
}
return true;
}
}
I want to add some more item to the 'mDiagnosisCommand',
by using this code:
sports.getData.implementation = function(){
Log.v("hook-sports", "try to add obd commands!");
var ret = this.getData();
var DataArray = Java.use("com.obd2.comm.DataArray");
var items = DataArray.$new("0x00,0x00,0x00,0x00,0x00,0x42");
this.mDiagnosisCommand.add(items); // not working!!!
Log.v("hook-sports", "hook done!");
return ret;
}
but doesn't works well.
I googled frida ArrayList add items without any help.

You have two problems:
You are using this.mDiagnosisCommand but the field is a static field, therefore it belongs to the class OBDSportsModelManager and not to the class instance this.
By calling this.mDiagnosisCommand you only get the Frida object representing this field, not the field value itself. If you want the ArrayList referenced by a field you have to add .value.
Considering both problems the following lines should work (after correcting the class name):
// correct the class name in the next line
var cls = Java.use("<full.name.to>.OBDSportsModelManager");
cls.mDiagnosisCommand.value.add(items);

Related

Dart Hive TypeAdapter rational for write() method

I am trying to understand the rational behind using writeByte(3) in the write method in Hive TypeAdapter.
Please consider the class:
#HiveType()
class Person{
#HiveField(0)
String name;
#HiveField(1)
int age;
}
In the TypeAdapter below It is easy to understand the read method, since it is just reads sequentially each field.
However, I'm trying to figure out why the same mechanism does not apply to the write, instead of using ..writeByte(...) just before each field. And, what is the meaning of the first ..writeByte(2)?
class PersonAdapter extends TypeAdapter<Person> {
#override
Person read(BinaryReader reader) {
var numOfFields = reader.readByte();
var fields = <int, dynamic>{
for (var i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return Trips()
..name = fields[0] as String
..age = fields[1] as int;
}
#override
void write(BinaryWriter writer, Person obj) {
writer
..writeByte(2) // Why this here? (sometimes I see writeByte(3) !! )
..writeByte(0)
..write(obj.name)
..writeByte(1)
..write(obj.age);
}
}
Thanks for any clarification.
I know nothing about Hive but if you take a look at the builder which create this write method you can see the following:
String buildWrite() {
var code = StringBuffer();
code.writeln('writer');
code.writeln('..writeByte(${getters.length})');
for (var field in getters) {
var value = _convertIterable(field.type, 'obj.${field.name}');
code.writeln('''
..writeByte(${field.index})
..write($value)''');
}
code.writeln(';');
return code.toString();
}
https://github.com/hivedb/hive/blob/59ad5403593283233d922f62f76832c64fa33a3b/hive_generator/lib/src/class_builder.dart#L122
So based on this we can conclude the first writeByte is the length of getters. The next one is the index of the first getter (0) following by the value and next getter (1) with value and so on.
This makes sense since the protocol properly needs to know how many fields it can expect to get.

Invalid constructor name

bbeginner question here:
I have a class like this
class Data {
String name;
String imgUrl;
Data(this.name, this.imgUrl);
}
and I'm trying to create a list like this
var list = new List<Data>();
var data = new Data("caca", "toto");
list.add(data);
But I get an error saying invalid constructor name.
What I'm I doing wrong here?
Thanks for your help
My guess is that your code is not inside a function. Something like:
class Something {
var list = new List<Data>();
var data = new Data("caca", "toto");
list.add(data);
}
Maybe it's due to one-too-many end braces just above the code, or some other syntactic typo.
Ensure that your code is actually inside the body of a function, not just directly inside the class.
class Something {
void tutu() {
...
var list = new List<Data>();
var data = new Data("caca", "toto");
list.add(data);
... use list ...
}
}

Instantiate a class in MVC 5 gives error [class] does not contain a definition for [member] and no extension method [member] ... could be found

I have a class defined in a model:
public class OfficeData
{
public int ReportID;
public DateTime? LastReported;
public string ReportedBy;
}
and this method to instantiate the class in the model:
public IEnumerable<OfficeData> GetOfficeData(int OfficeId)
{
MyContext db = new MyContext();
var officeData = db.Database.SqlQuery<OfficeData>("get_data #OfficeID", new SqlParameter("OfficeID", OfficeId)).Select(data => new OfficeData { ReportID = data.ReportID, LastReported = data.LastReported, ReportedBy = data.ReportedBy};
return officeData;
}
and I am calling the method in a controller:
public ActionResult Create()
{
OfficeModel officeModel = new OfficeModel;
...
MyServiceLayer serv = new MyServiceLayer; // the service layer class holds the method
var officeData = serv.GetOfficeData(officeModel.OfficeID);
officeModel.ReportID = officeData.ReportID;
officeModel.LastReported = officeData.LastReported;
officeModel.ReportedBy = officeData.ReportedBy;
...
}
This is my first project in MVC and I'd like to understand what's going on here. Thanks in advance for any help.
GetOfficeData returns an IEnumerable<OfficeData>:
public IEnumerable<OfficeData> GetOfficeData(int OfficeId)
so the inferred type of officeData is also IEnumerable<OfficeData>. You can see this if you hover over the var keyword (or officeData, I forget which you have to hover over).
var officeData = serv.GetOfficeData(officeModel.OfficeID);
So the compiler is complaining that IEnumerable<OfficeData>.ReportID doesn't exist. Which it most likely doesn't. Did you mean to call FirstOrDefault() or possibly enumerate through the collection via a call to Select or a loop?

Get private variable by reflection in dart

I would like to get private variable in an object in dart.
This variable has no getter so I want to do this with reflection.
I try many way but nothing works to me.
For exemple, when I do this:
var reflection = reflect(this);
InstanceMirror field = reflection.getField(new Symbol(fieldName));
I get an error:
No getter for fieldName.
It's normal because the variable hasn't getter.
How can I get this variable ?
EDIT with a test code:
Here is my reflect test (test variable is a reflectClass(MyClass))
reflectClass(Injector).declarations.keys.forEach((e) => test.getField(e, test.type.owner))
I get this error:
Class '_LocalInstanceMirror' has no instance method 'getField' with
matching arguments.
If I do this:
reflectClass(Injector).declarations.keys.forEach((e) => test.getField(e))
I get:
Class 'DynamicInjector' has no instance getter
'_PRIMITIVE_TYPES#0x1b5a3f8d'.
Same thing with values of declarations.
The error message you got is actually correct. The class has a getter for this field.
Dart implicitly creates getters for all and setters for all non-final/non-const fields.
It seems access to private members isn't yet supported in Dart2JS.
see https://code.google.com/p/dart/issues/detail?id=13881
Here an example how to access private fields:
(example from https://code.google.com/p/dart/issues/detail?id=16773)
import 'dart:mirrors';
class ClassWithPrivateField {
String _privateField;
}
void main() {
ClassMirror classM = reflectClass(ClassWithPrivateField);
Symbol privateFieldSymbol;
Symbol constructorSymbol;
for (DeclarationMirror declaration in classM.declarations.values) {
if (declaration is VariableMirror) {
privateFieldSymbol = declaration.simpleName;
} else if (declaration is MethodMirror && declaration.isConstructor) {
constructorSymbol = declaration.constructorName;
}
}
// it is not necessary to create the instance using reflection to be able to
// access its members with reflection
InstanceMirror instance = classM.newInstance(constructorSymbol, []);
// var s = new Symbol('_privateField'); // doesn't work for private fields
// to create a symbol for a private field you need the library
// if the class is in the main library
// var s = MirrorSystem.getSymbol('_privateField', currentMirrorSystem().isolate.rootLibrary);
// or simpler
// var s = MirrorSystem.getSymbol('_privateField', instance.type.owner);
for (var i=0; i<1000; ++i) {
instance.setField(privateFieldSymbol, 'test');
print('Iteration ${instance.getField(privateFieldSymbol)}');
}
}
using dson or serializable you can do it in next way:
library example_lib;
import 'package:dson/dson.dart';
// this should be the name of your file
part 'example.g.dart';
#serializable
class Example extends _$ExampleSerializable {
var _privateVar;
}
main() {
var example = new Example();
example['_privateVar'] = 'some value';
print('example._privateVar: ${example._privateVar}');
print('example["_privateVar"]: ${example["_privateVar']}");
}

how to create a new class instance object dynamically

I have a class
class Account extends Stuff{
String name;
newObject(){
return new Account();
}
}
inside the Stuff class I have a method
//generates list of objects of the same type
//as given object and fills attribute
generateObjectsFromExisting(names)
{
List list = new List();
InstanceMirror instanceMirror = reflect(this);
Symbol formatSymbol = new Symbol("newObject");
for(var name in names){
//calles newObject function from this and returns a new object
var newInstanceObject = instanceMirror.invoke(formatSymbol, []);
Symbol symbol = new Symbol("name");
InstanceMirror field = newInstanceObject.setField(symbol,name);
list.add(newInstanceObject.reflectee)
}
return list;
}
so when writing
main(){
var account = new Account();
List accounts = new List();
accounts = account.generateObjectsFromExisting(['tim','tom']);
print(account.name) // returns null
print(accounts[0].name) // returns tim
print(accounts[1].name) // returns tom
}
the problems with this way are
1 'generateObjectsFromExisting()' is on the 'account' object and not on Account
2 I have to manually add the "newObject" Method to every single class I implement.
I would prefer a static Method like 'Account.generateObjectsFromExisting()'
but how to to access 'this' (since its not available in static)
so I can say "this.new()" or something equivalent to "new Account();" eg "new this();"
and therefor be able to only have one 'newObject' function inside Stuff or maybe wont need it at all.
so now my code would look like this
class Account extends Stuff{
String name;
}
in Stuff
static generateObjectsFromExisting(names)
{
List list = new List();
for(var name in names){
var object = new this();
object.name = name;
list.add(object)
}
return list;
}
in main
main(){
// returns list of Accounts filled with names
accounts = Account.generateObjectsFromExisting(['tim','tom']);
print(accounts[0].name) // returns tim
print(accounts[1].name) // returns tom
}
if you can show me a way to access the Class to do something like this.new(); or new this(); then obviously the class 'Account' needs to be accessed and not the extended 'Stuff'
if the 'this' approach is not possible, then maybe you can show me a way how to access the Class from within an already existing object
like
generateObjectsFromExisting(names)
{
List list = new List();
var class = this.class;
var newObject = class.new():
...
}
or is my current approach the only solution. .. hope not :)
thank you
There are two ways I can think of at the moment. But both of them are pretty close to your initial solution as they both use reflection..
The non-static solution:
class Stuff {
generateObjectsFromExisting(List<String> names) {
var cm = reflectClass(this.runtimeType);
return names.map((name) {
var newInstance = cm.newInstance(const Symbol(''), []).reflectee;
newInstance.name = name;
return newInstance;
}).toList();
}
}
The static solution:
class Stuff {
static generateObjectsFromExisting(type, List<String> names) {
var cm = reflectClass(type);
return names.map((name) {
var newInstance = cm.newInstance(const Symbol(''), []).reflectee;
newInstance.name = name;
return newInstance;
}).toList();
}
}
You would call the static solution like this:
var accounts = Stuff.generateObjectsFromExisting(Account, ['tim', 'tom']);
There might be another solution involving factory constructors but can't think of any right now. Also, this code would easily break when you get another subclass of Stuff that does not have a name attribute. I don't know if you really intended on putting that attribute on Account instead of Stuff.
Also answering you 'Class'-Question. There is no class in Dart, there is only the Type and to get it you can do:
Type type1 = Account;
Type type2 = account.runtimeType;
But the Type doesn't have any methods you could use to create a new instance.

Resources