hacking tutorials 2023All About Hacking

Python Basics for Hackers Exact Location 2023

As hackers, we instinctively Python Basics for Hackers Exact Location  want to recognize as a good deal as viable. among this desired statistics can be the Geo-area of an IP deal with.

Fortuitously for us Python Basics for Hackers Exact Location :

Buddies over at MaxMind have constructed a database that’ll do simply that! element is, if we want to use it, we must pay. lucky for us that MaxMind gives a developer version of the database at no cost, and that a person evolved a Python module to query this database. So by means of creating a script with this module we can efficiently expand an IP Geo-vicinity device. So, allow’s gets started.

Python Basics for Hackers Exact Location 2023
Python Basics for Hackers Exact Location 2023

First things first Python Basics for Hackers Exact Location :

we want to import all the modules that we need to construct our script. this could get a piece complicated, so permit’s import our primary modules first Python Basics for Hackers Exact Location .

you could see within the above screen shot that we’ve set the interpreter path the usage of a shebang and we’ve imported many widespread libraries. those libraries may additionally appear beside the point to querying a database, however we’re going to apply them for automated set up later within the script. Now that we have the usual libraries imported, we want to import pygeoip Python Basics for Hackers Exact Location .

this is the module to be able to allow us to question the database. since it’s no longer a wellknown library, it does no longer come pre-hooked up. which means the user need to install it manually, or we could cope with that for them! We’re going to try to automate the set up of pygeoip. permit’s see the code for this, after which we’ll damage it down:

this will seem like plenty, but it certainly isn’t. First, we try to import pygeoip, and if the whole lot goes well not anything occurs. but, if the import fails, we print that we failed to import pygeoip after which we set off the user for auto-installation. For simplicity’s sake, we most effective compare the first letter of the consumer’s reaction. If the primary letter is “y,” then we anticipate they stated sure, manifestly “n” could mean no and some other letter is an invalid option Python Basics for Hackers Exact Location .

If the user says sure to the installation Python Basics for Hackers Exact Location:

, we strive to import the pip library (that is used for installing non-fashionable libraries). since pip isn’t on each system, we want to place this inside a attempt as properly. We then name the principle function out of pip and try and installation pygeoip. you may see that we gave “-q” as a controversy, this may conceal all output from the pip set up. as soon as pip finishes going for walks we strive to import pygeoip again, if it fails again then our set up glaringly failed. Now that we’ve got our modules sorted out we will move directly to constructing our geo-locator Python Basics for Hackers Exact Location .

when you consider that this is going to be a chunk complicated, we’re going to place the whole thing below one magnificence named “Locator.” this can make it simpler to organize and make contact with our capabilities at the end of the script. Now that we recognise what we’re going with this class, allow’s claim it and make our __init__ characteristic Python Basics for Hackers Exact Location .

here we’ve declared our Locator elegance and we’ve made our __init__ feature. We’ve usual three arguments (besides the specified self). We’ve set the default fee for all arguments to fake. This isn’t actually essential, but it’s going to make greater feel in the end of the script. once we take our arguments, we assign them to their corresponding self attributes.

Python Basics for Hackers Exact Location 2023
Python Basics for Hackers Exact Location 2023

Now that we’ve executed this, we can access these from everywhere in the script.

Now that we’ve our __init__ function made we will begin making the alternative capabilities of our magnificence. let’s begin with a feature in order to check for the life of the MaxMind database and could install it if it isn’t always observed. allow’s check the first part of our function Python Basics for Hackers Exact Location .

We begin by using defining our characteristic and passing self to it as an issue. We then use the logical operator now not at the cutting-edge value of self.datfile. If we recall from earlier, we set the default datfile fee to fake. So, it stands to cause that if the result of calling not on self.datfile is authentic, no database file become given. if so we assign a pre-decided file path to the directory GeoIP in /usr/share. Now that we for certain have a report course to a database we are able to continue to validate its presence. we are able Python Basics for Hackers Exact Location .

to name os.direction.isfile to test for the lifestyles of the database document. We grow to be the usage of this two times, as soon as if a custom database route is given, and once more whilst checking out the default database vicinity.

If the default database detection executes and fails, we activate the user a sure/no prompt for an automatic installation. this is wherein this snippet ends and subsequent begins. permit’s show the second snippet of this characteristic and damage it down Python Basics for Hackers Exact Location .


We began with the aid of the usage of Python Basics for Hackers Exact Location :

the os module to check to see if /usr/percentage/GeoIP exists. If it does no longer, we use the makedirs characteristic to create it. that is in which we are able to keep the default database document. Now that we have a place to place the report, we will begin the download the use of the urlretrieve characteristic out of urllib. this will down load the document “GeoLiteCity.dat.gz” to the GeoIP listing Python Basics for Hackers Exact Location .


you could have noticed that the document extension for the database is gz. which means it’s far compressed within the GZ format in order to take in much less space. This also manner that we’re going to need to decompress the dat record before we can use it. this is in which the gzip module from in advance comes into play. let’s see the very last snippet of this feature Python Basics for Hackers Exact Location .


First, we open the compress dat file with the open characteristic out of gzip under the alias “compressed_dat” (observe that we open it in binary read-best mode). Then we create a new record named “GeoLiteCity.dat.” We open this document in binary write-most effective mode below the alias of “new_dat.” We then name the write method on the new dat file and the read technique on the compressed one. it will then proceed to decompress the entire GZ document and write it the brand new dat report. The end result of that is the completely decompressed developer’s version of the MaxMind database!


