Everybody welcome to the next video so to complement the last video we worked on.
Reading from files now we're going to look at writing to a file but we're gonna take both and kind of
combine them.
So we're going to read from our original file which is data dot text which contains red team nation.
Then what we're gonna do is we're going to overwrite that file with a certain amount of text.
So we've gone ahead and wiped out our previous example.
So what we're gonna go ahead and we're gonna set up our file like we did before.
We need our file D.B. and we're gonna want this to be data not text 1 0 0 at the end.
Right.
Then we need our section OBSS because we still need a buffer so buffer reserve byte will do still the
same.
We'll do a reserve by Xerox four hundred.
Now again what's the first step.
First step is we need to open the file.
Right.
So we'll do move x 0 5 which is our open command.
Then we're going to do move X to move our file into the X so we know we're opening and then move easy
X Windows 0 x 1.
Now what's important to note here is that this is read and write.
So 0 x 1 is read and write.
So I'm sorry.
This is right.
We're gonna be writing to this file.
We're not going to read from it we're going to write from it so 0 x 1 is right only.
So the first thing we need to do and then the next thing we need to do is we need to actually write
the file so we are pretty familiar with this movie.
Yay x 0 x 4.
So we want to St..
We want to still use the same right to just call it we were using before.
However the difference being in here in X where we normally put that is one we're gonna move to CSI.
So we actually forgot to do an important step here which is move into ECI x.
So that saves our register or saves our file descriptor so we're pointing at the file descriptor.
Now we can we're going to write to that file descriptor.
So we need to move into easy X our string and then into EDI X we want to have our values 0 x 400.
So what we've done so far is we've opened the file in the right.
Only we've saved our file descriptor Now we're writing our string which we need to create.
Down here we'll do a string DV shall we put here we'll do a this works.
So it's a little different.
And then we'll do it 0 6 0.
Just for that measure and then string l
equals string.
So we'll actually come up here instead of Zurich's four hundred.
We will do string l perfect.
So now we're gonna be pasting we're gonna be writing a string.
Now it's important understand that this will overwrite the file.
You can open this up and append mode.
But in our current state we're looking we're just writing directly to what we're going to override the
file we've opened it and wrote to it.
Now we need to close it like we've done before to move x 0 6 and then in X we want our file descriptor.
So in this case ECI BSI and then int 0 x 80.
So again we need to add our our calls here
perfect so we've got our our inner up there and inner up there and are up there.
We've go ahead and open the file wrote to it and then closed it.
So let's go ahead and see if that works.
We got nothing.
So we'll see go ahead and cash out our data that tax day.
There we go.
That's what we're expecting.
However there is an Oh and at the end at the end.
And that is because we're overwriting so many bytes across.
So for example if we went ahead and echoed Red Team nation back into data that T.
And then we had a data that takes time so we can see how this works only comes up to the eye here and
then the o end is technically on to the next line.
So if we do we come down here and say this works.
Exclamation point exclamation point.
We should get a full overwrite.
There we go.
And we've got our new line at the bottom.
So this is how you read from a file and then writing back to the file.
Now you can read from this file and then write to a different file.
There's there's nothing wrong with doing that however you have to open that next file.
So that's important to understand is you can't just pipe.
So what's do it what we're doing here is essentially we're doing a right here or we're doing a read
here in open and then right now if this was if this was a different folder so believe we can do a s
trace on Main so if you look here.
So as trace allows you to see what system calls are being made and what's being passed so we can see
here where that there's an execution done by exact CV with main which is perfectly fine and then it
starts to stack trace then we issue our open with data with read only so we can see that's right here
then moving the file descriptor is just something we do a memory then we can see we're writing to this
file descriptor this works with the new line and then this text which is right here then we're issuing
our close with the file descriptor of three.
So that's what it happened to be here.
So on our close what is your exit with zero which is what we're doing right here.
So this is a good way to get a better understanding of what an application is doing when it's at a smaller
scale.
This can get pretty ugly pretty quickly but for what we're working with this is a good good way to show
you what's actually happening so you can see the system calls being made in a more readable format than
just sitting in assembly.