-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathirc_challenges.py
170 lines (137 loc) · 4.82 KB
/
irc_challenges.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/local/bin/python
#cSpell:disable
import sys, socket, ssl
from time import sleep
from math import sqrt
import base64, codecs, zlib
server = "irc.root-me.org"
port = 6697
nick = "TestUser42"
password = "MYSECRETPASSWORD1337"
channel = "#root-me_challenge"
sendtobot = "candy"
timeout = 1 #seconds
retries = 5
DEBUG = False
challenge = 0 #dummy
try:
challenge = 4 if len(sys.argv) == 1 else int(sys.argv[1])
if challenge < 1 or challenge > 4:
raise Exception('BadNum')
except Exception, e:
raise RuntimeError('First and only argument must be integer! (challenge number, from 1 to 4)')
print('You selected {} challenge\n'.format(challenge))
class irc(object):
socket = None
srv = None
port = None
nick = None
pwd = None
channel = None
isAuthorized = False
def _check_auth(func):
def wrapper(self, *arg, **kw):
if self.isAuthorized:
return func(self, *arg, **kw)
else:
raise Exception('Not authorized!')
return None
return wrapper
def __init__(self, srv, port):
try:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket = ssl.wrap_socket(self.socket)
self.socket.settimeout(timeout)
self.socket.connect((srv, port))
except Exception, e:
raise RuntimeError('Failed to connect to IRC server!')
self.srv = srv
self.port = port
def __del__(self):
if self.socket:
self._send('QUIT :bye bye!')
self.socket.close
self.socket = None
def _send(self, line, EOL = '\r\n'):
if DEBUG:
print('SEND:\n\t%s' % (line))
self.socket.send(line + EOL)
def _recv(self, size = 8192):
data = ''
while True:
try:
tempdata = self.socket.recv(size)
except ssl.SSLError:
tempdata = None
if not tempdata:
break
data += tempdata
if DEBUG and data:
print('RECV:\n\t%s' % (data))
return data if data else None
def auth(self, nick, pwd, _check_auth_func, realname = None):
self._send('NICK ' + nick)
sleep(1)
self._send('PASS ' + password)
sleep(1)
self._send('USER {0} {0} {0} :{1}'.format(nick, realname if realname else nick + '-Test'))
sleep(1)
self._send('PRIVMSG nickserv :identify {} {}'.format(nick, pwd))
sleep(1)
if not _check_auth_func(self._recv()):
raise Exception('Auth Failed!')
else:
self.isAuthorized = True
self.nick = nick
self.pwd = pwd
@_check_auth
def join(self, channel):
self._send('JOIN ' + channel)
self.channel = channel
@_check_auth
def privmsg(self, reciever, msg):
self._send("PRIVMSG {} :{}".format(reciever, msg))
return self._recv()
@_check_auth
def ping_pong(self):
reply = self._recv()
if reply:
if reply.find('PING') != -1:
self._send('PONG ' + reply.split()[1])
print "Establishing connection to {}:{}...".format(server, port)
IRC = irc(server, port)
print "Connection established!\n"
print "Try to auth as {}...".format(nick)
IRC.auth(nick, password, lambda reply: True if reply.find(' :your unique ID') != -1 else False)
print "Auth succeed!\n"
print "Join channel {}".format(channel)
IRC.join(channel)
print "Start chat with bot '{}'\n".format(sendtobot)
def ep1(bot_reply):
nums = [int(num) for num in bot_reply.split(' / ')]
return round(sqrt(nums[0]) * nums[1], 2)
def ep2(bot_reply):
return base64.b64decode(bot_reply)
def ep3(bot_reply):
return codecs.decode(bot_reply, 'rot_13')
def ep4(bot_reply):
return zlib.decompress(base64.b64decode(bot_reply))
retries_actual = retries
while retries_actual > 0:
IRC.ping_pong()
print('Attempt #{} (of {})'.format(retries - retries_actual + 1, retries))
reply = IRC.privmsg(sendtobot, '!ep' + str(challenge)).split(':')[2]
print('Challenge - {}'.format(reply.replace('\r\n', '')))
ans = locals()['ep{}'.format(challenge)](reply)
print('Answer - {}'.format(ans))
reply = IRC.privmsg(sendtobot, '!ep{} -rep {}'.format(challenge, ans))
if reply.find('You dit it!') != -1:
print('SUCCESS!\n{}'.format(reply.split(':')[2]))
break
else:
print('Failed!\n{}'.format(reply.split(':')[2]))
if reply.find('BANNED') != -1:
print 'Sleep for 30 seconds...'
sleep(30)
retries_actual -= 1
print('\n{0}\nHave a good day, h4x0r!\n{0}\n'.format('=' * 40))