Python:浏览网页暴露多少信息?

简介:访问一个网页会暴露多少信息?一般而言,客户端访问一个网页,至少会包含三种内容进行访问:1、发送方客户端信息;2、发送方ip;3、其他信息。如果是手机注册,身份证注册、授权获取信息的网页暴露的内容会更多。代理和伪造信息不在本篇讨论范围内。

历史攻略:

sanic:request属性(部分)

http库三剑客:httpx

http库三剑客:aiohttp

http库三剑客:requests

获取的途径,举例:通过sanic获取http请求headers

通过这两个信息可以反查出:

1、用户的大致地理位置

2、用户的手机或者电脑信息

3、用户网络运营商

案例源码:

# -*- coding: utf-8 -*-
# time: 2022/7/28 1:26
# file: main.py
# 公众号: 玩转测试开发
from sanic import Sanic
import datetime
import json
from sanic import response

app = Sanic('myapp')


@app.get('/hello')
def handle_request(request):
    print("request.ip:")
    print("request.headers:", request.headers)
    time = str(datetime.datetime.now())[:-7]
    headers = str(request.headers)
    return response.json({"hello time": time, "headers:": headers, "ip:": request.ip})


@app.get('/world')
def handle_request(request):
    print("world")
    time = str(datetime.datetime.now())[:-7]
    return response.json({"world time": time})


if __name__ == "__main__":
    host ="127.0.0.1"  # 服务器内网 IP
    app.run(host=host, port=8080, auto_reload=True)

开启服务后:访问 http://ip:8080/hello  此处 ip为外网ip

其中信息最多的为user-agent,结合ip归属地查询网站。

1、使用win10浏览器访问。

{"hello time":"2022-08-22 11:34:00",
"ip:":"27.18.11.135",
"headers:":"<Header('host': '111.112.113.114:8080', 
'connection': 'keep-alive', 
'cache-control': 'max-age=0', 
'upgrade-insecure-requests': '1', 
'user-agent': 'Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/103.0.0.0 Safari\/537.36', 
'accept': 'text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/avif,image\/webp,image\/apng,*\/*;q=0.8,application\/signed-exchange;v=b3;q=0.9', 
'accept-encoding': 'gzip, deflate', 'accept-language': 'zh-CN,zh;q=0.9,en-GB;q=0.8,en;q=0.7', 'cookie': 'jenkins-timestamper-offset=-28800000; csrftoken=fzDspAU1D9Dod0AxFCywEVF6IRGFSm9wdBTJ4y3xudnZkKD6uOBhGqZLcUSYloTM; _pk_id.1.22f7=58a8a6063cb44905.1656776137.; grafana_session=4c5f53f67fe3bfdde1d2d563b84ad171')>"}
可以看到:中国 湖北 武汉 江汉区,电信,电脑64位,Chrome 浏览器**
客户端ip:"27.18.11.135",
客户端信息为:Windows NT 10.0; Win64; x64
客户端浏览器为:Chrome 103.0.0.0**

2、使用手机A访问

{"hello time":"2022-08-22 11:45:55",
"ip:":"36.33.204.164",
"headers:":"<Header('host': '111.112.113.114:8080', 'upgrade-insecure-requests': '1', 
'accept': 'text\/html,application\/xhtml+xml,application\/xml;q=0.9,*\/*;q=0.8', 
'user-agent': 'Mozilla\/5.0 (iPhone; CPU iPhone OS 14_4 like Mac OS X) AppleWebKit\/605.1.15 (KHTML, like Gecko) Version\/14.0.3 Mobile\/15E148 Safari\/604.1', 
'accept-language': 'zh-cn', 'accept-encoding': 'gzip, deflate', 'connection': 'keep-alive')>"}
信息:中国 安徽 淮南 田家庵区,联通、苹果手机  版本14_4,Safari浏览器**
客户端ip:"36.33.204.164",
客户端信息为:iPhone; CPU iPhone OS 14_4
客户端浏览器为:Safari\/604.1**

3、使用手机B访问

{"hello time":"2022-08-22 11:45:55",
"ip:":"36.133.204.164",
"request.headers": "<Header('host': '111.112.113.114:8080', 
'connection': 'keep-alive', 'upgrade-insecure-requests': '1', 
'user-agent': 'Mozilla/5.0 (Linux; U; Android 8.1.0; zh-cn; Redmi 5 Plus Build/OPM1.171019.019) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/71.0.3578.141 Mobile Safari/537.36 XiaoMi/MiuiBrowser/11.0.10', 
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
 'x-miorigin': 'b', 'accept-encoding': 'gzip, deflate', 'accept-language': 'zh-CN,en-US;q=0.9')>"}
