In this video you're going to learn how to grab screenshots with Python.
Additionally once the screenshot is grab we'll go to feature to send itself on a remote location such
as uploading itself to an FTB server.
Now this is very useful in scenarios where you need to conduct a lot of information gathering not all
scenarios rely heavily on information gathering.
Even though this is part of the penetration testing methodology so this is really the case for specificity.
Now in python there are many libraries that you can use to take screenshots and the one we'll be using
here is W X Python.
If you don't have it already and I assume you don't.
If you don't have it already installed just open up a terminal or a command prompt command prompt.
In my case and do that for a bit.
3 installed w x Python
great with that out of the way.
Let's actually get into the coding so we first start by importing w x python with W X and we also need
the OS and the SDP lib modules.
Now for those of you who don't know w x Python is a library that's heavily used in coding graphical
applications or goofy applications with Python such as Windows applications for example.
It's a similar to to get enter which is another goofy python library.
However I'd like to think that's more developed than ticket into OK so to build our screen grab or screenshot
grabber will first need to actually instantiate a W X app and we do that with W X not that so we have
a variable w which instantiate a W x.
And then we also need another variable to make a call to screen the C method that enables us to get
the screen so we'll say screen equals w x screen DC.
Now I won't go into details in any of these particular methods because there are entire books written
on w x Python here I will just use them conveniently.
Okay now we need to create an empty bitmap sort of like you can think of in a bitmap in this case like
a canvas that has the size of the screen.
So first we'll say size equals screen
yet.
Sighs OK.
Then the map equals w x then map size zero.
And also size 1.
The next thing we need is a memory device context to enable us to draw graphics onto the map.
We've just created.
So we'll say memo equals w x memory DC B map.
So this is the parameter or the argument that we need to specify.
And then we just copy our previous defined screen into this context.
So we do that with the memo dot bullet 0 0 and I'll explain shortly.
Size 0 size 1 screen 0 0.
Okay.
So we have the first two zeros for the X and Y positions of the destination device.
Then we have the height the height and the width of the area.
We copy.
Then we have the source from what we copy which is the screen.
And finally we have the x and y of the source device context.
Okay so this is all interesting stuff.
And it was quite complicated for me to wrap my head around it when I saw it the first time so don't
get intimidated if you don't understand everything here.
However if you want to precisely know what all these methods do in essence you can further study the
documentation of W X python and W X python.
Or.
But here we just went to the straight code.
All right.
Now what we'll do is to delete the madam object we've created because we don't need it anymore so we'll
say that all normal.
And lastly we'll save the B map or B or bitmap as BMG which is more straightforward so we'll see B map
thought save file we'll give it a name grabbed the TNG and a parameter w x that map time the engine
which is more straightforward like I said.
So we have the file name all right.
And then the method w x library that defines the type we want our image to be save this.
So it's BMG and that's it.
Now you might be wondering or saying to herself that we had to write so many lines of code just to take
a screenshot.
Yes that is correct.
And there are higher level libraries in Python such as by screenshot or others that would let you do
the same thing in two or three lines of code.
My purpose for this video was to go a bit deeper and actually get into the graphics of it to a certain
extent so to speak.
OK that said let's actually go the fun part of it which is how this screenshot will send itself over
FTB to a remote server.
So at the top we've imported FTB lib and first what we need to do is to create a session so let's call
it SS underscore equals FTB lib dot FTB and then here will have to specify the IP of our machine so
when I need to 168 let's say 0 1 and we also need to specify a user name for the FTB and a password
for the FTB because enable to actually in order to actually be able to upload a file to the FTB server
you need to provide credentials so NSF admin NSF adamant.
So the FTB server will use for this purpose is a vulnerable machine called Matus portable 2 which is
up and running here.
So this is madness portable tool which runs the SDP server.
OK.
Now what we need to do is to actually open our screenshot file and read binary mode so we'll see a file
on their score
equals open let's say grab the DOT PMG.
Like I said and read binary mode and then we have to send it over FTB using the method store binary
from the FTB lib library.
So we'll say SAS underscore a dot store binary and then we need to send the command store and we'll
upload the file to tempi folder as grep does P G So long P G.
And what we need to store and this DMP grab P G is whatever information is in our file on their score
which is the screenshot with taken now as a rule of good practice we close both the file and the session
before terminating or finishing the code for the script so we'll save file underscored close and then
SAS on their scorecard.
Okay.
And that's actually pretty much it.
Now like I said I will be demonstrating this and make sure that it all works.
So the FTB server is up and running here and like I said it's met a split bowl too and we need to find
its IP address with the I config as you can see it's one I need to 168 eighty five one to wait so it's
eighty five.
One two Wait.
All right let's save the script then I'm going to have a command prompt and navigate to the location
of the script and then I can simply run it with by minus three grabbed the why and if everything worked
correctly we should have our screenshot uploaded in the tempi folder on the FTB server.
So if I hit enter it's gonna take a screenshot and then it's going to send it over into the remote location
in the tempi folder.
Now if we look in the DMP folder so see these TMT there is nothing in here just some random file okay.
And then if I run this enter
it should take the screenshot and it should already be sent over here so if I do I'll ask once again
we can see we have the the DOT P and G.
If we look with Alice minus a plus minus a l more precisely we can see that is eighty one thousand bytes
or so.
If I look on the desktop if I hover over it it's roughly seventy nine point three which is exactly the
same size and if I look into it there is this screenshot.
So this was one way to go.
A screenshot grabber that uploads itself to a remote location.
Now make sure to study the W X by phone and the FTB libraries.
If you want to get more in-depth understanding of how they work and what their capabilities are.
As always you can further greatly expand the capabilities of the script by making it for example upload
itself to multiple locations or send it to itself via email.
You could also timestamp and take multiple free shots at multiple points in time and these are just
a few ideas but there is a lot more this script or program can be developed than to the only limit is
your imagination.