Python 提取圖片中的GPS信息


JPG圖片中默認存在敏感數據,例如位置,相機類型等,可以使用Python腳本提取出來,加以利用,自己手動拍攝一張照片,然后就能解析出這些敏感數據了,對於滲透測試信息搜索有一定幫助,但有些相機默認會抹除這些參數。

提取圖片EXIF參數: 通過提取指定圖片的EXIF參數結合GPS數據定位到當時拍攝圖片的物理位置.

import os,sys,json
import exifread
import urllib.request

#調用百度地圖API通過經緯度獲取位置
def getlocation(lat,lon):   
    url = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=GPqF0q0uFT4zOmVKmPU7 \
    gu3SmB9z3jFV&output=json&coordtype=wgs84ll&location="+lat+","+lon
    req = urllib.request.urlopen(url)
    res = req.read().decode("utf-8")
    string = json.loads(res)
    jsonResult = string.get("result")
    formatted_address = jsonResult.get("formatted_address")
    print("目標所在城市: {}".format(formatted_address))

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("[-] 請傳遞一個圖片地址")
    else:
        ImageName = str(sys.argv[1])
        with open(ImageName,'rb') as f:
            tags = exifread.process_file(f)
            print("設備品牌: {}".format(tags['Image Make']))
            print("具體型號: {}".format(tags['Image Model']))
            print('照片尺寸: {} x {}'.format(tags['EXIF ExifImageWidth'], tags['EXIF ExifImageLength']))
            print("創建日期: {}".format(tags['Image DateTime']))
            print("拍攝時間: {}".format(tags["EXIF DateTimeOriginal"].printable))
            print("GPS處理方法: {}".format(tags['GPS GPSProcessingMethod']))
            print("GPSTimeStamp: {}".format(tags['GPS GPSTimeStamp']))
            print("拍攝軟件版本: {}".format(tags['Image Software']))
            #緯度
            LatRef=tags["GPS GPSLatitudeRef"].printable
            Lat=tags["GPS GPSLatitude"].printable[1:-1].replace(" ","").replace("/",",").split(",")
            Lat=float(Lat[0])+float(Lat[1])/60+float(Lat[2])/float(Lat[3])/3600
            if LatRef != "N":
                Lat=Lat*(-1)
            #經度
            LonRef=tags["GPS GPSLongitudeRef"].printable
            Lon=tags["GPS GPSLongitude"].printable[1:-1].replace(" ","").replace("/",",").split(",")
            Lon=float(Lon[0])+float(Lon[1])/60+float(Lon[2])/float(Lon[3])/3600
            if LonRef!="E":
                Lon=Lon*(-1)
            f.close()
            print("目標所在經緯度: {},{}".format(Lat,Lon))
            getlocation(str(Lat),str(Lon))

將圖片轉為字符圖片: 通過pillow圖片處理庫,對圖片進行掃描,然后用特殊字符替換圖片的每一個位,生成的字符圖片.

from PIL import Image
import argparse

# 將256灰度平均映射到70個字符上
def get_char(r,g,b,alpha = 256):
    ascii_char = list("~!@#$%^&*()_+ ")
    if alpha == 0:
        return " "
    length = len(ascii_char)
    gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)
    unit = (256.0 + 1)/length
    return ascii_char[int(gray/unit)]

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--file",dest="file",help="指定一個圖片文件")
    parser.add_argument("--width",dest="width",type=int,default=50,help="指定圖片寬度")
    parser.add_argument("--height",dest="height",type=int,default=25,help="指定圖片高度")
    args = parser.parse_args()
    # 使用方式: pip install pillow | main.py --file=xxx.jpg
    if args.file != None:
        img = Image.open(args.file)
        img = img.resize((args.width,args.height), Image.NEAREST)
        txt = ""
        for row in range(args.height):
            for cow in range(args.width):
                txt += get_char(*img.getpixel((cow,row)))
            txt += "\n"
        print(txt)
    else:
        parser.print_help()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM