But I come back after finishing the quiz.
Let's have a look into patch these if instructions.
We are now pretty familiar with how they work, but how can we patch them to modify the the application
behavior to get into the other branches or things like this?
Generally speaking, there are a lot of methods.
I've been introduced three of them because I guess, yeah, with those three you will solve all your
tasks you want to do.
All right, let's dive into the first one.
Method one is logic.
Flipping sounds promising.
Let's see how it works.
Okay.
On the right side, we've got our Java code and on the left we've got the corresponding smiley code.
And let's have a look into this.
We are creating a new play object over here.
Then the player gets hit.
So in the last exercise, we lose a player life over here.
But now there's something different.
The player has now a new instance field named Shield.
You got the feeling what we are going to do in the next couple of videos?
Yes, absolutely.
So in this case, our player has now a new instance, Field Shield.
And if the player has now a shield and the player gets hit, the player doesn't lose a life, the shield
gets removed.
So player the shield is now equals false.
And we move on in our code and in case we do not have a shield.
So if player the shield is false, then the player loses one life and we ought to move on in our code.
All right.
Okay, let's have a look at this smiley code.
So in this line, we are having v one initialized with 0x1 and then we've got an I get boolean.
So we're getting this instance player thought shield, which is from type boolean into V two.
And now we've gotten if statement and this if statement is please read it out loud.
If V two is equal zero then jump to condition zero.
So it looks like this.
Now we need to step back and think about this.
Let's see what we are checking.
Okay.
We are checking if you're having a shield.
The shield is from type boolean.
This means the shield can be true or false.
If we are now doing a comparison against zero, it means we are checking if the player does not have
a shield because if the shield is zero, then it's false.
And this is the comparison against zero.
So this statement would have been or would be true.
All right.
So this is our statement.
If we do not have a shield, then you come to condition zero.
Let's see.
Condition zeros over here we are fetching our player dot lives into V two.
We are subtracting one from this.
We are putting it back and then we return out of this.
All right, this looks good.
Otherwise, let's have a look.
So if we are having a shield, it means it's not equal to zero.
Then we can see b one is initialized with zero and we are putting our shield state sorry, shield state
to zero, which means our player Dodge Shield is now forwards.
So we lose our shield.
But we do not lose a life in this in this approach.
Okay.
And then we jump to go to zero, which is here and we return out.
All right.
This looks okay for me.
Now, let's see how we can patch this code that we get an advantage out of this.
So there are many different approaches we can do.
In my case, I want to talk about flipping the logic, which means if we are not having a shield, we
do not get hit.
And if we are having a shield, we do get hit.
All right.
How can we do this with only one instruction over there?
Think about the if statements and all these different types.
Can we use this to change the behavior?
Absolutely correct.
We can do this and we do this with the following sign.
We are adding this exclamation mark, and by doing this, we will flip the logic.
Our if equals zero will become, if not equal, zero.
And let's sort this approach once again.
How does it looks like?
So still we've got a one within our register v one now we are putting our player dot shield into v two.
We are not having a shield right now.
So this statement or v two is holding the Yeah.
The value false.
Now we've got, this becomes now a little bit of a brain task.
So it's getting a little bit complicated.
But let's see, let's, let's do this once again.
If V two, which is false, is not equal to zero, we jump to condition zero.
All right.
Once again, if we two is not equal zero, we jump to condition zero.
Okay?
This means if our V zero is not equal to zero, we jump to condition zero.
So in our case v two is not true.
V two is false.
Therefore we do not jump to condition zero.
Okay, then we move on.
Over here we are putting zero into v one and then we're putting false back again into our player shield
state.
And this is ridiculous because this means we still keep our player shield on because we have flipped
the logic by having player shield equals false, we cannot get hit.
And this is good because yeah, we are putting this value here once again into this.
Therefore it does not get overridden.
Therefore we are having still our shield or imaginary shield over here, but it will protect us.
So that's good.
Okay.
And afterwards we go out of this.
So flipping a logic makes it really complicated.
Therefore, yeah, we have to say it out loud and think about what we want to check and what we did.
Normally.
If you understood the initial if statement and you want to have the opposite of this, you can simply
flip this by not even thinking about this.
By thinking about this, you might get a little bit confused afterwards, but you will figure out it's
still correct.
So yeah, that's what we've got right here.
And this is something we want to code right now and then analyze back again because it's really complicated.
I know, but we practice this.
That's why you're here.
And together we will make this.
And afterwards you can do a lot of funny things.
We will learn some other methods, which might be sometimes a little bit easier.
But for now, let's stick to this one.
Okay?
Before we code it, let's do it once again together.
Let's do it for the initial state.
If B two is equal zero, we jump to our condition.
Zero.
This means we are not having a player shield.
Now we patch this instruction with the exclamation mark and therefore we switch the equals zero to not
equal zero.
Which means unless we are not having a player shield, we do not lose a line.
It's also for me not easy to do this.
So I have to think about this.
But by thinking about, we get the solution.
Okay.
All right, now let's get back to our Ubuntu VM and code is really quick.
Here we add our Ubuntu VM and we still got this old project open.
Let's see.
Yes, we've got to check high score and we've got to get hit over here.
All right, let's write this piece of code to implement that we've gotten sheared.
So we are having a new instance field and boolean sheared.
And let's set this to true because then we start with and shield and if we get hit we shouldn't lose
the life afterwards we remove the shield and if we get hit once again, then we lose alive.
Alrighty.
So let's see.
We are now in our get hit method.
So if this dot sheared is equals true we do not get hit otherwise.
Which is in this else over here.
We do get hit.
Okay.
After getting hit and having a shield, we need to remove the shield because the shield blocked this
collision.
So this shield is equals false.
And here we can also see an additional patch possibility, which yeah.
Comes handy for us.
Let's say we are not removing our shield, so let's say this one stays true.
This would mean that if you pick up once a shield, it does not get removed.
So it stays there forever until we end the game.
This would be also a nice patch, but with a little bit of a downside.
We have to pick up the shield first and this is something we do not want to do.
Be a super lazy and we want to start with all we want to have.
Therefore, it's a nice patch but not what we are looking for.
All right.
So this one is set to false.
Afterwards, we get hit and that's our code.
And let's try this out.
So we click on Build.
And I'm also making the ADB connection to our VM.
Already connected.
That's good.
Relocate.
There we go.
And yeah, let's install this app.
Screen copy.
There we go.
Smiley to.
This is our application.
Let's see.
So we are now having a shield.
This means if we get hit or if you press the button, we get hit.
But we have our shield.
Therefore, we should not lose a life.
They should be in five afterwards.
Five lives.
Let's see.
We get hit in three, two, one now.
All right.
Nice.
It's working.
We have now lost our shield.
We can shake this by hidden.
By hitting this button once again.
Yes.
Now it's reducing the list.
All right.
Let's do a quick check if it's really correct.
So let's close this one over here.
Okay.
And now let's remove this shield.
We build it once again.
Okay.
Locate ADB install app debug.
We are open.
Opening it.
If he now gets it, we do not have a shield.
Therefore, we should lose a life.
And there should be a four.
Let's see.
Three, two, one.
Yes.
Okay.
I'm glad that this worked.
Otherwise, I had to figure out what goes wrong.
But this is exactly what we want to have.
All right.
Regarding to the state, this doesn't really matter because the state changes in the game over and over
again.
We take a look at this over here at this.
Yeah, this if statement and how to patch this.
So I'm going to decompile this application and then we pull out our graphic tablet.
And so before we decompile this application, I really would like to add something.
So this is not an Android development course and the things I have shown so far are perfectly fine to
create simple mockup apps to test or train a special topic like if conditions, however, in these applications
might not look that fancy, or let's say they are a little bit boring, and therefore I have also provided
a more fancy version of this application over here.
The code is pretty identical, so the only difference is that the smiley to design version has an additional
button to pick up a shield and some form of visual output.
You can do exactly the same in the mockup application with some sort of debug statements.
For example, System.out.println.
This is also perfectly fine.
And so to Petra's application to start with The Shield, I recommend using the Smiley to design version
or to set the Shield state to false in our mock up version.
I also provide you all three applications in this section as an attachment in the Smiley to design version,
you have some sort of visual output in the smiley to no shield version.
You shouldn't lose a life after patching it.
It doesn't matter in which version you apply the patch.
All that matters is that we understand how flipping the logic affects our application and that we can
achieve what we want in there.
Because later on we will take our our approach to real world applications, for example, to bypass
the routine detection or to modify our space bank application to achieve certain features.
All righty, perfect.
Let's reverse this real quick, and then we figure out how we can patch this to get and shield permanent.
Okay.
This time I also try to write this a little bit more clean that yeah, it doesn't get that messy, but
I'm not sure how good this goes, so let's see.
All right, we've got our method over here.
Get hit.
So here we have our method name.
We can see zero parameters and return white.
This is our function prototype.
Och then we've got, I get and we have our this object over here so p zero is this I get means we've
got an instance field so we've got this and here's our instance field and this dot shield.
And this is being put into re zero.
Okay.
So we zero is now this thought shared.
Okay.
Then we are having we one is being initialized with zero and we are having b two is also being initialized
with B two.
By the way, I skipped about this, but we are already familiar with this, so I guess I do not have
to explain regarding to the local three anymore.
You might be bored about this one and I can fully understand this.
Okay, so back on track.
We are within B one.
We one is now zero and B two is now one.
Okay.
Then we have our if statement.
So we stick to this dramatic color over here and let's read this out loud together.
If we zero is not equal, we to.
If we zero it's not equal we to.
Then we jump to this condition, and this condition is here.
So we jump to this one over here.
Okay.
Let's check what this is is about B0 is our this dodge shield.
So if this dodge here.
Okay.
My promise.
I guess it's already over.
So let's see if she.
It's not equal.
And we too is true.
Oops.
There we go.
Okay, this is our if statement.
So if this sheet is not equally true, sounds a little bit strange, but we will figure out what this
is about.
Let's see.
Then we get into this one.
I get we are fetching p zero.
This one's is our this back again in this dot lives and we are fetching this value and putting it into
v zero.
So we zero.
Oh, that's interesting.
Let's change the color.
Zero.
Is this thought lives?
So why is it interesting?
Because we are overwriting v zero at this point over here.
So here we zero is reference to this dot shield, but now it's being overwritten to this dot lives.
So take care.
Overwritten.
And yeah, if we keep track on those registers in this way, we do not have any trouble.
Therefore we know.
Okay, we zero is now the lives.
Okay.
So we are at this line over here.
Then we are subtracting.
We zero equals zero minus v two.
So we are subtracting the two from the zero and storing the result back in v zero.
We zero is now we zero minus v two which is v zero is now this dot lives.
This thought lives so abstracted with one.
Yes, this one over here.
Minus one.
Okay.
And then.
Yeah.
We are putting this value back again.
And you can see those patterns.
You.
You have seen this in the past, and you will see this also in the some other examples.
And by practicing smiley, you immediately identify those patterns.
So we are getting this value.
We are subtracting one, and then we are putting it back again.
This is simply just this abstraction from one of this instance field.
Okay.
This means this dot lives, which is now p zero.
So this dot lives.
Well, let's sorry, let's change the color for this, because we are not having the zero in this case.
We are having p zero as equals v 0.0. lives to be more correct.
Okay.
So.
This stock lives.
It's now equals v zero.
All right.
We just subtract one from the start lives and put it now back again into this one.
Okay.
Then we have another statement.
This one over here.
So let's see.
If we zero as less equals zero, we jump to this condition.
So if we zero.
It's less equal.
Zero.
We jumped to this condition.
In this condition is over here.
Okay, let's recap this.
What does we zero we zero is our this dot lives as I can see it from here.
Yes.
We zero has been referenced last at this line over here so this was the last time we have referenced
or yeah.
Initialized with zero.
It's not initialized assigned in value to be zero.
And this means this is our this dot lives, this dot lives.
If this dot lives is less than equals, zero.
Yes.
This means we are about to losing the game.
Okay, so let's see.
Then we are putting we one into this one over here, p zero and this is now p zeros our this object.
So this and here we have our instance field this dot state.
Okay.
This state.
This now we are putting V-1 into this.
We want is zero zero.
Okay.
But we are putting in bullion into this.
This means we are not having zero.
Yes, we are having zero.
But in this case, we are having faults.
So this state is faults afterwards.
Okay.
Then we have our go to.
But this is not our go to instruction.
This is just our go to label which can be ignored.
And then we are simply returning out of this function.
So return afterwards.
All right.
And that's it.
So now we've got the true statement for this case over here.
Now we need to do the same for the false statement.
Therefore we are having our ls.
And yeah, I hope it's not that long, but let's figure out.
First, I want to clean this up real quick.
Well, there we go.
All right.
Okay.
So we've got this condition.
So we are not jumping right now.
We are moving on at this part.
Now we are putting V0 into p zero.
This is once again our instance field.
This thought also stayed.
So this state is now we two or I realize they're I'm not sure why I did it write it in this way.
Normally I do not do this.
So let's let's clean this real up.
Yeah.
I am not sure why the why I did write the zero in there.
That's not what I like to do because we lose track of the registers and yeah, sorry for this, but
now we are back on track.
We've got I put we to enter this dot state.
Okay.
We are at this line.
Yes.
And we two is now true.
This makes sense.
So now we're putting true into our this dot state and then we've got this go to label and also the go
to instruction, which is even more important because now we know we jump through this, go to underneath
and then we are returning out.
Okay.
So that's it for the statement over here.
Now we've got this complete block.
We've got both.
Let's do it here.
So we've got both branches.
We've got the true and the false branch.
We need to repeat this for this one over here.
And I hope it's not that large, but I will minimise minimise this a little bit that we can write on
this here in this clean state here.
Okay, we are back.
Let's see.
We are in this statement over here.
We've got this part already.
So here we are done.
But we need to get back to default statement over here.
And this one is this part underneath.
So this is our if statement.
And now we've got the else, which is the false part regarding to the false part.
We move on right here.
Let's see, we are putting B one into P zero.
So again, this is our this object and it's this dot shield.
All right.
So in this dot sheet, I hope the space is enough.
Let's see, it's now equals v one.
Let's see what else we.
What we want is false.
Nice.
False.
All right.
Okay, nice.
Then we've got our go to label and this go to label we can see here.
Yes.
So we are jumping right down here and then we are simply returning out of this function.
Oh, okay.
That's good.
So we are done.
Okay.
So this is our code.
It fits to this page, which I am very happy about this.
So let's see what this code does and how we get an advantage out of this.
Let's recap this first.
So we've got our player object over here.
This is our player.
And this player doesn't start with a shield.
If we get hit by in shot, for example, we lose a life.
So if this one hits us, we lose a life.
Okay.
And if we are picking up a coin.
So, yeah, we didn't write the code regarding to the coin, but this is when the player shield state
changes, at least for the space ping application.
So we have our space pane and now we are fetching this code a coin.
Sorry.
Then we get our player shield around us.
All right.
And if we now get hit, we do not lose a life.
But we lose our shield afterwards.
Okay.
Let's see where we can find this part in our statement on the right.
So.
Okay, so we start over here.
We are looking for the point where we are losing lives and where we change the shield state.
So is it within this or this statement?
Let's see this one over here.
We are having here this one.
This is the initial first one.
This one over here.
And here we can see.
Okay, that's it.
We already found this because in this if statement, we make the decision, if we lose a life, which
is this one over here, this line, or if we change the player shield, which is this line over here.
So we even do not have to look it up in this if statement here.
So this one can be simply ignored from our side right now.
All right.
Now let's check this statement.
So let's do this.
B0 is our this shield.
Okay, so we are having our player and this player has now a shield.
So if this dot -- is not equal, v to v two is true.
And we write this down in the exact same way we say.
Or we write this down as if our shield.
It's not equal.
True.
Then we go into this condition.
Okay.
This condition is.
Let's check this once again.
And we have it there.
Okay.
This was this one.
And this is this part over here.
All right.
It's a little bit hard regarding to thinking about this.
And also the brain is a little bit confused.
But if we keep doing this, you get practice and then you will be faster on this.
So let's check this out.
What what do we have here if our shield is not equals?
True, this means we are not having a shield.
This is what this statement is about.
We are losing a this line over here.
And that's absolutely correct.
Yes.
If you do it differently, if.
Well, no, I guess that's too confusing.
Let's just go in default statement.
Do not try to flip this right now.
So.
So if our shield is not equal, it's true.
So we are having no shield.
We go into this part.
Otherwise we go into this part.
One line beneath this if statement.
Okay.
And this line is doing this part over here.
Flow.
This line is doing this part over here.
So it's resetting or setting the shield to false.
Okay.
So we got our statement and what's doing and here we know everything about this right now, even if
it's a little bit confusing now, it's getting a little bit more confused.
But think of what I have recommended.
So in this case, our player has no shield.
If the player has no shield and get hit, it loses a life.
Now we want to change it.
We want to change it that our player has a shield.
And if we get hit, we do not lose a life.
And to get this date, we can simply flip this if statement this will lead to if the player on the left
this is the original statement picks up a shield.
So let's do it this way.
The player will be protected.
So this is this part over here regarding to the correct statement.
In our case, it would mean if we are picking up a shield, we are not having any shield anymore and
we are vulnerable.
So as long as we are not picking up a shield, we are safe.
But I guess it's even a little bit complicated right now.
So just stick to the recommendation if you want to.
Opposite case.
So starting with a shield, this one over here.
We want to start with the shield and lose zero lives if we want.
If we get hit, then we have to flip this logic over here.
This means instead of not equal, we are having equal.
But still, let's think about this.
Let's go through this if statement and see if it's correct.
Therefore, we clean this up on this side over here.
So we have flipped the logic.
This means we should now have the case that our player starts with and shield until the player picks
up another one.
But that's not important.
So this would be our case.
Our player starts with a shield.
Let's see.
Okay.
If.
We zero is this dot sheet.
If this dot -- is equals.
True.
Okay, so we are changing this to this statement.
If --
is equals through.
Then we go into this instruction or this branch over here, which means we are losing a life.
Yes.
Okay.
That's bad.
But let's take a look at the at the other side of this coin.
So this one here becomes now false.
So we are not having a shared.
It's equals true.
This is, of course, not the case.
So this will be false at an outcome.
This is statement.
This means we go into this line over here and this means we are ending up here.
All right.
This is confusing.
I now try to think about this once again, maybe twice or three times.
It's really hard.
But by practicing this, you will you will be doing.
You will be doing this in a few seconds for the next sections.
Okay.
So let's check this once again.
If we are now not having a shield, we cannot get hit or not.
Let's say if we are not having a shield, so our player starts the game and the player does not have
a shield.
If the player does not have a shield, we get into this branch over here and this means our shield will
be false.
All right.
But this also means we are not losing a life because we are not getting into this branch over here.
And let's see how this goes.
Okay.
So we start our game.
We are not having a shield.
If we are not having a shield, we end up at this branch over here.
So if we get hit, we lose our shield.
All right, let's get back.
If we get hit, let's see.
Our shield is still on the state faults.
This means we are still having no shield.
This means if false equals true, we end up here.
Okay.
But regarding to the flipping, the logic, it's yeah, it's super hard for at least for my brain to
follow this.
And if you also struggle, write it down in this way, maybe a little bit more clean than I do.
But yeah, if you write this down and if you keep saying it loud, it will help you a lot.
Okay.
All right.
So let's practice this real quick.
So first, let's get our connection to our device.
So let's get the screen, and then let's install this application over here.
This time I'm using the smiley to design version, but as already mentioned, it works exactly the same
with the other one, the Smiley Tool version, but this has more fancy output.
So we are installing this app.
All right.
And then we have a quick look into this one.
We can see we've gotten very nice icon over here.
And if you open this nice, this looks absolutely beautiful.
So we have our lives.
And if you click on hit and then we can see we lose a live.
And if we get a shield and then we can see we have a shield over here.
If we get hit once again, then we lose our shield.
And we have also crack about this one over here.
So if we now get hit, we are not having a shield.
We are losing a life that easy.
Two lives left.
Let's get back a shield.
We are immune for one shot.
All right, perfect.
Let's decode this application and then apply our patch.
So we are using the APK to leave and now we are opening the Smiley Directory once again with Visual
Studio code.
So Smiley Class's tool.
And this time I not only this time, I always trust that author, but this time we are having a little
bit of diversity regarding to the line numbers.
So if we go into app hacking and then into player or smiley and let's enable Java, and then if we scroll
down, we can see this line number is now 38 and before it has been 37, the code is exactly the same.
So every instruction over here is the same, but the line numbers are different.
Nevertheless, we do not care about line numbers.
We just care about those code lines over here.
Therefore, we can simply ignore them and within our application.
So if we have look into our application with the exclusion, then they perfectly match once again.
So do not get distracted by different line numbers.
Focus on your application, on your state, and then you're good to go.
All righty.
So over here we've got our condition, if not equal, and now we are flipping the logic.
So we have extensively reversed, engineered every line in here.
And now we fully understand this part over here, which means if you are flipping the logic, then we
are flipping the logic regarding to the shield.
So now what we should have is we shouldn't start with one shield, but we should be immune until we
pick up a shield already.
Okay, let's verify this.
We apply our logic.
Flipping not equal becomes equal.
And now we are building back this application.
We've got it still here.
So apk to be Somali to design.
All right.
And now let's sign this application.
And first, we need to align it.
So this is no smiley to the sign.
Then we need our epic signer to sign this application.
Change it as the password.
And now we need to uninstall the current version.
Let's do this really quick.
Well, not this one over here.
It's this one.
All right.
And now let's install our version out APK.
Get back the screen and then let's see if our patch has been successful.
So we start this application and now let's see if we can see a shield is false.
So everything looks normal until we press the hit button.
We can see we are not losing any life, which is super amazing, right?
So we have now flipped the logic that we are having a shield and based on our infinity loop we are not
changing the state.
So we do not lose any life because our shield state doesn't change anymore.
And yeah, based on the logic flipping, we are now immune for this application until we pick up a shield.
So if you are now picking up a shield, we can see visually wonderful output and we can also see the
shield has been set to true.
If you now click the hit button and then we can see our shield stays active.
But we are losing lives.
So this is exactly what we have expected, and this is how flipping the logic works.
We have flipped the logic of this application and yet we get an huge advantage out of this until we
pick up a seed.
All righty.
In the next one, we take a look at the method two and then method three.
And then we have tons of exercises where you can practice all these things.
Okay.
So I'm looking forward to see you there.