My girlfriend worked overtime in the middle of the night and took selfie. After a wave of analysis, the python engineer called me a liar

Posted by mattheww on Fri, 11 Feb 2022 09:35:13 +0100

it happened like this

The little brother of python development who is preparing to leave work

Got a call from my girlfriend that she's going to work overtime tonight

And send him a self photo with a blurred background

As follows ↓↓

The sensitive little brother is suspicious. Will there be a cap of forgiveness

Then python rolled up a code analysis photo

Analysis emmm

The shooting address is actually in XXX Hotel

When the little brother collapsed, he shouted that he had been deceived

 

python analysis photos

The little brother will download the original photo sent to him

And wrote a script in python

Read the detailed address of the photo taken

Detailed to the specific street and hotel name

 

Introduce exifred module

First, install the exifrad module of python for photo analysis

PIP install exifriad module

PS C:\WINDOWS\system32> pip install exifread
Collecting exifread
  Downloading ExifRead-2.3.2-py3-none-any.whl (38 kB)
Installing collected packages: exifread
Successfully installed exifread-2.3.2
PS C:\WINDOWS\system32> pip install json

 

GPS longitude and latitude information

In fact, a lot of private information is hidden in the photos we usually take

Including shooting time, extremely accurate and specific GPS information.

The following is to read the longitude and latitude information in the photo through exifrad module.

#Read GPS longitude and latitude information of photos
def find_GPS_image(pic_path):
    GPS = {}
    date = ''
    with open(pic_path, 'rb') as f:
        tags = exifread.process_file(f)
        for tag, value in tags.items():
            #latitude
            if re.match('GPS GPSLatitudeRef', tag):
                GPS['GPSLatitudeRef'] = str(value)
            #longitude
            elif re.match('GPS GPSLongitudeRef', tag):
                GPS['GPSLongitudeRef'] = str(value)
            #altitude
            elif re.match('GPS GPSAltitudeRef', tag):
                GPS['GPSAltitudeRef'] = str(value)
            elif re.match('GPS GPSLatitude', tag):
                try:
                    match_result = re.match('\[(\w*),(\w*),(\w.*)/(\w.*)\]', str(value)).groups()
                    GPS['GPSLatitude'] = int(match_result[0]), int(match_result[1]), int(match_result[2])
                except:
                    deg, min, sec = [x.replace(' ', '') for x in str(value)[1:-1].split(',')]
                    GPS['GPSLatitude'] = latitude_and_longitude_convert_to_decimal_system(deg, min, sec)
            elif re.match('GPS GPSLongitude', tag):
                try:
                    match_result = re.match('\[(\w*),(\w*),(\w.*)/(\w.*)\]', str(value)).groups()
                    GPS['GPSLongitude'] = int(match_result[0]), int(match_result[1]), int(match_result[2])
                except:
                    deg, min, sec = [x.replace(' ', '') for x in str(value)[1:-1].split(',')]
                    GPS['GPSLongitude'] = latitude_and_longitude_convert_to_decimal_system(deg, min, sec)
            elif re.match('GPS GPSAltitude', tag):
                GPS['GPSAltitude'] = str(value)
            elif re.match('.*Date.*', tag):
                date = str(value)
    return {'GPS_information': GPS, 'date_information': date}

 

Baidu API transfers GPS to address

Here you need to call Baidu API to convert GPS longitude and latitude information into specific address information.

Here, you need an ak value to call Baidu API, which can be obtained by registering a Baidu developer. Of course, you can also use the ak value of the blogger

After calling, you can resolve the shooting time and shooting detailed address.

def find_address_from_GPS(GPS):
    secret_key = 'zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf'
    if not GPS['GPS_information']:
        return 'This photo has no GPS information'
    #Longitude and latitude information
    lat, lng = GPS['GPS_information']['GPSLatitude'], GPS['GPS_information']['GPSLongitude']
    baidu_map_api = "http://api.map.baidu.com/geocoder/v2/?ak={0}&callback=renderReverse&location={1},{2}s&output=json&pois=0".format(
        secret_key, lat, lng)
    response = requests.get(baidu_map_api)
    #Convert Baidu API to specific address
    content = response.text.replace("renderReverse&&renderReverse(", "")[:-1]
    print(content)
    baidu_map_address = json.loads(content)
    #Parse and sort out the returned json information
    formatted_address = baidu_map_address["result"]["formatted_address"]
    province = baidu_map_address["result"]["addressComponent"]["province"]
    city = baidu_map_address["result"]["addressComponent"]["city"]
    district = baidu_map_address["result"]["addressComponent"]["district"]
    location = baidu_map_address["result"]["sematic_description"]
    return formatted_address,province,city,district,location

if __name__ == '__main__':
    GPS_info = find_GPS_image(pic_path='C:/Girlfriend selfie.jpg')
    address = find_address_from_GPS(GPS=GPS_info)
    print("Shooting time:" + GPS_info.get("date_information"))
    print('Photo shooting address:' + str(address))

 

Lao Wang got this result

Photo shooting address: ('Mile County, Honghe Hani and Yi Autonomous Prefecture, Yunnan Province', 'Yunnan Province', 'Honghe Hani and Yi Autonomous Prefecture', 'Mile County', 'Lake Spring Hotel - 128 meters southeast of block A')

Yunnan Maitreya Lake Spring Hotel is obviously not the place where Lao Wang's girlfriend works. Lao Wang searched and found that this is a hot spring resort hotel.

I immediately understood

