Is there a way to access "this" in Frida? - frida

Let's say I have the following hook:
Java.perform(function() {
var test = Java.use('hello.world');
test.test2.overload('int').implementation = function(arg0) {
console.log(this);
return this.test(arg0);
}
It gives me
{$handle: '0x2345', '$weakRef': 283}
But I know it has a field that I want to access.
How do I access this?

Java.cast(this, Java.use('com.whatever.class')).fieldName.value
You can also get class name from .$className

Related

Swift - Using a closure inside a repeat loop

I am trying to implement a user registration system and I need to know whether a user id (generated randomly) has already been assigned to another user or not. To do so, I connect my Firebase database and use the observer() method to check to availability of the generated id.
However, since the Firebase database query runs asynchronously and I can only know the result once the query returns, I am not able to use the correct return value from within the calling method.
My approach here is
repeat {
id = generateUniqueId()
check availability
} while (id is not unique)
The implementation I have is
var id:String
var test = true
repeat {
id = generateId()
ref.child("\(databaseReferenceName)").observe(.value) { (snapshot) in
test = snapshot.hasChild("\(id)")
}
} while (test == true)
This loop keeps running even though the test variable is set to false by the hasChild() method.
How can I change the code so that I would be able to capture the right value of the test variable?
I am using Swift 4.1
Thanks
You don't say, but I'm assuming the observe() function you're using is asynchronous, and will return before the work is done. As PaulW11 says, you need to trigger the next round in the completion handler:
func observeChildren() {
var id:String
id = generateId()
ref.child("\(databaseReferenceName)").observe(.value) { (snapshot) in
if snapshot.hasChild("\(id)") {
observeChildren() //recursively call observeChildren again.
}
}
Since the process is asynchronous the loop will run until hit the first test = false , but i think you need this recursive way until find an available id
func checkID() {
id = generateId()
ref.child("\(databaseReferenceName)").observeSingleEvent(.value) { (snapshot) in
let test = snapshot.hasChild("\(id)")
if test {
print("id exists")
checkID() // check another one
}
else {
print("this is the one \(id)")
}
}
}
another thing is that it should be observeSingleEvent instead of observe

How to define functions within a function in typescript?

I know basic Javascript, but am confronted with a problem in a Typescript file. I'm using Ionic framework to test a page where a user can theoretically "swipe" like they're on Tinder, just for fun.
I have all the JS written, because I'm moving this over from Codepen, but I can't seem to get past Typescript's syntax.
The Javascript:
var tinderContainer = document.querySelector('.tinder');
var allCards = document.querySelectorAll('.tinder--card');
var nope = document.getElementById('nope');
var love = document.getElementById('love');
function initCards(card, index) {
var newCards = document.querySelectorAll('.tinder--card:not(.removed)');
newCards.forEach(function (card, index) {
card.style.zIndex = allCards.length - index;
}
}
The Typescript (that I put together using Google and SOF answers):
export class TestPage {
constructor(public navCtrl: NavController) {
}
tinderContainer = document.querySelector('ion-content');
allCards = document.querySelector('.tinder--card');
nope = document.getElementById('nope');
love = document.getElementById('love');
declare initCards(card,index) {
newCards = document.querySelectorAll('.tinder--card:not(.removed)');
newCards.forEach((card,index)) {
card.style.zIndex = allCards.length - index;
}
}
}
some hints are:
Use let newCards in you function as you have to declare your variable.
Your forEach should be something like this.
newCards.forEach((card, index) => {
...
});
but in order to use syntax like card.style.zIndex and allCards.length you will have to declare variable types..
For unknown models you can use something like card['style']['zIndex']
Also you have to use this to access class properties, like this.allCards

FLUTTER How to get variable based on passed string name?

I have stored variables in a class with their code names.
Suppose I want to get XVG from that class, I want to do
String getIconsURL(String symbol) {
var list = new URLsList();
//symbol = 'XVG'
return list.(symbol);
}
class URLsList{
var XVG = 'some url';
var BTC = 'some url';
}
Can someone help me achieve this or provide me with a better solution?
Dart when used in flutter doesn't support reflection.
If it's text that you want to have directly in your code for some reason, I'd advise using a text replace (using your favourite tool or using intellij's find + replace with regex) to change it into a map, i.e.
final Map<String, String> whee = {
'XVG': 'url 1',
'BTC': 'url 2',
};
Another alternative is saving it as a JSON file in your assets, and then loading it and reading it when the app opens, or even downloading it from a server on first run / when needed (in case the URLs need updating more often than you plan on updating the app). Hardcoding a bunch of data like that isn't necessarily always a good idea.
EDIT: how to use.
final Map<String, String> whee = .....
String getIconsURL(String symbol) {
//symbol = 'XVG'
return whee[symbol];
}
If you define it in a class make sure you set it to static as well so it doesn't make another each time the class is instantiated.
Also, if you want to iterate through them you have the option of using entries, keys, or values - see the Map Class documentation
I'd just implement a getProperty(String name) method or the [] operator like:
class URLsList{
var XVG = 'some url';
var BTC = 'some url';
String get operator [](String key) {
switch(key) {
case 'XVG': return XVG;
case 'BTC': return BTC;
}
}
}
String getIconsURL(String symbol) {
var list = new URLsList();
return list[symbol];
}
You can also use reflectable package that enables you to use reflection-like code by code generation.
Assuming that the class is being created from a JSON Object, you can always use objectName.toJSON() and then use the variable names are array indices to do your computations.

Class create an instance of itself

I want an instance method of a class to create an instance of itself
and append it to an array.
I tried this:
var vampireArray: [Vampire] = []
class Vampire {
func createSelf() {
vampireArray.append(Vampire())
}
}
but I get a strange error on console like (lldb)
Any ideas why?
Even without knowing the context to this code, in general, creating and then appending an instance of a class to an array outside of the scope of the class is a bad idea.
Instead, try putting the line vampireArray.append(Vampire()) wherever you were originally planning to put createSelf().
If you wanted to stick with this route, it would seem like the method createSelf() should be a static method and var vampireArray: [Vampire] = [] should also be a static variable inside your class.
EDIT:
If all you want to do is keep track of the number of vampires, this could be a good solution:
class Vampire {
static var VampireCount = 0
init(){
Vampire.VampireCount += 1
}
}
Then, whenever you wanted to access the count, just use Vampire.VampireCount

How to make a query with usage of like operator over a string collection in GORM

Assume a domain class called User. User class looks like this:
class User {
List<String> values
}
The collection values contains strings like "http://example.com/x", "http://google.com/y", "http://google.com/z" and so on...
Let's say we want to build a query which gets all the users that have specific string in the collection values (e.g. "google.com"). Something like this:
def criteria = User.createCriteria()
def results = criteria.listDistinct () {
and {
user.values.each { like('someField', '%${it}%') }
}
}
Any ideas?
I have found the answer by experimentation. The solution is:
def criteria = User.createCriteria()
def results = criteria.listDistinct () {
and {
user.values.each { like('someField', '%'+it+'%') }
}
}
I am not sure what you are doing with your suggested answer.
I have never seen that usage of each in the criteria query before.
This question has been asked many times before but never given an answer.
The problem is that you are queriyng a String association, which is not a domain class. If you would make your own String domain class for example ondrej.String { String strValue } then you would be able to do :
User.withCriteria {
values { ilike("strValue", "...") }
}
The problem is not having access to the value of the String object. The value of the String class is called value, but it is a char array, so I do not believe the following will work:
User.withCriteria {
values { ilike("value", "...") }
}
You could try using :
User.withCriteria {
values { ilike("toString", "...") }
}
or something else instead of toString ... I do not have the possibility to test this right now.
Hope that helps
After a lot of trying and researching, I found this will work with Grails 2.4.0, I don't know about older versions.
Cat.withCriteria {
createAlias('nicknames', 'n')
ilike 'n.elements', '%kitty%'
}
The trick is to use 'n.elements'

Resources