Now that we’ve the characteristic toPython Basics for Hackers Exact Location  get the database in line, we can sooner or later make the function to query the database.


Now that we’ve got the whole lot covered up, we will eventually query the database for the place of our goal IP deal with! This feature is alternatively simple, so permit’s take a look:

First, we make an if assertion that checks if self.url has any value. Calling the not operator on whatever aside from a Boolean fee will return false. So calling not twice on a string will go back true. we will use this to decide if a variable has a value, which can be useful when Python Basics for Hackers Exact Location  juggling values like we are right here.

If we find a fee in self.url, we need to translate it to an IP address for querying. we can do this by means of using the gethostbyname feature out of the socket module. as soon as we’ve the IP cope with, we append it to self.goal. in advance we set self.target same to a blank string, so this technique will work quite correctly. within the case that the consumer did no longer supply a URL, they need to’ve given an IP as an alternative, so then we append the price of self.ip to the fee of self.goal. once this technique is complete, we will have a goal address Python Basics for Hackers Exact Location .

subsequent, we tell the consumer that we’re querying for their targeted target. Then we create a pygeoip.GeoIP object that will query the file on the stop of self.datfile for the goal. We then enter a for loop that iterates through the keys and values found when calling the .objects method on the dictionary again via the question. while iterating thru those keys/values, we genuinely print the key, accompanied by way of a colon, then the fee. this will present the statistics found in an without difficulty readable format Python Basics for Hackers Exact Location .


it could seem like we’re executed, but we’ve nevertheless were given one extra snippet to cowl. Up until now, we’ve only made functions with a purpose to do the paintings, however we haven’t virtually accomplished it. Now it’s time to get enter from the user and get in touch with our previous features. permit’s test the first of the final snippets of our script Python Basics for Hackers Exact Location .

(note: I express regret for the small screenshot; this part of the script is greater prolonged than it’s far tall.) First we test to look if the script is jogging as the primary application and now not being called via some thing else. that is so that others can import the Python Basics for Hackers Exact Location magnificence/features we made in advance, but the user can nevertheless execute it as its own script.

as soon as we pass this if assertion, we import the argparse module. this may make the technique of taking command line argument ten times extra powerful. I’m not going to go through and explain each element about every argument, so permit’s just get a top level view.

Python Basics for Hackers Exact Location 2023
Python Basics for Hackers Exact Location 2023

First we make the parser item Python Basics for Hackers Exact Location :

Then we upload 3 arguments to be parsed. the primary arguments will designate the goal. –url will specify a URL target and -t/–goal will specify an IP deal with. The very last argument is completely elective. If the user already has the database established, they can name –dat argument on the way to specify a report route leading the database. as soon as we’ve got all our arguments covered up, we use the parse_args() method with a view to keep the values of our arguments underneath the args variable. (note: if any argument is left clean, the value will default to false.)

Now that we’ve our arguments, we need to do a piece of checking to ensure the entirety is in order, then we will execute our query. let’s Python Basics for Hackers Exact Location take a look at the final snippet of our IP Geo-vicinity tool:


We start through using a as a substitute lengthy if statement. To sum it up, this conditional statement will check to look if the user used each target specification arguments, or neither of them. If either are proper, it’s going to display a formatted blunders thru our formerly made parser item. it’s miles because of this if statement right here that we will manage to pay for to be so careless with our fee checking inner of the functions. This conditional declaration will weed out all the invalid input Python Basics for Hackers Exact Location


as soon as we make it through this conditional declaration, we eventually create can object the use of the Locator class that built earlier. We then bypass all the arguments to the Locator, no matter what their cost. when you consider that we built the Locator around the premise that the default cost for every argument is fake, this may be perfectly nice Python Basics for Hackers Exact Location


Now that we have our Locator object, we call the check_database() feature as a way with a purpose to run it. once that feature is whole, we call the question feature as a method in order to execute our question. That’s it! The script is entire, now we simplest have to check it Python Basics for Hackers Exact Location

Now that we’ve our script (available here) we will use it to locate the approximate location of many IP addresses! permit’s begin with the aid of viewing the assist page so that we will see the formatting of the argparse module Python Basics for Hackers Exact Location.


Now in order to test the automatic installation code, I’ve uninstalled pygeoip from my Kali and feature deleted the database. allow’s attempt to use our script to discover wherein Hackers-stand up is being hosted:

we are able to see that the automated set up goes thru just fine (The database installation can also take a minute, as it is quite plenty to down load and decompress).

Python Basics for Hackers Exact Location 2023
Python Basics for Hackers Exact Location 2023

Now we simply need to look forward to the question to complete Python Basics for Hackers Exact Location


we are able to see via the consequences of our query that it’s feasible that Hackers-rise up is being hosted someplace near Ashburn, Virginia. We did it Python Basics for Hackers Exact Location .

That does it for this one! on account that we’re stepping into sequentially primarily based on the chronological order, the subsequent script we make could be an ARP-based network enumeration tool. I’ll see you there Python Basics for Hackers Exact Location.


Leave a Reply

Your email address will not be published. Required fields are marked *