-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.py
More file actions
154 lines (143 loc) · 5.29 KB
/
bot.py
File metadata and controls
154 lines (143 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from config import logininfo
import re,json,time,configparser,logging,sys,os,requests,asyncio
def login(login_url, username, password):
#请求头
my_headers = {
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding' : 'gzip',
'Accept-Language' : 'zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4'
}
#获取token
sss = requests.Session()
try:
r = sss.get(login_url, headers = my_headers)
except:
#logging.error('[error]fail to login,check your config and network')
return
reg = r'<input type="hidden" name="nonce" value="(.*)">'
pattern = re.compile(reg)
result = pattern.findall(r.content.decode('utf-8'))
token = result[0]
#postdata
my_data = {
'name' : username,
'password' : password,
'nonce' : token,
}
#登录后
try:
r = sss.post(login_url, headers = my_headers, data = my_data)
except:
#logging.error('[error]fail to login,check your config and network')
return
if r.ok == True:
logging.info('[success]login ok,start the robot...')
return sss
else:
pass
#logging.error('[error]fail to login,check your config and network')
#取配置文件
def readConf(configFile,subject,key):
cf = configparser.ConfigParser()
filename = cf.read(configFile)
return cf.get(subject,key)
#取用户列表
def get_user_list():
theSession = login(logininfo.login_url,logininfo.username,logininfo.password)
apiUrl = 'http://ip:port/api/v1/users' #ctfd 地址
try:
responseJson = theSession.get(apiUrl)
except:
logging.error('[error]fail to get api info,continue.')
return []
jsonInfo = json.loads(responseJson.text)
if jsonInfo['success'] != True:
logging.error("error to get userlist")
return []
userList = eval(str(jsonInfo['data']))
return userList
#取提交flag信息
def get_attempt_info():
theSession = login(logininfo.login_url,logininfo.username,logininfo.password)
apiUrl = 'http://ip:port/api/v1/submissions' #ctfd 地址
try:
responseJson = theSession.get(apiUrl)
except:
logging.error('[error0]fail to get api info,continue.')
return []
jsonInfo = json.loads(responseJson.text)
if jsonInfo['success'] != True:
logging.error("error to get attemptlist")
return []
allList = eval(str(jsonInfo['data']))
return allList
#异步循环发送请求
async def deal_user_list():
global userLen,userList
while True:
try:
tmpList = get_user_list()
tmpLen = len(tmpList)
print(userLen,tmpLen)
if tmpLen == 0:
await asyncio.sleep(3)
continue
if userLen < tmpLen:
for i in range(userLen,tmpLen):
message = tmpList[i]['name']+" 成功注册~"
requests.get(logininfo.group_api+message)
userLen = tmpLen
userList = tmpList
else:
userLen = tmpLen
userlist = tmpList
await asyncio.sleep(3)
except TypeError:
logging.error('[error1]fail to get api info,continue.')
continue
await asyncio.sleep(3)
async def deal_attemp_list():
global userLen,userList,allLen,allList
while True:
try:
tmpallList = get_attempt_info()
tmpallLen = len(tmpallList)
if tmpallLen == 0:
await asyncio.sleep(3)
continue
if allLen < tmpallLen:
for i in range(allLen,tmpallLen):
if tmpallList[i]['type'] == "correct":
chaname = ""
for s in userList:
if str(s['id']) == str(tmpallList[i]['user_id']):
chaname = s['name']
if chaname == "":
continue
await asyncio.sleep(3)
message = "恭喜" + chaname + "做出" + str(tmpallList[i]['challenge']['category'])+"题目-" + str(tmpallList[i]['challenge']['name'])
#requests.get(logininfo.url_api+message)
requests.get(logininfo.group_api+message)
allLen = tmpallLen
allList = tmpallList
else:
allLen = tmpallLen
allList = tmpallList
await asyncio.sleep(3)
except TypeError:
logging.error('[error2]fail to get api info,continue.')
continue
if __name__ == ("__main__"):
logging.basicConfig(filename='err.log',level=logging.ERROR,format='%(asctime)s %(filename)s[line:%(lineno)d] %(message)s',datefmt='%Y-%m-%d')
# 全局变量声明
userList = get_user_list()
#userLen = 0
userLen = len(userList)
allList = get_attempt_info()
allLen = len(allList)
#allLen = 0
loop = asyncio.get_event_loop()
tasks = [deal_user_list(),deal_attemp_list()]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()