Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/py
- # /nick twoj_nick
- # /server 127.0.0.1 9999 login_do_poczty:haslo_do_poczty
- # ------------------------------------------------------
- from socket import *
- from thread import start_new_thread
- from re import findall, search, sub
- from time import ctime
- import select
- import sys
- def dawaj_date():
- sys.stdout.write("[%s] = " % ctime())
- if len(sys.argv) == 2:
- BindPort = int(sys.argv[1])
- else:
- BindPort = 9999
- def koduj(lm, sm):
- bm = ( 0, 14, 15, 0, 15, 1, 0, 4, 10, 4,
- 15, 13, 14, 0, 6, 3, 10, 14, 12, 6,
- 15, 0, 3, 15, 3, 15, 2, 8, 4, 0,
- 15, 1 )
- ej = ( 0, 2, 3, 11, 5, 2, 3, 15, 2, 10,
- 11, 5, 3, 13, 1, 8, 6, 3, 3, 11,
- 15, 5, 2, 13, 14, 4, 11, 2, 11, 2,
- 9, 14 )
- c = 0
- a = []
- b = []
- kod = ""
- while (c & 0x20) == 0:
- a.insert(c, (bm[c] + c & 0xf))
- b.insert(c, (ej[c] - c & 0xf))
- c = c + 1
- c = 0
- while c < 32:
- x = ord(lm[c])
- y = ord(sm[c&0x7])
- if x <= 57:
- x = x - 48
- else:
- x = (x - 97) + 10
- if y <= 57:
- y = y - 48
- else:
- y = (y - 97) + 10
- x = ((x ^ a[c] ^ y) + b[c] & 0xf)
- if x <= 9:
- x = x + 48
- else:
- x = (x + 97) - 10
- kod = kod + chr(x)
- c = c + 1
- return kod
- def s1(host,z,xl=0):
- s = socket(AF_INET, SOCK_STREAM)
- s.connect((host, 80))
- s.send(z)
- dane = ""
- while 1:
- dane_ = s.recv(1024)
- if dane_ == "":
- break
- dane = dane + dane_
- s.close()
- if xl == 1:
- return findall("<param name=\"magic\" value=\"(\S+)\" \/>",dane)[0]
- cookie = findall("Set-Cookie: (.+?;)", dane)
- try:
- loc=findall("Location: (.+?)\r",dane)[0]
- except:
- loc=""
- return [cookie,loc]
- def s2(host,lo,keep,ref,cooks="",enc=0,post=""):
- a="%s HTTP/1.1\r\n" % lo
- a+="Host: %s\r\n" % host
- a+=("User-Agent: Mozilla/5.0 (Linux)\r\n"
- "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
- "Accept-Language: pl,en-us;q=0.7,en;q=0.3\r\n")
- if enc==0: a+="Accept-Encoding: gzip,deflate\r\n"
- a+="Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7\r\n"
- if keep==1:
- a+=("Keep-Alive: 300\r\n"
- "Connection: keep-alive\r\n")
- else: a+="Connection: close\r\n"
- if ref!="": a+="Referer: %s\r\n" % ref
- if cooks!="": a+=cooks+"\r\n"
- if post=="": a+="\r\n"
- else: a+=post
- return s1(host,a,enc)
- def kook(cookies):
- i=0
- while(i<len(cookies)):
- if search("wpdticket",cookies[i]): del cookies[i]
- if search("rekticket",cookies[i]): del cookies[i]
- i+=1
- return cookies
- def dawaj_magic(nickname,username,password):
- cookies = []
- cook = "Cookie: "
- [c,l] = s2("czat.wp.pl","GET /",1,"")
- cookies += c
- lold = l
- cook+=' '.join(cookies)
- #[c,l] = s2("czat.wp.pl","GET "+l,1,"",cook)
- #cookies=kook(cookies)
- #cookies+=c
- #cook = "Cookie: "
- #cook+=' '.join(cookies)
- #[c,l] = s2("czat.wp.pl","GET /i,5,chat.html",1,"http://czat.wp.pl"+lold,cook)
- #cookies=kook(cookies)
- #cookies+=c
- #cook = "Cookie: "
- #cook+=' '.join(cookies)
- [c,l] = s2("profil.wp.pl","GET /login.html?url=http%3A%2F%2Fczat.wp.pl%2Fchat.html%3Fi%3D173662&serwis=cafe",1,"http://czat.wp.pl/i,5,chat.html",cook)
- lold=l
- #cookies=kook(cookies)
- cookies+=c
- cook = "Cookie: "
- cook+=' '.join(cookies)
- #[c,l] = s2("profil.wp.pl","GET "+lold,1,"http://czat.wp.pl/i,5,chat.html",cook)
- #cookies=kook(cookies)
- #cookies+=c
- #cook = "Cookie: "
- #cook+=' '.join(cookies)
- POST="serwis=cafe&url=http%3A%2F%2Fczat.wp.pl%2Fchat.html%3Fi%3D173662&"+"tryLogin=1&countTest=1&login_username=%s&login_password=%s&savelogin=2&savessl=2&starapoczta=2&minipoczta=2&zaloguj=Zaloguj" % (username,password)
- [c,l] = s2("profil.wp.pl","POST /index2.html",1,lold,cook+"\r\nContent-Type: application/x-www-form-urlencoded\r\n"+"Content-Length: "+ str(len(POST))+"\r\n",0,POST)
- cookies=kook(cookies)
- cookies+=c
- cook = "Cookie: "
- cook+=' '.join(cookies)
- l = l.replace("http://dot.wp.pl","")
- [c,l] = s2("dot.wp.pl","GET "+l,0,"http://profil.wp.pl"+lold,cook)
- cookies+=c
- cook = "Cookie: "
- cook+=' '.join(cookies)
- l=l.replace("http://czat.wp.pl","")
- lold=l
- [c,l] = s2("czat.wp.pl","GET "+l,1,"http://profil.wp.pl"+lold,cook)
- cookies=kook(cookies)
- cookies+=c
- cook = "Cookie: "
- cook+=' '.join(cookies)
- return [cook,s2("czat.wp.pl","GET /auth,tak,i,5,nick,"+nickname+",chat.html",1,"http://czat.wp.pl"+lold,cook,1)]
- def is_upper(sfx, i):
- code = int(sfx, 16)
- maxlen = len(sfx) * 2
- return (code >> maxlen) & (1 << (maxlen - i - 1))
- def is_special(sfx, i):
- code = int(sfx, 16)
- maxlen = len(sfx) * 2
- return ((code << maxlen) >> maxlen) & (1 << (maxlen - i - 1))
- #
- # funkcje hash2nick() oraz nick2hash() przepisane z programu WP Gate / rawsock.31337.pl
- #
- def hash2nick(hash):
- nick = ""
- if hash[0] == 'a':
- nick = '~'
- if hash.find('|') == -1:
- return hash
- [mid,sfx] = hash.split('|')
- mid=mid[1:]
- i1 = 0
- while i1 < len(mid):
- if (is_upper(sfx, i1) != 0) and (is_special(sfx, i1) == 0):
- nick = nick + chr(ord(mid[i1]) - 32)
- else:
- nick = nick + mid[i1]
- i1 = i1 + 1
- return nick
- def nick2hash(nick):
- if nick[0] == '~':
- hash = 'a'
- start = 1
- else:
- hash = 'b'
- start = 0
- a = "ABCDEFGHIJKLMNOPQRSTUWXYZ"
- maxlen = (1 + ((len(nick) - start - 1) / 4)) * 4
- i = start
- tmp = ""
- upcode = 0
- i2 = 0
- while i < len(nick):
- i2 = 0
- while i2 < 25:
- if a[i2] == nick[i]:
- upcode = upcode | (1 << (maxlen - i - 1 + start))
- i2 = i2 + 1
- hash = hash + nick[i].lower()
- i = i + 1
- up = "%Lx" % upcode
- if len(up) < maxlen/4:
- tmp = "0"
- i = 1
- while i < maxlen/4 - len(up):
- tmp = tmp + "0"
- i = i + 1
- tmp = tmp + up
- up = tmp
- hash = hash + '|' + up + "0" * len(up)
- return hash
- def s33(nickname, cookies):
- s = socket(AF_INET, SOCK_STREAM)
- s.connect(("czati1.wp.pl", 80))
- z = "GET /getticket.html?nick=%s HTTP/1.1\r\n" % nickname
- z = z + ("User-Agent: Mozilla/4.0 (Windows 2000 5.0) Java/1.6.0\r\n"
- "Host: czati1.wp.pl\r\n"
- "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n"
- "Connection: keep-alive\r\n")
- z = z + cookies + "\r\n\r\n"
- s.send(z)
- bilet = findall("\r\n\r\n(.*)", s.recv(2224))[0]
- bilet = bilet.replace('+','%2B')
- bilet = bilet.replace('/', '%2F')
- s.close()
- return bilet
- def jedziesz(sock, ID):
- bufor = ""
- koniec = 0
- nickname = ""
- nickhash = ""
- username = ""
- password = ""
- sock.send(("kdw.py : uzycie:\r\n"
- "kdw.py : /set nick twoj_nick\r\n"
- "kdw.py : /server 127.0.0.1 9999 login_wp:haslo_wp\r\n"
- "kdw.py : login_wp podawaj bez @wp.pl na koncu\r\n"))
- while 1:
- b1=""
- b1=sock.recv(1024)
- if b1 == "": return
- bufor += b1
- if not bufor.find("NICK") == -1:
- nickname = findall("NICK (.*?)(\r|\n)", bufor)[0][0]
- if not bufor.find("PASS") == -1:
- password = findall("PASS (.*?)(\r|\n)", bufor)[0][0]
- if (password != "") and (nickname != ""):
- break
- [username, password] = password.split(':')
- nickhash = nick2hash(nickname)
- sys.stdout.write("nick: %s\n" % nickname)
- gniazda = []
- gniazda.append(sock)
- wpserv = socket(AF_INET, SOCK_STREAM)
- wpserv.connect(("212.77.100.125", 5579))
- try:
- [cookies,mag] = dawaj_magic(nickname,username,password)
- wpserv.send("NICK %s\r\n" % nickhash)
- wpserv.send("PASS %s\r\n" % s33(nickname, cookies))
- smag = wpserv.recv(512)
- wpserv.send("USER 192.168.0.3 8 %s140b :Czat-Applet\r\n" % (koduj(mag, findall(".*?MAGIC.*?:(.*?)\r\n", smag)[0])))
- except:
- dawaj_date()
- my_err = "[%3d] ERROR: sprawdz swoje polaczenie sieciowe z internetem, poprawnosc wprowadzonego hasla i nicka lub zmiany autoryzacji\r\n" % ID
- sys.stdout.write(my_err)
- sock.send(my_err)
- sock.close()
- return
- gniazda.append(wpserv)
- sock.send(":czat.wp.pl 001 " + nickname + " :Welcome to the Internet Relay Network\r\n")
- while 1:
- (dr, dw, de) = select.select(gniazda, [], [])
- for gotowe in dr:
- if gotowe == sock:
- try:
- bufor = gotowe.recv(1024)
- if bufor == "":
- koniec = 1
- break
- tabbuf = bufor.split(' ')
- if tabbuf[0] == "PRIVMSG":
- if tabbuf[1][0] != '#':
- bufor = bufor.replace(tabbuf[1], nick2hash(tabbuf[1]))
- elif tabbuf[0] == "JOIN":
- bufor = bufor.replace("JOIN", "WPJOIN")
- elif (tabbuf[0] == "WHOIS") and (len(tabbuf)>1):
- bufor = bufor.replace(tabbuf[1], nick2hash(tabbuf[1]))
- wpserv.send(bufor)
- except:
- dawaj_date()
- print "[%3d] nick: %s = blad odczytu/zapisu z/do gniazda" % (ID, nickname)
- koniec = 1
- break
- if gotowe == wpserv:
- bufor = ""
- try:
- while 1:
- bu2 = gotowe.recv(1024)
- if bu2 == "":
- koniec = 1
- break
- if bu2[len(bu2) - 1] == '\n':
- bufor += bu2
- break
- bufor += bu2
- tab = findall("(.*?\n)", bufor)
- for linia in tab:
- linia = linia.replace(nickhash, nickname)
- try:
- tabbuf = linia.split(' ')
- if tabbuf[1] == "PRIVMSG":
- (mnick, mchan, mmsg) = findall("(\S+) PRIVMSG (\S+) :(.*)\r", linia)[0]
- mn = findall(":(\S+?)!", mnick)[0]
- mn2 = hash2nick(mn)
- mnick = mnick.replace(mn, mn2)
- mchan = hash2nick(mchan)
- linia = "%s PRIVMSG %s :%s\r\n" % (mnick, mchan, mmsg)
- linia=linia.replace("<b>","\x02")
- linia=linia.replace("<u>","")
- linia=linia.replace("<i>","")
- linia=linia.replace("<color=\"kolor21\">",'\x03'+"06")
- linia=linia.replace("<color=\"kolor25\">",'\x03'+"11")
- linia=linia.replace("<color=\"kolor18\">",'\x03'+"10")
- linia=linia.replace("<color=\"kolor16\">",'\x03'+"09")
- linia=linia.replace("<color=\"kolor20\">",'\x03'+"14")
- linia=linia.replace("<color=\"kolor13\">",'\x03'+"15")
- linia=linia.replace("<color=\"kolor9\">",'\x03'+"04")
- linia=linia.replace("<color=\"kolor8\">",'\x03'+"12")
- linia=linia.replace("<color=\"kolor7\">",'\x03'+"08")
- linia=linia.replace("<color=\"kolor6\">",'\x03'+"03")
- linia=linia.replace("<color=\"kolor3\">",'\x03'+"05")
- linia=linia.replace("<color=\"kolor5\">",'\x03'+"07")
- linia=linia.replace("<color=\"kolor1\">",'\x03'+"05")
- linia=sub("<size=\"\d+\">","",linia)
- linia=sub("<c.*?=\".+\">","",linia)
- linia=sub("\{.+\}\{.+\}\{.+\}","",linia)
- elif tabbuf[1] == "WPJOIN":
- mnick = tabbuf[0]
- mn = findall(":(\S+?)!", mnick)[0]
- mn2 = hash2nick(mn)
- mnick = mnick.replace(mn, mn2)
- mchan = linia.split(' ')[2]
- linia = "%s JOIN :%s\r\n" % (mnick, mchan)
- elif tabbuf[1] == "PART":
- mnick = tabbuf[0]
- mn = findall(":(\S+?)!", mnick)[0]
- mn2 = hash2nick(mn)
- mnick = mnick.replace(mn, mn2)
- mchan = tabbuf[2]
- linia = "%s PART %s :%s\r\n" % (mnick, mchan, mn2)
- elif tabbuf[1] == "QUIT":
- mnick = tabbuf[0]
- mn = findall(":(\S+?)!", mnick)[0]
- mn2 = hash2nick(mn)
- linia = linia.replace(mn, mn2)
- elif tabbuf[1] == "353":
- nl2 = []
- b = findall("(\S+) 353 \S+ = (\S+) :(.*)", linia)[0]
- nl = findall("(\S+)", b[2])
- for i in nl:
- nl2.append(hash2nick(i))
- linia = "%s 353 %s = %s :%s\r\n" % (b[0], nickname, b[1], ' '.join(nl2))
- except:
- pass
- sock.send(linia)
- except:
- dawaj_date()
- print "[%3d] nick: %s = blad odczytu/zapisu z/do gniazda" % (ID, nickname)
- koniec = 1
- break
- if koniec == 1:
- sock.close()
- wpserv.close()
- break
- s = socket(AF_INET, SOCK_STREAM)
- try:
- s.bind(('', BindPort))
- except:
- dawaj_date()
- print "ERROR: nie mozna zabindowac portu %s, wybierz inny" % BindPort
- s.close()
- sys.exit()
- dawaj_date()
- try:
- print "server: %s:%d" % (gethostbyname(gethostname()), BindPort)
- except:
- print "server: 127.0.0.1:%d" % BindPort
- s.listen(5)
- cID = 1
- while 1:
- c, cinfo = s.accept()
- dawaj_date()
- sys.stdout.write("[%3d] %s:%s = " % ((cID, ) + cinfo))
- start_new_thread(jedziesz, (c, cID, ))
- cID = cID + 1
- s.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement