Frida Hook For Operator== - javascript

I want to hook operator== function using frida
Im Trying To create a hook for operator== function
Like Here Is The Real Code
std::string x = "YES";
std::string y = "YES";
if (x == y){
printf("VERIFIED");
}
Here I want to hook the operator==
with frida and print what both values it get for x and y or any other parameters strings, int etc
i just want to print both the values called in the operator== function

Related

What is the equivalent of executing multiple instance methods in functional programming in JS?

Suppose I have a class in JS with Typescript like this:
type Command = 'F' | 'B' // Forwards, Backwards
class Car {
private x: number
private y: number
constructor(x: number, y: number) {
this.x = x
this.y = y
}
private reportPosition() {
return `(${this.x}, ${this.y})`
}
private move(command: Command) {
if (command === 'F') this.y++
if (command === 'B') this.y--
}
execute(command: Command) {
this.move(command)
return this.reportPosition()
}
}
When I create a Car instance and execute the execute method, two things happen:
The internal state of the instance (x, y) is updated.
The execute function returns a string.
Now I want to write the same thing in a more FP way, making the functions pure, but I stumble at the execute function.
My approach is this:
type Command = 'F' | 'B'
type Car = {
x: number
y: number
}
function createCar(x: number, y: number): Car {
return { x, y }
}
function reportPosition(car: Car) {
return `$({car.x}, ${car.y})`
}
function move(car: Car, command: Command) {
if (command === 'F') return createCar(car.x + 1, car.y)
if (command === 'B') return createCar(car.x - 1, car.y)
return car
}
function execute(car: Car, command: Command) {
const newCar = move(car, command)
const msg = reportPosition(newCar)
return [newCar, msg]
}
My questions are the following:
Since execute does two things at once, I feel I am forced to return two values from it in the function. But this feels wrong.
Is this "valid" functional programming? Or would I never create such a function in the FP world and just call each of the functions inside (move, reportPosition) separately.
What if the move function also had to return the information on whether the car has crashed after its move? Would I also have to return two values from that modified function: the new car instance and a boolean (indicating a crash)?
Also, I used the createCar function within the move function, which is technically not allowed for pure functions, correct?
What would be the best way to fix that? Pass the createCar function as an argument to move?
Thanks!
Doing two things at once doesn't necessarily make a function invalid functional programming (I think by "valid" you're referring to pure functional programming). What makes a function "pure" in functional programming is that its return value is only determined by its input values and nothing else. It also does not modify any external state or variables (referred to as "free variables" or global variables, meaning a variable that is not bound in the input parameters). What you're doing in execute can be expressed in a functional programming language trivially, for example Haskell:
execute :: Car -> Command -> (Car, String)
execute car cmd = let newCar = move car cmd in (newCar, reportPosition newCar)
If move had to report additional data, you could include it in the return type and it would remain pure. However, assuming "if the car crashed" is an error state, then typically this would be modeled by returning a sum type (Maybe or Either in Haskell). Take Maybe for example: data Maybe a = Just a | Nothing, if the car crashed you could return Nothing and if it didn't then return Just position, then anything using the move function can verify that it didn't return Nothing.
Why would you not be allowed to call createCar inside move? Neither move nor createCar are modifying any external state/variables, both are only using the inputs provided in their returns.
Re-iterating my comment in the main post, a lot of these things from Haskell that I mentioned above (e.g. Maybe) are available in libraries for JavaScript/TypeScript. For TypeScript in particular, there's https://github.com/gcanti/fp-ts. It can be a little bit confusing sometimes, as usually there are many names that refer to the same concept. For instance, some libraries refer to Maybe as Option.

specify the result of a function in javascript