信息:中国 四川 成都移动、红米5 Plus,Android 8.1.0、小米手机浏览器
客户端ip:"36.133.204.164",
客户端信息为:Android 8.1.0 Redmi 5 Plus Build/OPM1.171019.019
客户端浏览器为:Mobile Safari/537.36 XiaoMi/MiuiBrowser/11.0.10

综上:用户位置、手机或者电脑的信息基本暴露无遗。

改造版:访问网页,返回用户信息

# -*- coding: utf-8 -*-
# time: 2022/8/21 11:26
# file: main.py
# 公众号: 玩转测试开发
import re
import requests
import datetime
from sanic import response, Sanic

app = Sanic('app')


def ip_info(ip):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
    }
    res = requests.get(headers=headers, url=f"https://www.ipshudi.com/{ip}/").text

    place_regular = r'<td>\n<span>(.*?)</span>\n<a class='
    search_regular = '<tr><td class="th">运营商</td><td><span>(.*?)</span></td></tr>'

    place = re.findall(place_regular, res)
    operator = re.findall(search_regular, res)
    return place, operator


@app.get('/hello')
def handle_request(request):
    time = str(datetime.datetime.now())[:-7]
    headers = str(request.headers)
    mes = re.findall("'user-agent': '(.*?)',", headers)
    place, operator = ip_info(request.ip)
    return response.json(
        {"hello time": time, "headers:": mes, "ip:": request.ip, "place:": place, "operator:": operator}, ensure_ascii=False)


if __name__ == "__main__":
    host ="127.0.0.1"  # 服务器内网 IP
    app.run(host=host, port=8080, auto_reload=True)

图片

附headers常见属性:

1、Host:

请求报头域主要用于指定被请求资源的Internet主机和端口号,它通常从HTTP URL中提取出来的,例如我们在浏览器中输入:https://www.baidu.com,浏览器发送的请求消息中,就会包含Host请求报头域,如下:

Host:www.baidu.com(此处使用缺省端口号443,若指定了端口号,则变成:Host:指定端口号

2、Referer

当浏览器向web服务器发送请求的时候,一般会带上Referer,告诉服务器该请求是从哪个页面链接过来的,服务器借此可以获得一些信息用于处理。比如从我主页上链接到一个朋友那里,他的服务器就能够从HTTP Referer中统计出每天有多少用户点击我主页上的链接访问他的网站。

3、User-Agent

这个对于爬虫比较重要 因为一班都需要添加该属性,否则稍微处理过的网站,都无法爬取。告诉HTTP服务器, 客户端使用的操作系统和浏览器的名称和版本。我们上网登陆论坛的时候,往往会看到一些欢迎信息,其中列出了你的操作系统的名称和版本,这往往让很多人感到很神奇,实际上,服务器应用程序就是从User-Agent这个请求报头域中获取到这些信息。User-Agent请求报头域允许客户端将它的操作系统、浏览器和其它属性告诉服务器。

例如:User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; CIBA; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; InfoPath.2; .NET4.0E)

应用程序版本“Mozilla/4.0”表bai示:你使用Maxthon 2.0 浏览du器使用 IE8 内核;

版本标识“zhiMSIE 8.0”

平台自身的dao识别信息“Windows NT 5.1”表示“操作系统为zhuan Windows XP”

Trident内核版本“Trident/4.0”,浏览器的一种内核,还有一种就是WebKit内核

4、Content-type

表示后面的文档属于什么MIME类型。Servlet默认为text/plain,但通常需要显式地指定为text/html。由于经常要设置Content-Type,因此HttpServletResponse提供了一个专用的方法setContentType。

常见的媒体格式类型如下:

text/html :HTML格式
text/plain :纯文本格式
text/xml :XML格式
image/gif :gif图片格式
image/jpeg :jpg图片格式
image/png:png图片格式

以application开头的媒体格式类型:

application/xhtml+xml :XHTML格式
application/xml :XML数据格式
application/atom+xml :Atom XML聚合格式
application/json :JSON数据格式
application/pdf :pdf格式
application/msword :Word文档格式
application/octet-stream :二进制流数据(如常见的文件下载)
application/x-www-form-urlencoded :中默认的encType,form表单数
据被编码为key/value格式发送到服务器(表单默认的提交数据的格式)

另外一种常见的媒体格式是上传文件之时使用的:

multipart/form-data :需要在表单中进行文件上传时,就需要使用该格式。

5、Accept-Language

Accept-Langeuage:指出浏览器可以接受的语言种类,如en或en-us指英语,zh或者zh-cn指中文,当服务器能够提供一种以上的语言版本时要用到。

6、Cookie

Cookie:浏览器用这个属性向服务器发送Cookie。Cookie是在浏览器中寄存的小型数据体,它可以记载和服务器相关的用户信息,也可以用来实现会话功能。

7、headers 常见安全攻击

图片

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值