-
Notifications
You must be signed in to change notification settings - Fork 1
/
ircdaemon.py
253 lines (224 loc) · 8.47 KB
/
ircdaemon.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import socket
import os
from threading import Thread
import logging
import errno
from time import sleep
import ssl as ssl_mod
logging.basicConfig(filename='irclog.log', format='%(asctime)s;%(levelname)s;%(message)s', level=logging.DEBUG)
''' IRC Client begin '''
class ircclient:
def __init__(self, server, port, realname, ident, nick, password, channel, ssl, adminhost, adminpw):
self.server = server
self.port = port
self.realname = realname
self.ident = ident
self.nick = nick
self.channel = channel
self.state = "offline"
self.ssl = ssl
self.adminhost = adminhost
self.adminpw = adminpw
#light red
#purple
#pink
#light green
#dark grey
self.colors = {
'crit': "\x03\x34",
'warn': "\x03\x36",
'debug': "\x03\x31\x33",
'info': "\x03\x39",
'spam': "\x03\x31\x34"
}
def connect(self):
logging.info("[IRC] Connecting to IRC")
try:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((socket.gethostbyname(self.server), self.port))
#self.sock.settimeout(120)
if self.ssl:
self.ssl = ssl_mod.wrap_socket(self.sock)
logging.info("[IRC] Connected to IRC")
self.sendSocket("USER %s %s DIONAEA :%s\r\n" % (self.ident, self.server, self.realname))
self.sendSocket("NICK %s\r\n" % self.nick)
self.sendSocket("JOIN %s\r\n" % self.channel)
self.state = "online"
logging.info("[IRC] Joined channel")
self.t = Thread(target=self.server_response, args=(self,))
self.t.start()
except socket.error as v:
if v.errno == errno.ECONNREFUSED:
logging.warning("[IRC] Connection refused, retrying in 5 seconds")
else:
logging.warning("[IRC] Connection failed, retrying in 5 seconds: %s" % v)
sleep(5)
self.connect()
except Exception as v:
logging.warning("[IRC] Connection failed, retrying in 5 seconds: %s" % v)
sleep(5)
self.connect()
def parseMessage(self, s):
try:
s = s.rstrip()
"""Breaks a message from an IRC server into its prefix, command, and arguments.
"""
prefix = ''
trailing = []
if not s:
return
if s[0] == ':':
prefix, s = s[1:].split(' ', 1)
if s.find(' :') != -1:
s, trailing = s.split(' :', 1)
args = s.split()
args.append(trailing)
else:
args = s.split()
command = args.pop(0)
nick, host = prefix.split("!")
dest = args.pop(0)
cmd = args[0].split(" ")
#logging.debug("[IRC] Received command: %s %s %s %s" % (nick, host.replace("~", ""), dest, cmd[0]))
if len(cmd) >= 1:
if "!opme" in cmd[0] and len(cmd) >= 2:
if cmd[1] == self.adminpw and host.replace("~", "") == self.adminhost:
self.sendSocket("MODE %s +o %s\r\n" % (dest, nick))
logging.debug("[IRC] Opped %s" % nick)
except:
return
def server_response(self, client):
#i = 0
while(client.state != "offline" and self.state != "offline"):
try:
#i = i + 1
#logger.debug("PRIVMSG %s :%s" % (client.channel, i))
#client.sendSocket("PRIVMSG %s :%s\r\n" % (client.channel, i))
response = self.recvSocket()
if len(response) <= 0:
return
'''if "!" in response and ":" in response[response.index(":") + 1:]:
return client.parseMessage(response)'''
if "PING :" in response:
client.sendSocket(response.replace("PING", "PONG"))
logging.debug("[IRC] Received PING")
elif "PRIVMSG " in response:
self.parseMessage(response)
else:
logging.debug("[IRC] Received MSG: %s" % response)
except KeyboardInterrupt:
#break
pass
return
def send_message(self, message, type="info"):
if not message:
logging.warning("[IRC] Executed send_message without a message")
return
if self.channel:
return self.sendSocket("PRIVMSG %s :%s%s\r\n" % (self.channel, self.colors[type], message))
else:
logging.warning("[IRC] Executed send_message without a defined channel")
return False
def close(self, msg=""):
logging.info("[IRC] Disconnected from IRC")
self.sendSocket("QUIT :%s" % msg)
self.state = "offline"
self.sock.close()
return
def sendSocket(self, data):
try:
data = bytearray(data, 'utf-8')
if self.ssl:
self.ssl.write(data)
else:
self.sock.send(data)
except Exception as e:
logging.warn("[IRC] Error sending to socket: %s" % e)
return
def recvSocket(self):
data = str()
while data.find("\r") == -1:
chunk = str()
try:
if self.ssl:
chunk = self.ssl.read()
else:
chunk = self.sock.recv(4096)
chunk = chunk.decode('utf-8')
except:
pass
if chunk == None:
self.close()
sleep(5)
self.connect()
break
elif len(chunk) <= 0:
self.close()
sleep(5)
self.connect()
break
else:
data += chunk
return data
'''def setAdminPw(self, host, pw):
self.adminpw = pw
self.adminhost = host'''
''' IRC Client end '''
class daemon:
def __init__(self):
self.client = None
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
os.remove("/tmp/ircdaemon")
except OSError:
pass
s.bind("/tmp/ircdaemon")
logging.info("[LOCAL] Waiting for a connection")
s.listen(1)
self.conn, self.addr = s.accept()
t = Thread(target=self.recvLocalSocket, args=(self,))
t.start()
def __del__(self):
self.conn = None
self.addr = None
self.client = None
self.adminpw = None
def recvLocalSocket(self, daemon):
logging.info("[LOCAL] Dionaea connected")
while True:
try:
data = daemon.conn.recv(4096)
except socket.timeout:
logging.warning("[LOCAL] Timeout reveiving localsocket")
continue
except KeyboardInterrupt:
#break
pass
if not data:
continue
data = data.decode('utf-8')
data = data.split(':s:')
if data[0] == "MSG" and daemon.client.state != "offline":
logging.debug("[LOCAL] Received MSG:%s" % data[1])
if len(data) >= 3:
daemon.client.send_message(data[2], data[1])
else:
daemon.client.send_message(data[1])
elif data[0] == "CONNECT":
logging.debug("[LOCAL] Received CONNECT:%s:%i:%s:%s:%s:*****:%s" % (data[1], int(data[2]), data[3], data[4], data[5], data[7]))
daemon.client = ircclient(server=data[1], port=int(data[2]), realname=data[3], ident=data[4], nick=data[5], password=data[6], channel=data[7], ssl=bool(data[8]), adminhost=data[9], adminpw=data[10])
daemon.client.connect()
elif data[0] == "DISCONNECT":
logging.debug("[LOCAL] Received DISCONNECT")
daemon.client.close("Dionaea requested disconnect")
daemon.client = None
'''elif data[0] == "SETADMIN":
logging.debug("[LOCAL] Received SETADMIN %s:%s" % (data[1], data[2]))
daemon.adminpw = data[1]
daemon.client.setAdminCredentials(data[1], data[2])'''
return
def closeLocalConnection(self):
self.conn.close()
self.conn = None
self.addr = None
d = daemon()