Am not sure if functions in javascript are what we call methods in other programming languages.Methods in other programming languages can have their result specified just after the access-specifier like in C# for example i would do like
//method to return int
private int myinteger(){
int a=0;
return a;
}
//method to return string
private string mystring(){
string b="myreturn";
return b;
}
I just don't know how to do that with javascript functions, you think you can help me with a sample?Thank You very much :)
You don't need to provide the data types in javascript. The functions are pretty similar you just have to start it with the function keyword.
Also, we need to start the variables with const or let.
I use the console.log(myinteger()); below to log the value of the myinteger() function in the browser console. (Similar to c++'s cout)
//method to return int
function myinteger() {
const a = 0;
return a;
}
//method to return string
function mystring() {
const b = "myreturn";
return b;
}
console.log(myinteger());
console.log(mystring());
If you are someone who wants to use javascript but still want to assign the data type and many more thing then you can use TypeScript By Microsoft.
You cannot do this with javascript, but you still have two workarounds available:
Use eslint and use the https://eslint.org/docs/rules/consistent-return rule
Use typescript
Javascript has types for values (not variables)
so you can define a variable as
var name = "Hamond";
and to know it's type you have to use typeof
typeof name; // "string"
Side note: you can use let or const instead of var but let that be for another time.
so variables in javascript doesn't have types, values have.
You can add static typing using typescript so
var name: string = "Hamond";
and at dev time if you wanted to edit name and incorrectly deal with it as a non string type you will get an error
warning you immediately
name = 3; // error
name - 4; // error
// and so forth because `name` is of `string` type
so this type check is done at author or dev time and you don't have to wait until run time to get the error.
Why all the talk about variables and values?
Because Javascript function can return any value(even returning a variable is basically returning its value if it's scalar value or its reference if it's an object type)
so defining a function look like:
function doSomething(){
return 33;
}
notes:
no return type
can have no return statement(by default will return undefined)
with typescript
function doSomething(): number{
return 33;
}
typing problems solved at dev/write time
About function vs method:
I think developers in many times use these terms interchangeably, but in javascript we just have function, even a function defined inside a class in javascript is just a function. People like the name method when its defined inside some class.
references:
JS syntax
The Nature Of Functions

how to create a swapping function in jquery

i basically want to swap the content of 2 variables using a function either in jquery or javascript. and i cont have a bias against any method.(it won't make difference using a temporary variable or not using it)
I tried this in javascript
function swap(x,y){
x = x + y;
y = x - y;
x = x - y;
return x,y
}
var two = 1;
var one= 2;
swap (one,two);
document.write(one);
There are a few errors in your code. It seems like you are not familiar with how datastructures and variable/functions work in Javascript. I strongly recommend you learn about them first.
Your swap function takes 2 arguments x and y and does some mathematical operations on it which would swap the values. Here, you are making the assumption that the variables will always be a number or a type of variable where addition and subtraction would make sense. Unless you can assure that variables will always be numbers, this is the wrong approach.
Similarly, your return statement is return x, y. You cannot return 2 variables from a function. Output from a function is a single value. In this case, since you need 2 variables to be returned, you should use a composite structure like an array / object.
The value returned from this function needs to be stored in appropriate variables so that you can use them. Line number 10 in your code calls the method, but does not store its return value anywhere.
Generic solution for your use case will be as follows
// Create two variables
let x = 1, y = 2;
function swap(x, y) {
// Return the 2 variables in swapped order
return [y, x]
};
// Call the swap function and assign the return values back to the variables
[x, y] = swap(x, y);
// Print to console to verify
console.log(x, y);
You don't need to do any addition or subtraction in the function; you can simply return y and then x. However, note that you can't return two values at once from a function -- you'll need to return then as an array (that optionally gets converted to a string), if you want to return them both at the same time.
function swap(x, y) {
return [y, x].toString();
}
console.log(swap(1, 2));

javascript onclick set to variable function name?

Is it possible to use JavaScript to set the click event to a variable function name?
I want to dynamically set each of the selected elements to functions that the names are declared elsewhere. These functions are class methods. I don't know if this is why it's not working.
I know this is funky monkey stuff, but it's preferable to the alternative of using a switch, with all the functions I have to parse through.
function name get
// get last part of the string
var x = strName.split("_");
var y = x[x.length - 1];
This works
holder[j].onclick = function(){ini['add']();};
This does not, for some reason
holder[j].onclick = function(){ini[y]();}; // I checked: y == 'add' in the test
I found that this does not work. The onclick is not set to the function I want it to be, just the statement
ini[y]();
As y is no longer in scope after the setup is complete (and even if it was, there's no saying that it would be the correct y, as it has a different value each iteration), I've had to pull it out to make it determine function on the fly.
Using this:
holder[j].onclick = function(){clickMaster(this);};
Then this:
function clickMaster(ele){
// pull out strName based on ele
var x = strName.split("_");
var y = x[x.length - 1];
ini[y]();
}
Rather than just trying to set the onclick directly to the class method.

Check whether function result value is used

I was wondering whether it is possible to check whether a function is called in such a way that its return value is stored in a variable, or that it is called 'standalone'.
In the first case, the function should indeed return something, but otherwise it should save the value(s) to the current instance of my object.
To be specific, I have a Vector3D as follows:
function Vector3D(x, y, z) {
this.x = parseNumber(x);
this.y = parseNumber(y);
this.z = parseNumber(z);
}
Vector3D.prototype.add = function(that) {
return new Vector3D(
this.x + that.x,
this.y + that.y,
this.z + that.z
);
};
As can be seen, the add function returns something based on the object instance itself and on another instance. As it is now, the function must be called in a way like this:
var addedVector = vect1.add(vect2);
However, if I were to call it like this:
vect1.add(vect2);
it should not return anything (that's quite useless), but instead store the return coordinates (x, y and z) in the variables of vect1. So vect1 should become what is called addedVector in the other line of code.
To accomplish this, I guess I'm going to need to know whether the function is called alone or that its return value is stored.
Is there any way to accomplish this?
You could pretty easily break this apart into two functions, add(vector) and add_m(vector).
The second function (add_m) would mutate vector 1 and add the values of vector 2 to it and return nothing, whereas the first would make a new vector that is the result and return it.
I'm reasonably sure what you're describing is impossible. Even if it were, though, You'd probably not want to do it that way. Compare your code with a small variation:
var addedVector = vect1.add(vect2);
vect1.add(vect2);
var addedVector = eval("vect1.add(vect2)");
eval("vect1.add(vect2)");
I'm pretty sure that you'd like the lines with the evals to work the same as the ones without, right? Yet eval() trivially has to "use" the return value so it can propagate it outside.
Now, what I'd do is write any of the following functions that I happened to need:
v1.add(v2); // returns (v1+v2), v1 and v2 are unchanged
v1.addFrom(v2) // v1 = v1 + v2, v2 is unchanged, returns undefined
v1.addTo(v2) // v2 = v1 + v2, v1 is unchanged, returns undefined
depending on the usage, I might or might not have addTo/addFrom return the result.
There's no way to know how your caller has called your method, i.e. whether your caller has assigned the return value to a variable or not.
Nope. No way to do that, the function body is completely independent from the code context it's called in. Sounds like you want two separate functions, one to add in place and one to add and return a new value.
You cannot find that out. How about letting the function accept two parameters?
If only one is passed, you add it to the vector the function is called on.
If two are passed, you create a new one and sum them.
Something like:
Vector3D.prototype.add = function(vec1, vec2) {
var target = vec2 ? new Vector3D(0,0,0) : this;
vec1 = vec2 ? vec1 : this;
vec2 = vec2 || vec1;
target.x = vec1.x + vec2.x;
target.y = vec1.y + vec2.y;
target.z = vec1.z + vec2.z;
return target;
}
Then you can call it with:
v1.add(v2); // adds v2 to v1
or
var addedVector = v1.add(v1, v2); // creates a new vector v1 + v2
But I agree that it is probably better to create two separate functions. A function should only do one thing.

Categories