All right, guys, welcome back.
Now, what's the question?
Why this one is not that easy on instance methods.
And this is something we will find out in this video over here.
So therefore let's start directly at Ubuntu M.
All right.
So here we are back the point of M and let's have a look into this application once again.
So within apps free to func we are opening this with Shadi.
We.
All right.
Here we have an open.
Let's see.
Com epic in free to funk and give you a having our main activity.
Here we have it.
So we have seen calling and static method is absolutely easy in freedom.
All we need to do is we simply need to get this class reference and then we can simply call this method.
We can actively trigger it.
We have done this for the next level function over here.
So if this one has a static attribute, very easy.
Now the question why is this not that easy on an instance method?
For example, if you take a look back in our player class, this one over here.
So if you want to call a method, for example, increased life or decreased life, why can we not simply
use this class reference and then call in this method over here?
Let's find this out right now.
So we try to call an instance method and we will observe and see what Frieda is telling us afterwards.
Alrighty.
Okay, let's go.
Therefore, I'm getting back the smartphone screen.
Here we have it.
We keep it in foreground.
Then I'm opening a new terminal, and now I'm starting to free the server.
So adb shell root privileges.
Then we change into data and local temp and then we start this free server.
Nice.
This one is now running in the background and now we are opening this research to the code editor and
we do the exact same approach like it would have done thus on static method.
I mean, we could use this one over here, but to get even more practice, let's close them and rewrite
this completely on our own.
All right.
New file in JavaScript, and let's get back the code on the right smartphone.
We will we will pull this out later on.
All right.
Everything has been set up so far.
Now, let's see.
The first thing we need to do is we need to tell Frieda to connect to the KVM.
And once again, we are dealing with Java code and therefore we need our connection to the JDM.
So let's do this in Java.
Perform.
And here we have it.
Our parameter.
Once again, this anonymous function.
And this one over here.
Okay, now we need to get this reference to this class.
In this case, it's the player class.
So let's see.
I named this also player, and I put the mouse on the right that you can see this text.
So now getting this reference in Java dot use and then we are having comm app checking free to func
dot and the class is player dot player.
And this time I'm making the comments directly.
So getting a reference to the player class.
Okay, now we're having the reference to this player class and let's try to call this method, for example,
decreased live player dot decrease life.
All right, let's run the script and see why it's not working.
So we take this.
We are opening a new terminal, and here we're connecting to this process free data.
As you come attacking free to func.
I mean, the package name is this time.
Luckily the same.
But if you want to be sure, then please have a look into the Android manifest example to get this one
over here.
So let's see.
And here we are having the package identifier.
Okay, we connect to this process.
Oh yeah, we can see it has not been started, therefore we cannot connect to it.
If we pull out our smartphone or this connection seems to get lost, that's opening it once again.
There we go.
We can see this process has not been opened.
Therefore, Frida cannot attach to a process that does not exist.
Before we start this on the smartphone, let's simply use Frida to do this.
So by appending this dash F and then dash dash, no pause.
We can run the application over here with Frida.
All right.
Application has been started.
We are hooked or attached to this process.
Now let's try to call this method.
We are getting an error.
I mean, this is something we.
We could have expected because I already announced it in this way.
But let's see what this error is telling us.
So in decreased life, we cannot call the instance method without an instance.
All right.
What does this over here means?
If you take it back and look into this one, into our code.
So within the main activity, we can see this one here was a static method.
Next level calling esthetic method is simply like main activity.
Dot next level.
So class name, dot method name.
The reason why we can call static method in this way is we do not have to create an object and static
method is not being bound to an object and therefore we can simply trigger it in this way.
If we are now taking a look into our player class, there we have it.
We can see if we want to call method, for example, increased life or decreased life.
These are not static and therefore we have to create an object first.
And this is exactly what happens in our code.
Within the main activity we can see we are creating here this new player object.
So here we are creating a new object.
Now we're having this player instance and this one over here.
And then on this player instance, we can call this method decrease life.
This is the reason why we cannot simply use it in this way over here in this one, because here we do
not have an object.
We are just having an class reference and then we are calling a method which is bound to an object.
But we do not have an object right now.
All right.
Maybe let's do some a little bit of an explanation, because also class object.
In instance, these words gets mixed together a lot.
And to be honest, I'm not 100% sure regarding to objects and instances.
But nevertheless, let me try to explain this to you, because then I guess it's getting a little bit
more clear.
All right.
Therefore, let's take a quick look on the graphic tablet.
Okay.
So here we have back on the graphic tablet and now let's have a look into classes, object and instances
and yeah, why we need an instance to call this method over here.
All righty.
So therefore, let's go.
So on the left, we've got our player class.
And this one over here.
And this is our class.
If you now want to use this class over here, we have to create an object and we can do this in the
following way.
So let's split this over here.
Player.
Equals new player.
Okay.
And this is how we can create new objects.
And this one appears over here.
And this is now our player object.
And now we can use these methods on this player object over here.
So, for example, this method decrease life.
In this one, we can now simply use player our object.
Not decrease life.
And this will decrease the life of this player option of this one over here.
And those lives over here in these are bound to this object right now.
So this player object, maybe let's let's write this down.
And this player object is now losing a life if we call this method over here.
Okay.
All right.
So the difference between an static method and an instance method is this one over here.
In this one, this is generating a new object.
And this is also something we need to do in freedom.
We will have a look into this one in a moment.
But for now, let me try to explain you the difference between objects and instances.
But before we go into this, maybe let's try to clarify these words classes, objects and instances.
As already mentioned, I might not be 100% correct on this one, but I can explain it in a way that
we can understand and use it in freedom.
And to be honest, regarding to this course over here, that's absolutely enough.
So this is enough.
We need to know about these terms already.
Okay, let's have look into this one.
Okay.
So on the top left, we have now our class, our player class.
And you can think of this like some blueprint of our player.
So these are the method and fields we can have in our player over here.
Okay.
All right.
Next, we've got an object.
And to create an object, we have to use it in the following way.
New player.
So now we are creating an object out of this class within our application memory.
And this is our application memory.
We are now having a player object in there.
Okay.
The last time we've got over here is an instance, and I keep explaining it in the following way.
So here we are creating a new player object on the right.
And now this one over here.
Player.
And this is now an instance and this is now in concrete object.
For example, this one over here.
This is now our instance.
Okay.
For now, it might not be 100% clear what the difference of all of this is.
Classes and objects might be clear, but instance is an object.
It's still a little bit similar.
Therefore, let's create a new object over here.
So here we are, creating now a new player.
And this one over here.
And this one is now a new player instance.
Player two This means we are now having a second player in our application over here.
So this one over here, and this is another instance.
And this one also has some lives.
Okay.
So this is the difference between these three components, this class, this object and an instance
we can think of an instance like having a copy of an object, but to be honest, it makes it a little
bit more confusing than it helps, at least for me.
So if we are talking about an instance, we are talking about this one over here, for example, this
player instance.
So this one over here, of course, also has a memory location, for example, over here.
And this is now our instance of this player.
And we've also got this instance of this player.
So now we are having two players at all over here.
This one and this one.
All right.
So I hope this was a little bit helpful for you right now.
We will now go back to our free script and then we will figure out how we can call an instance method.
Alrighty, let's switch back to the Ubuntu VM.
So here we are back at our ubuntu VM and we have seen this approach over here doesn't work.
The reason for this is now clear.
We cannot call an instance method on a class.
So in this case decrease life.
The the application doesn't know on what object it needs to decrease this lives because we are not providing
an object over here.
And this is exactly what Frieda is telling us.
If you take a look back into this one, we can see we cannot call an instance method without having
an instance.
And therefore, we have now two possibilities.
One is searching the memory for an existing instance.
Then we can use this one over here, and then we can call this method also playing around with this
fields.
This is something we will learn in the next chapter because this is our second huge part regarding to
our free the hacking journey over here.
The first one over here is regarding to manipulating class methods.
The second part is working with existing objects, and the third part is playing around with the NDDC,
the native development kit.
We can also use Reader to hook into C C++ functions, libraries and play around with them.
And it's absolutely amazing how much we can do inside an C++ function.
And for example, we can hook functions like string compare and then we can figure out when this method
is being called, which arguments are being provided and what's returned value of this native function.
Absolutely amazing.
But for now, let's finish this chapter over here.
This is about hacking and triggering class methods.
So now the question how can we actively trigger an instance method to find this out?
We will once again taking a look into the Java code and then we will apply this to our free script over
here.
And this time it's a little bit different, but not that much.
Therefore, let's have a look into this one.
So this one is not working.
We can get rid of this one.
Now let's see how we can call an instance method in Java.
So calling an instance method in Java if you are not having an existing instance without having an instance.
All right.
And let me put this on the right.
There we go.
So if you want to call this decreased line function, the things we have to do are the following.
We have to create our player instance, and here we are creating a new player object.
And then we can use this player instance to call this method decrease life.
And this is exactly what we are also seeing in Radix.
So if you're taking a look in this code and there we go, here we are creating a new player and here
we are using this player object to call this instance method.
All right, now the question, how can we create a new instance in Java?
And this one is done in the following way.
The things we need to do for this one is getting the class reference.
So this player class over here, and then we need to add this new operator to create a new instance.
Let me show you how we can do this.
Therefore, maybe we need to be a little bit more exact on this one right now.
So this one here is the player class reference.
This is this new player over here.
All right.
And then we are creating now a new instance.
So this is now the player instance.
This is this one over here.
Maybe let's get rid of this one.
So this one is the player class reference and this one is the player instance.
Okay, for now, let's only focus on the right path.
This one over here.
So here we are creating a new object, a new player object, and we're using this class, this player
class reference.
And then we are using the new operator in this one over here.
So within Frieda, it looks like the following.
We are also using our player class reference and then we are calling this new operator with dollar dot
new.
This is how we can create a new instance.
Now we have this player instance and we can use this one to call this method.
So we are having this instance right now and this time let's see what instance method we want to call.
Let's have a look back and say D, x, and this time we want to call the impossible method.
This one over here, which is increased life because this method is not being called anywhere and this
increases the life of the player.
All right.
So now we're having this instance and therefore we can simply use the player instance, dot this method
name, and this is how we can create a new instance.
And triggering and method actively on this object.
So regarding to the naming over here, it would look like following this is the player reference and
here we are having a player instance and now we can use player instance dot increase life.
This is the Java code and this is our free code over here.
Regarding to the free the code, I would like to be a little bit more exact on what's in class and what's
an instance, because otherwise we will mess up our code.
If this is getting a little bit more complex.
Okay, so absolutely amazing.
Let's see if this one works out.
Therefore, I comment this out because this is not invalid code anymore.
And then yeah, let's see.
Therefore, we are opening a new terminal.
We get back our smartphone screen.
And we are also making our free the connection.
We can restart this application.
It doesn't matter.
And now we are also opening the ADB logcat because this method is printing out some statements so we
can observe if we can actively trigger this method over here.
And we can also see that the lives are being increased.
Therefore, let's open also the ADB locket.
ADB Logcat.
All right.
I organize these windows really quick.
So there we have it organized.
I mean, the smartphone or the application over here, we do not need to observe this until it crashes.
Then we need to have a look into this one.
Otherwise, it's not important what happens over here.
Okay, so now let's trigger this in this function.
There we go.
This is impossible.
This method does not get called anywhere in this code.
And we can see the player lives has been increased to six.
We can also see a new player.
Sorry, a new player object has been created.
If we go back, this is something that's been printed out in the constructor.
So we are creating a new player object and we are calling this method on this player object absolutely
amazing.
Right?
Let's increase the lives further and further.
So therefore, let's wrap this in a function.
Sorry.
Miles.
Back on the right.
No function.
Increase our player lives.
Oops.
Once is enough.
Okay.
Now let's insert it over here.
And now let's call this function once again.
Increase our player lives.
There we have it once again.
But player lives increasing lives equal.
Six.
Strange, right?
Let's call it once again.
The lives seems not to be increased.
So this is not a problem because we have wrapped it in function.
If you also would do it over here.
Yes.
So we can see the player lives doesn't increase.
Why is this the reason?
Let's find this out on a graphic tablet.
Okay.
So here we are back on the graphic tablet.
And now let's find out why these player lives are not being increased.
We can see they are stuck at W six.
And this one over here.
This one over here.
And also this one over here.
So let's find out what's the reason behind this.
But first, let's clean this screen script a little bit.
There we go.
So this is not being long a required and also at some comments.
So here we are getting not we are creating a new player instance.
And here we're calling
the instance method increase life on this instance.
All right.
So now let's have a look what this group is doing over here, and therefore, let's keep this in foreground
and enable our pen and let's see.
All right.
So here we have this.
Okay.
This line over here is simply just getting in reference to the player class in our Java virtual machine.
So within our JDM
JVM, we are now having a reference to the player class.
Okay.
Nothing more.
All right.
Then we've got this line over here, and now things start to get interesting.
And this one is creating a new object, or in this case, a new instance.
So this one over here, and this is a new player instance.
All right?
And this means this player instance has some lives.
This one over here.
So lives are now said to five.
And then we're calling this method over here.
Increase life.
On this player instance.
This means these lives are being increased by one.
We are having sex life afterwards.
All right.
Everything is working.
So far, so good.
Now, let's see what happens if we executing this method once again.
So we have now created this player instance.
And now we're calling this method once again over here.
Once again, we are getting the player class reference in our travel machine.
All right.
But then over here, we are once again creating a new instance.
So this one over here will now create a new player instance, this one over here.
And this player instance also has some lives equal set to five, because this is the default value in
our constructor.
And therefore, if we are now calling this method increased life, once again, we are we are increasing
the life of this player instance.
So now within our application memory and this one over here, we are now having two player instances
inside here in this one which is the newly generated.
Player two And we are also having this one and we are also having this one.
Which is player one.
So these are now two objects in our application.
And if you now call it once again, the following thing happened, and this one has been called.
All right.
Now, once again, we are getting this player class reference.
Once again, we are creating a new player instance over here.
This one, once again, has lives set to five, and this one once again being increased by one.
And this is why we are having these debug outputs over here.
So if you take a look into this one, so here we can see player lives increasing lives to six, but
we can also see a new player object has been created.
And then we've got this method over here.
Here once again, player lives increased to six.
So these three are bound together and then we are having it once again, a new player object has been
created.
This is impossible.
Player lives increased to six and then once again a new player object has been created.
This is impossible and so on.
So we are not increasing the lives of a single object.
We are creating multiple objects and increasing the lives of them, but only once.
Therefore, if we want to increase the lives on our player object over here, we have to call it multiple
times.
So if we do it.
In this way, then we are increasing the lives of one player object because here we are having the reference
to one instance to one player in our screen.
If you toggle this once again and maybe also put the smartphone behind this.
There we go.
So here we are creating now a new player instance.
And then we are calling this method multiple times on the same instance.
So this means the lives of this instance and should increase.
Oops.
There we go.
And let's try this out.
So we take this code and we paste it in here, and then we're calling our method, not this one.
Then we have to write it.
The method name is increase.
And there we have it.
All right, let's observe the ADB output over here.
Okay.
Now we call it.
And then we have to scroll down.
There we have it.
Player lives increased seven, eight, nine, ten and so on.
So now we're having a player object with multiple lives.
And if we call this once again on the left and this one over here, and then we can see once again the
life start at six and it's being increased in this way.
The only question right now is where is this player object?
Right.
If you take a look in our smartphone, I mean, it's not there.
And if we click on this one, we are losing lives and these lives are not being set to six.
These lives are being set to five, the default value.
So where are those players?
We are now having three or four player objects, but they are not visible.
Let's see if we can find them.
Nope.
Or it's not here.
Are they gone?
Are they even existing?
These objects are definitely there because we can see we are increasing those those lives of them.
But we cannot see them in our smartphone over here because they only exist in memory.
The memory of this application would now look like the following.
So here we are having now all these objects and within the memory we are also having them.
I mean, I do not have enough colors for this one, but here we are having a new player object.
Here we are having a new player object and so on.
So we have created now all these player objects in memory, but visible is only one in this one over
here because these objects are not being applied to the smartphone screen.
So they are existing in memory, but they are not visible on the smartphone over here.
Therefore, they can also not interact with this application.
They are completely useless for our game right now.
If we get hit on our player, I can we can see this over here.
We are losing a life.
This means even if we are having now an object that has.
Let's see how many lives?
13, 14, 14 lives.
It's absolutely useless.
And this is exactly what we learn in chapter two.
In chapter two, we learn how we can use or find this object over here and increase the lives of this
one.
And that in this case, we are not having two lives anymore.
We are having 100,000 or even more.
All right.
So this is all for this video right now.
We have learned in this chapter over here how we can hook and manipulate functions.
We have hook parameters, return values.
We have modified static fields.
We have actively triggered static methods and now also actively triggered instance methods.
By creating a new instance, we learned a lot and don't worry, we do not stop right now.
It will be even more amazing because after finishing the next huge chapter in freedom, we will have
a lot of fun with our space planning application because in a mobile game most of the things are objects
and we want to work on existing objects to increase the goal, life, health, experience, whatever
we want inside here.
Alrighty.
So I'm looking forward to see you in the second big chapter of freedom.
See you there.