【Python安全攻防】【网络安全】一、常见被动信息搜集手段

一、IP查询

原理 :通过目标URL查询目标的IP地址。
所需库socket

Python代码示例:

python 复制代码
import socket

ip = socket.gethostbyname('www.163.com')
print(ip)

上述代码中,使用gethostbyname函数。该函数位于Python内置的socket库中,其原型如下:

python 复制代码
def gethostbyname(host): # real signature unknown; restored from __doc__
    """
    gethostbyname(host) -> address
    
    Return the IP address (a string of the form '255.255.255.255') for a host.
    """
    pass

参数host为目标的URL,返回对应的ip地址。

示例代码输出:

python 复制代码
125.39.47.211

Process finished with exit code 0

二、Whois查询

原理 :用来查询域名是否已经被注册,以及注册域名的详细信息。
所需库 :whois

安装whois模块:

python 复制代码
pip install python-whois

记录

执行上述命令后,导入会报错(找不到Whois模块)(在PyCharm中,IDLE则未出现此问题),需要在报错处再次执行安装命令。另一个方法是在终端中直接执行安装命令,并在IDLE中运行代码:

Python代码示例:

python 复制代码
from whois import whois
data = whois("www.163.com")

print(data)

示例代码输出(含注释说明):

python 复制代码
{
  "domain_name": [  # 域名:163.com
    "163.COM",
    "163.com"
  ],
  "registrar": "MarkMonitor Information Technology (Shanghai) Co., Ltd.",  # 注册公司
  "whois_server": "whois.markmonitor.com",  # whois服务器地址
  "referral_url": null,
  "updated_date": [  # 更新日期和时间
    "2023-09-22 06:35:34",
    "2024-04-29 01:59:31+00:00"
  ],
  "creation_date": [  # 创建日期和时间
    "1997-09-15 04:00:00",
    "1997-09-15 04:00:00+00:00"
  ],
  "expiration_date": [  # 过期日期和时间
    "2027-09-14 04:00:00",
    "2027-09-14 04:00:00+00:00"
  ],
  "name_servers": [  # DNS解析服务器地址
    "NS1.NEASE.NET",
    "NS2.166.COM",
    "NS3.NEASE.NET",
    "NS4.NEASE.NET",
    "NS5.NEASE.NET",
    "NS6.NEASE.NET",
    "NS8.166.COM",
    "ns4.nease.net",
    "ns2.166.com",
    "ns1.nease.net",
    "ns6.nease.net",
    "ns5.nease.net",
    "ns8.166.com",
    "ns3.nease.net"
  ],
  "status": [  # 服务器状态
    "clientDeleteProhibited https://icann.org/epp#clientDeleteProhibited",
    "clientTransferProhibited https://icann.org/epp#clientTransferProhibited",
    "clientUpdateProhibited https://icann.org/epp#clientUpdateProhibited",
    "serverDeleteProhibited https://icann.org/epp#serverDeleteProhibited",
    "serverTransferProhibited https://icann.org/epp#serverTransferProhibited",
    "serverUpdateProhibited https://icann.org/epp#serverUpdateProhibited",
    "clientUpdateProhibited (https://www.icann.org/epp#clientUpdateProhibited)",
    "clientTransferProhibited (https://www.icann.org/epp#clientTransferProhibited)",
    "clientDeleteProhibited (https://www.icann.org/epp#clientDeleteProhibited)"
  ],
  "emails": [  # 联络邮箱
    "abusecomplaints@markmonitor.com",
    "whoisrequest@markmonitor.com"
  ],
  "dnssec": "unsigned",
  "name": null,
  "org":  "\u5e7f\u5dde\u7f51\u6613\u8ba1\u7b97\u673a\u7cfb\u7edf\u6709\u9650\u516c\u53f8",
  "address": null,
  "city": null,
  "state": "Guang Dong",  # 城市(对应美国的"州"):广东
  "registrant_postal_code": null,  # 注册者邮编(无)
  "country": "CN"  # 国家码
}

三、子域名挖掘

通过必应(必应搜索引擎)进行子域名搜集:

首先确保安装有requests库、bs4库、urllib库,安装这些库的命令和过程如下:

Python代码示例:

python 复制代码
import requests
from bs4 import BeautifulSoup
from urllib.parse import urlparse
import sys

def bing_search(site, pages):
    Subdomain = []
    headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0",
               "Accept": "text/html,application/xhtml+xml, application/xml;q=0.9,*/*;q=0.8",
               "Referer": "https://cn.bing.com"
              }
    for i in range(1, int(pages)+1):
        url = "https://cn.bing.com/search?q=site%3a"+site+"&go=Search&qs=ds&first="+ str((int(i)-1) * 10) + "&FROM=PERE"
        html = requests.get(url, headers=headers)
        soup = BeautifulSoup(html.content, 'html.parser')
        job_bt = soup.findAll('h2')
        for i in job_bt:
            link = i.a.get('href')
            domain = str(urlparse(link).scheme + "://" + urlparse(link).netloc)
            if domain in Subdomain:
                pass
            else:
                Subdomain.append(domain)
                print(domain)


if __name__ == '__main__':
    if len(sys.argv) == 3:
        site = sys.argv[1]  # 参数1:网址
        page = sys.argv[2]  # 参数2:获取搜索引擎页数
    else:
        print("usage: %s baidu.com 10" % sys.argv[0])
        sys.exit(-1)
    Subdomain = bing_search(site,page)

对域名baidu.com进行子域名收集,获取50页搜索结果:

参考书目

《Python安全攻防------渗透测试实战指南》,MS08067安全实验室 编著,北京,机械工业出版社,2021年10月第1版。

相关推荐
安全系统学习3 小时前
【网络安全】Mysql注入中锁机制
安全·web安全·网络安全·渗透测试·xss
云泽野3 小时前
【Java|集合类】list遍历的6种方式
java·python·list
IMPYLH5 小时前
Python 的内置函数 reversed
笔记·python
小赖同学啊7 小时前
物联网数据安全区块链服务
开发语言·python·区块链
码荼7 小时前
学习开发之hashmap
java·python·学习·哈希算法·个人开发·小白学开发·不花钱不花时间crud
深圳安锐科技有限公司7 小时前
深圳安锐科技发布国内首款4G 索力仪!让斜拉桥索力自动化监测更精准高效
运维·安全·自动化·自动化监测·人工监测·桥梁监测·索力监测
潘锦7 小时前
海量「免费」的 OPENAI KEY,你敢用吗?
安全·openai
冰橙子id7 小时前
linux系统安全
linux·安全·系统安全
小陈phd8 小时前
李宏毅机器学习笔记——梯度下降法
人工智能·python·机器学习
kk爱闹8 小时前
【挑战14天学完python和pytorch】- day01
android·pytorch·python