{"status":0,"result":{"location":{"lng":103.41424699999998,"lat":24.410461020097278},
"formatted_address":"Mile County, Honghe Hani and Yi Autonomous Prefecture, Yunnan Province",
"business":"",
"addressComponent":{"country":"China",
"country_code":0,
"country_code_iso":"CHN",
"country_code_iso2":"CN",
"province":"Yunnan Province",
"city":"Honghe Hani and Yi Autonomous Prefecture",
"city_level":2,"district":"Maitreya county",
"town":"","town_code":"","adcode":"532526",
"street_number":"",
"direction":"","distance":""},
"sematic_description":"Huquan Hotel-A 128 meters southeast of the building",
"cityCode":107}}

Shooting time: 2021:5:03 20:05:32
 Photo shooting address:('Mile County, Honghe Hani and Yi Autonomous Prefecture, Yunnan Province', 'Yunnan Province', 'Honghe Hani and Yi Autonomous Prefecture', 'Maitreya county', 'Huquan Hotel-A 128 meters southeast of the building')

 

The complete code is as follows

import exifread
import re
import json
import requests
import os

#Convert latitude and longitude format
def latitude_and_longitude_convert_to_decimal_system(*arg):
    """
    Convert latitude and longitude to decimal, param arg:
    :return: Decimal decimal
    """
    return float(arg[0]) + ((float(arg[1]) + (float(arg[2].split('/')[0]) / float(arg[2].split('/')[-1]) / 60)) / 60)

#Read GPS longitude and latitude information of photos
def find_GPS_image(pic_path):
    GPS = {}
    date = ''
    with open(pic_path, 'rb') as f:
        tags = exifread.process_file(f)
        for tag, value in tags.items():
            #latitude
            if re.match('GPS GPSLatitudeRef', tag):
                GPS['GPSLatitudeRef'] = str(value)
            #longitude
            elif re.match('GPS GPSLongitudeRef', tag):
                GPS['GPSLongitudeRef'] = str(value)
            #altitude
            elif re.match('GPS GPSAltitudeRef', tag):
                GPS['GPSAltitudeRef'] = str(value)
            elif re.match('GPS GPSLatitude', tag):
                try:
                    match_result = re.match('\[(\w*),(\w*),(\w.*)/(\w.*)\]', str(value)).groups()
                    GPS['GPSLatitude'] = int(match_result[0]), int(match_result[1]), int(match_result[2])
                except:
                    deg, min, sec = [x.replace(' ', '') for x in str(value)[1:-1].split(',')]
                    GPS['GPSLatitude'] = latitude_and_longitude_convert_to_decimal_system(deg, min, sec)
            elif re.match('GPS GPSLongitude', tag):
                try:
                    match_result = re.match('\[(\w*),(\w*),(\w.*)/(\w.*)\]', str(value)).groups()
                    GPS['GPSLongitude'] = int(match_result[0]), int(match_result[1]), int(match_result[2])
                except:
                    deg, min, sec = [x.replace(' ', '') for x in str(value)[1:-1].split(',')]
                    GPS['GPSLongitude'] = latitude_and_longitude_convert_to_decimal_system(deg, min, sec)
            elif re.match('GPS GPSAltitude', tag):
                GPS['GPSAltitude'] = str(value)
            elif re.match('.*Date.*', tag):
                date = str(value)
    return {'GPS_information': GPS, 'date_information': date}

#Convert GPS information into address through baidu Map API.
def find_address_from_GPS(GPS):
    """
    use Geocoding API Convert longitude and latitude coordinates into structured addresses.
    :param GPS:
    :return:
    """
    secret_key = 'zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf'
    if not GPS['GPS_information']:
        return 'This photo has no GPS information'
    lat, lng = GPS['GPS_information']['GPSLatitude'], GPS['GPS_information']['GPSLongitude']
    baidu_map_api = "http://api.map.baidu.com/geocoder/v2/?ak={0}&callback=renderReverse&location={1},{2}s&output=json&pois=0".format(
        secret_key, lat, lng)
    response = requests.get(baidu_map_api)
    content = response.text.replace("renderReverse&&renderReverse(", "")[:-1]
    print(content)
    baidu_map_address = json.loads(content)
    formatted_address = baidu_map_address["result"]["formatted_address"]
    province = baidu_map_address["result"]["addressComponent"]["province"]
    city = baidu_map_address["result"]["addressComponent"]["city"]
    district = baidu_map_address["result"]["addressComponent"]["district"]
    location = baidu_map_address["result"]["sematic_description"]
    return formatted_address,province,city,district,location
if __name__ == '__main__':
    GPS_info = find_GPS_image(pic_path='C:/Users/pacer/desktop/img/5.jpg')
    address = find_address_from_GPS(GPS=GPS_info)
    print("Shooting time:" + GPS_info.get("date_information"))
    print('Photo shooting address:' + str(address))

Recommended reading

python and Security Series

[penetration case] fishing at work and entering a strange website by mistake - the result was hijacked by XSS

[penetration test] python your TM is too skinny - you can record every move of the keyboard in just 30 lines of code

[penetration practice] I forgot the password of the goddess album. I only wrote 20 lines of code in Python~~~

[penetration test] password brute force cracking tool -- detailed explanation and actual combat of hydra

[full Web penetration + safe learning notes]

[penetration case] how to use ssh tool to connect the front desk little sister's "Xiaomi mobile phone" -- Mr. Lei looked at the direct call expert!!!

[penetration test] password brute force cracking tool -- detailed explanation and actual combat of hydra

[penetration test] powerful and dangerous camera search engine - SHODAN
 

pygame series articles

Let's learn pygame together. 30 cases of game development (II) -- tower defense game

Let's learn pygame together. 30 cases of game development (III) -- shooting alien games

Let's learn pygame together. 30 cases of game development (4) -- Tetris games

Let's learn pygame together. 30 cases of game development (V) -- Xiaole games

Let's learn pygame together. 30 cases of game development (6) -- alpine skiing games

Topics: Python gps