-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnexusphp.py
More file actions
149 lines (127 loc) · 4.91 KB
/
nexusphp.py
File metadata and controls
149 lines (127 loc) · 4.91 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
#! /usr/bin/env python3
# -*- coding:utf-8 -*-
import re
import logging
import requests
import pytesseract
from io import BytesIO
from PIL import Image
from urllib.parse import urljoin
from bs4 import BeautifulSoup
logging.basicConfig(filename='ptsign.log',filemode='a',level=logging.INFO,format='%(asctime)s - %(message)s',datefmt='%d-%b-%y %H:%M:%S')
class CaptchaParse(object):
def __init__(self,image):
self.image = Image.open(image).convert('L')
def image_to_bin(self,threshold=120):
pixdata = self.image.load()
w, h = self.image.size
for y in range(h):
for x in range(w):
if pixdata[x, y] < threshold:
pixdata[x, y] = 0
else:
pixdata[x, y] = 255
return self.image
def delete_point(self):
pixdata = self.image.load()
w,h = self.image.size
for y in range(1,h-1):
for x in range(1,w-1):
count = 0
if pixdata[x,y-1] > 245:
count = count + 1
if pixdata[x,y+1] > 245:
count = count + 1
if pixdata[x-1,y] > 245:
count = count + 1
if pixdata[x+1,y] > 245:
count = count + 1
if pixdata[x-1,y-1] > 245:
count = count + 1
if pixdata[x-1,y+1] > 245:
count = count + 1
if pixdata[x+1,y-1] > 245:
count = count + 1
if pixdata[x+1,y+1] > 245:
count = count + 1
if count > 6:
pixdata[x,y] = 255
return self.image
def to_string(self):
regex = r"[\W\_]"
image = self.image_to_bin()
image = self.delete_point()
imagestring = pytesseract.image_to_string(image)
imagestring = re.sub(regex,'',imagestring)
return imagestring
class NexusPHP(object):
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36"
def __init__(self,url='https://hdhome.org',user_agent=user_agent):
self.url = url
self.session = requests.Session()
self.session.headers.update({'user-agent':user_agent})
self.session.headers.update({'origin':self.url})
self.session.headers.update({'referer':urljoin(self.url,'login.php')})
def login(self,username,password,captcha):
url=urljoin(self.url,'takelogin.php')
imagestring = captcha[0]
imagehash = captcha[1]
playload = {'username':username,
'password':password,
'imagestring':imagestring,
'imagehash':imagehash,}
r = self.session.post(url,playload,timeout=6)
logging.info('imagestring: {}'.format(imagestring))
logging.info('imagehash: {}'.format(imagehash))
logging.info('get {} code {}'.format(url,str(r.status_code)))
return self.is_logged_in()
def _get_login_captcha(self):
url = urljoin(self.url,'login.php')
r = self.session.get(url,timeout=6)
soup = BeautifulSoup(r.text,"html.parser")
img = soup.find_all("img")
for i in img:
if 'image' in i['src']:
imgurl = urljoin(self.url,i['src'])
image = self.session.get(imgurl)
image = BytesIO(image.content)
image = CaptchaParse(image)
imagestring = image.to_string()
imagehash = soup.find("input",{"name":"imagehash"})
assert imagehash and imagehash['value'],"there is no imagehash on this page"
return (imagestring,imagehash['value'])
def is_logged_in(self):
url = urljoin(self.url,'index.php')
r = self.session.get(url,timeout=6)
return 'Pls keep seeding' in r.text
def sign(self):
url = urljoin(self.url,'attendance.php')
self.session.headers.update({'referer':urljoin(self.url,'index.php')})
self.session.headers.update({'upgrade-insecure-requests':'1'})
r = self.session.get(url,allow_redirects=False,timeout=6)
logging.info('get {} code {}'.format(url,str(r.status_code)))
return r
def main():
import time
from random import randrange
username = 'hdhome'
password = 'hdhome'
gzt = NexusPHP('https://pt.gztown.net')
times = 1
while times < 8:
captcha = gzt._get_login_captcha()
if len(captcha[0]) == 6:
time.sleep(randrange(5))
logging.info('{} times trying'.format(times))
gzt.login(username,password,captcha)
times += 1
time.sleep(randrange(5))
r = gzt.sign()
if r.status_code == 200:
logging.info('sign success')
break
else:
logging.info('sign failure')
continue
if __name__ == '__main__':
main()