Advertisement
kamilnowinski

tunnel - potrzebna autoryzacja!

Dec 8th, 2011
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.93 KB | None | 0 0
  1. #!/usr/bin/py
  2. # /nick twoj_nick
  3. # /server 127.0.0.1 9999 login_do_poczty:haslo_do_poczty
  4. # ------------------------------------------------------
  5.  
  6. from socket import *
  7. from thread import start_new_thread
  8. from re import findall, search, sub
  9. from time import ctime
  10. import select
  11. import sys
  12.  
  13. def dawaj_date():
  14.  sys.stdout.write("[%s] = " % ctime())
  15.  
  16. if len(sys.argv) == 2:
  17.  BindPort = int(sys.argv[1])
  18. else:
  19.  BindPort = 9999
  20.  
  21. def koduj(lm, sm):
  22.  bm = ( 0, 14, 15, 0, 15, 1, 0, 4, 10, 4,
  23.         15, 13, 14, 0, 6, 3, 10, 14, 12, 6,
  24.         15, 0, 3, 15, 3, 15, 2, 8, 4, 0,
  25.         15, 1 )
  26.  ej = ( 0, 2, 3, 11, 5, 2, 3, 15, 2, 10,
  27.         11, 5, 3, 13, 1, 8, 6, 3, 3, 11,
  28.         15, 5, 2, 13, 14, 4, 11, 2, 11, 2,
  29.         9, 14 )
  30.  c = 0
  31.  a = []
  32.  b = []
  33.  kod = ""
  34.  while (c & 0x20) == 0:
  35.   a.insert(c, (bm[c] + c & 0xf))
  36.   b.insert(c, (ej[c] - c & 0xf))
  37.   c = c + 1
  38.  c = 0
  39.  while c < 32:
  40.   x = ord(lm[c])
  41.   y = ord(sm[c&0x7])
  42.   if x <= 57:
  43.    x = x - 48
  44.   else:
  45.    x = (x - 97) + 10
  46.   if y <= 57:
  47.    y = y - 48
  48.   else:
  49.    y = (y - 97) + 10
  50.   x = ((x ^ a[c] ^ y) + b[c] & 0xf)
  51.   if x <= 9:
  52.    x = x + 48
  53.   else:
  54.    x = (x + 97) - 10
  55.   kod = kod + chr(x)
  56.   c = c + 1
  57.  return kod
  58.  
  59. def s1(host,z,xl=0):
  60.  s = socket(AF_INET, SOCK_STREAM)
  61.  s.connect((host, 80))
  62.  s.send(z)
  63.  dane = ""
  64.  while 1:
  65.   dane_ = s.recv(1024)
  66.   if dane_ == "":
  67.    break
  68.   dane = dane + dane_
  69.  s.close()
  70.  if xl == 1:
  71.   return findall("<param name=\"magic\" value=\"(\S+)\" \/>",dane)[0]
  72.  cookie = findall("Set-Cookie: (.+?;)", dane)
  73.  try:
  74.   loc=findall("Location: (.+?)\r",dane)[0]
  75.  except:
  76.   loc=""
  77.  return [cookie,loc]
  78.  
  79. def s2(host,lo,keep,ref,cooks="",enc=0,post=""):
  80.  a="%s HTTP/1.1\r\n" % lo
  81.  a+="Host: %s\r\n" % host
  82.  a+=("User-Agent: Mozilla/5.0 (Linux)\r\n"
  83.      "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
  84.      "Accept-Language: pl,en-us;q=0.7,en;q=0.3\r\n")
  85.  if enc==0: a+="Accept-Encoding: gzip,deflate\r\n"
  86.  a+="Accept-Charset: ISO-8859-2,utf-8;q=0.7,*;q=0.7\r\n"
  87.  if keep==1:
  88.   a+=("Keep-Alive: 300\r\n"
  89.       "Connection: keep-alive\r\n")
  90.  else: a+="Connection: close\r\n"
  91.  if ref!="": a+="Referer: %s\r\n" % ref
  92.  if cooks!="": a+=cooks+"\r\n"
  93.  if post=="": a+="\r\n"
  94.  else: a+=post
  95.  return s1(host,a,enc)
  96.  
  97. def kook(cookies):
  98.  i=0
  99.  while(i<len(cookies)):
  100.   if search("wpdticket",cookies[i]): del cookies[i]
  101.   if search("rekticket",cookies[i]): del cookies[i]
  102.   i+=1
  103.  return cookies
  104.  
  105. def dawaj_magic(nickname,username,password):
  106.  cookies = []
  107.  cook = "Cookie: "
  108.  [c,l] = s2("czat.wp.pl","GET /",1,"")
  109.  cookies += c
  110.  lold = l
  111.  cook+=' '.join(cookies)
  112.  #[c,l] = s2("czat.wp.pl","GET "+l,1,"",cook)
  113.  #cookies=kook(cookies)
  114.  #cookies+=c
  115.  #cook = "Cookie: "
  116.  #cook+=' '.join(cookies)
  117.  #[c,l] = s2("czat.wp.pl","GET /i,5,chat.html",1,"http://czat.wp.pl"+lold,cook)
  118.  #cookies=kook(cookies)
  119.  #cookies+=c
  120.  #cook = "Cookie: "
  121.  #cook+=' '.join(cookies)
  122.  [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)
  123.  lold=l
  124.  #cookies=kook(cookies)
  125.  cookies+=c
  126.  cook = "Cookie: "
  127.  cook+=' '.join(cookies)
  128.  #[c,l] = s2("profil.wp.pl","GET "+lold,1,"http://czat.wp.pl/i,5,chat.html",cook)
  129.  #cookies=kook(cookies)
  130.  #cookies+=c
  131.  #cook = "Cookie: "
  132.  #cook+=' '.join(cookies)
  133.  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)
  134.  [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)
  135.  cookies=kook(cookies)
  136.  cookies+=c
  137.  cook = "Cookie: "
  138.  cook+=' '.join(cookies)
  139.  l = l.replace("http://dot.wp.pl","")
  140.  [c,l] = s2("dot.wp.pl","GET "+l,0,"http://profil.wp.pl"+lold,cook)
  141.  cookies+=c
  142.  cook = "Cookie: "
  143.  cook+=' '.join(cookies)
  144.  l=l.replace("http://czat.wp.pl","")
  145.  lold=l
  146.  [c,l] = s2("czat.wp.pl","GET "+l,1,"http://profil.wp.pl"+lold,cook)
  147.  cookies=kook(cookies)
  148.  cookies+=c
  149.  cook = "Cookie: "
  150.  cook+=' '.join(cookies)
  151.  return [cook,s2("czat.wp.pl","GET /auth,tak,i,5,nick,"+nickname+",chat.html",1,"http://czat.wp.pl"+lold,cook,1)]
  152.  
  153. def is_upper(sfx, i):
  154.  code = int(sfx, 16)
  155.  maxlen = len(sfx) * 2
  156.  return (code >> maxlen) & (1 << (maxlen - i - 1))
  157.  
  158. def is_special(sfx, i):
  159.  code = int(sfx, 16)
  160.  maxlen = len(sfx) * 2
  161.  return ((code << maxlen) >> maxlen) & (1 << (maxlen - i - 1))
  162.  
  163. #
  164. # funkcje hash2nick() oraz nick2hash() przepisane z programu WP Gate / rawsock.31337.pl
  165. #
  166.  
  167. def hash2nick(hash):
  168.  nick = ""
  169.  if hash[0] == 'a':
  170.   nick = '~'
  171.  if hash.find('|') == -1:
  172.   return hash
  173.  [mid,sfx] = hash.split('|')
  174.  mid=mid[1:]
  175.  i1 = 0
  176.  while i1 < len(mid):
  177.   if (is_upper(sfx, i1) != 0) and (is_special(sfx, i1) == 0):
  178.    nick = nick + chr(ord(mid[i1]) - 32)
  179.   else:
  180.    nick = nick + mid[i1]
  181.   i1 = i1 + 1
  182.  return nick
  183.  
  184. def nick2hash(nick):
  185.  if nick[0] == '~':
  186.   hash = 'a'
  187.   start = 1
  188.  else:
  189.   hash = 'b'
  190.   start = 0
  191.  a = "ABCDEFGHIJKLMNOPQRSTUWXYZ"
  192.  maxlen = (1 + ((len(nick) - start - 1) / 4)) * 4
  193.  i = start
  194.  tmp = ""
  195.  upcode = 0
  196.  i2 = 0
  197.  while i < len(nick):
  198.   i2 = 0
  199.   while i2 < 25:
  200.    if a[i2] == nick[i]:
  201.     upcode = upcode | (1 << (maxlen - i - 1 + start))
  202.    i2 = i2 + 1
  203.   hash = hash + nick[i].lower()
  204.   i = i + 1
  205.  up = "%Lx" % upcode
  206.  if len(up) < maxlen/4:
  207.   tmp = "0"
  208.   i = 1
  209.   while i < maxlen/4 - len(up):
  210.    tmp = tmp + "0"
  211.    i = i + 1
  212.   tmp = tmp + up
  213.   up = tmp
  214.  hash = hash + '|' + up + "0" * len(up)
  215.  return hash
  216.  
  217. def s33(nickname, cookies):
  218.  s = socket(AF_INET, SOCK_STREAM)
  219.  s.connect(("czati1.wp.pl", 80))
  220.  z = "GET /getticket.html?nick=%s HTTP/1.1\r\n" % nickname
  221.  z = z + ("User-Agent: Mozilla/4.0 (Windows 2000 5.0) Java/1.6.0\r\n"
  222.           "Host: czati1.wp.pl\r\n"
  223.           "Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n"
  224.           "Connection: keep-alive\r\n")
  225.  z = z + cookies + "\r\n\r\n"
  226.  s.send(z)
  227.  bilet = findall("\r\n\r\n(.*)", s.recv(2224))[0]
  228.  bilet = bilet.replace('+','%2B')
  229.  bilet = bilet.replace('/', '%2F')
  230.  s.close()
  231.  return bilet
  232.  
  233. def jedziesz(sock, ID):
  234.  bufor = ""
  235.  koniec = 0
  236.  nickname = ""
  237.  nickhash = ""
  238.  username = ""
  239.  password = ""
  240.  sock.send(("kdw.py : uzycie:\r\n"
  241.             "kdw.py : /set nick twoj_nick\r\n"
  242.             "kdw.py : /server 127.0.0.1 9999 login_wp:haslo_wp\r\n"
  243.             "kdw.py : login_wp podawaj bez @wp.pl na koncu\r\n"))
  244.  while 1:
  245.   b1=""
  246.   b1=sock.recv(1024)
  247.   if b1 == "": return
  248.   bufor += b1
  249.   if not bufor.find("NICK") == -1:
  250.    nickname = findall("NICK (.*?)(\r|\n)", bufor)[0][0]
  251.   if not bufor.find("PASS") == -1:
  252.    password = findall("PASS (.*?)(\r|\n)", bufor)[0][0]
  253.   if (password != "") and (nickname != ""):
  254.    break
  255.  [username, password] = password.split(':')
  256.  nickhash = nick2hash(nickname)
  257.  sys.stdout.write("nick: %s\n" % nickname)
  258.  gniazda = []
  259.  gniazda.append(sock)
  260.  wpserv = socket(AF_INET, SOCK_STREAM)
  261.  wpserv.connect(("212.77.100.125", 5579))
  262.  try:
  263.   [cookies,mag] = dawaj_magic(nickname,username,password)
  264.   wpserv.send("NICK %s\r\n" % nickhash)
  265.   wpserv.send("PASS %s\r\n" % s33(nickname, cookies))
  266.   smag = wpserv.recv(512)
  267.   wpserv.send("USER 192.168.0.3 8 %s140b :Czat-Applet\r\n" % (koduj(mag, findall(".*?MAGIC.*?:(.*?)\r\n", smag)[0])))
  268.  except:
  269.   dawaj_date()
  270.   my_err = "[%3d] ERROR: sprawdz swoje polaczenie sieciowe z internetem, poprawnosc wprowadzonego hasla i nicka lub zmiany autoryzacji\r\n" % ID
  271.   sys.stdout.write(my_err)
  272.   sock.send(my_err)
  273.   sock.close()
  274.   return
  275.  gniazda.append(wpserv)
  276.  sock.send(":czat.wp.pl 001 " + nickname + " :Welcome to the Internet Relay Network\r\n")
  277.  
  278.  while 1:
  279.   (dr, dw, de) = select.select(gniazda, [], [])
  280.   for gotowe in dr:
  281.    if gotowe == sock:
  282.     try:
  283.      bufor = gotowe.recv(1024)
  284.      if bufor == "":
  285.       koniec = 1
  286.       break
  287.      tabbuf = bufor.split(' ')
  288.      if tabbuf[0] == "PRIVMSG":
  289.          if tabbuf[1][0] != '#':
  290.              bufor = bufor.replace(tabbuf[1], nick2hash(tabbuf[1]))
  291.      elif tabbuf[0] == "JOIN":
  292.       bufor = bufor.replace("JOIN", "WPJOIN")
  293.      elif (tabbuf[0] == "WHOIS") and (len(tabbuf)>1):
  294.       bufor = bufor.replace(tabbuf[1], nick2hash(tabbuf[1]))
  295.      wpserv.send(bufor)
  296.     except:
  297.      dawaj_date()
  298.      print "[%3d] nick: %s = blad odczytu/zapisu z/do gniazda" % (ID, nickname)
  299.      koniec = 1
  300.      break
  301.    if gotowe == wpserv:
  302.     bufor = ""
  303.     try:
  304.      while 1:
  305.          bu2 = gotowe.recv(1024)
  306.          if bu2 == "":
  307.              koniec = 1
  308.              break
  309.          if bu2[len(bu2) - 1] == '\n':
  310.              bufor += bu2
  311.              break
  312.          bufor += bu2
  313.      tab = findall("(.*?\n)", bufor)
  314.      for linia in tab:
  315.       linia = linia.replace(nickhash, nickname)
  316.       try:
  317.        tabbuf = linia.split(' ')
  318.        if tabbuf[1] == "PRIVMSG":
  319.         (mnick, mchan, mmsg) = findall("(\S+) PRIVMSG (\S+) :(.*)\r", linia)[0]
  320.         mn = findall(":(\S+?)!", mnick)[0]
  321.         mn2 = hash2nick(mn)
  322.         mnick = mnick.replace(mn, mn2)
  323.         mchan = hash2nick(mchan)
  324.         linia = "%s PRIVMSG %s :%s\r\n" % (mnick, mchan, mmsg)
  325.         linia=linia.replace("<b>","\x02")
  326.         linia=linia.replace("<u>","")
  327.         linia=linia.replace("<i>","")
  328.         linia=linia.replace("<color=\"kolor21\">",'\x03'+"06")
  329.         linia=linia.replace("<color=\"kolor25\">",'\x03'+"11")
  330.         linia=linia.replace("<color=\"kolor18\">",'\x03'+"10")
  331.         linia=linia.replace("<color=\"kolor16\">",'\x03'+"09")
  332.         linia=linia.replace("<color=\"kolor20\">",'\x03'+"14")
  333.         linia=linia.replace("<color=\"kolor13\">",'\x03'+"15")
  334.         linia=linia.replace("<color=\"kolor9\">",'\x03'+"04")
  335.         linia=linia.replace("<color=\"kolor8\">",'\x03'+"12")
  336.         linia=linia.replace("<color=\"kolor7\">",'\x03'+"08")
  337.         linia=linia.replace("<color=\"kolor6\">",'\x03'+"03")
  338.         linia=linia.replace("<color=\"kolor3\">",'\x03'+"05")
  339.         linia=linia.replace("<color=\"kolor5\">",'\x03'+"07")
  340.         linia=linia.replace("<color=\"kolor1\">",'\x03'+"05")
  341.         linia=sub("<size=\"\d+\">","",linia)
  342.         linia=sub("<c.*?=\".+\">","",linia)
  343.         linia=sub("\{.+\}\{.+\}\{.+\}","",linia)
  344.        elif tabbuf[1] == "WPJOIN":
  345.         mnick = tabbuf[0]
  346.         mn = findall(":(\S+?)!", mnick)[0]
  347.         mn2 = hash2nick(mn)
  348.         mnick = mnick.replace(mn, mn2)
  349.         mchan = linia.split(' ')[2]
  350.         linia = "%s JOIN :%s\r\n" % (mnick, mchan)
  351.        elif tabbuf[1] == "PART":
  352.         mnick = tabbuf[0]
  353.         mn = findall(":(\S+?)!", mnick)[0]
  354.         mn2 = hash2nick(mn)
  355.         mnick = mnick.replace(mn, mn2)
  356.         mchan = tabbuf[2]
  357.         linia = "%s PART %s :%s\r\n" % (mnick, mchan, mn2)
  358.        elif tabbuf[1] == "QUIT":
  359.         mnick = tabbuf[0]
  360.         mn = findall(":(\S+?)!", mnick)[0]
  361.         mn2 = hash2nick(mn)
  362.         linia = linia.replace(mn, mn2)
  363.        elif tabbuf[1] == "353":
  364.         nl2 = []
  365.         b = findall("(\S+) 353 \S+ = (\S+) :(.*)", linia)[0]
  366.         nl = findall("(\S+)", b[2])
  367.         for i in nl:
  368.          nl2.append(hash2nick(i))
  369.         linia = "%s 353 %s = %s :%s\r\n" % (b[0], nickname, b[1], ' '.join(nl2))
  370.       except:
  371.        pass
  372.       sock.send(linia)
  373.     except:
  374.      dawaj_date()
  375.      print "[%3d] nick: %s = blad odczytu/zapisu z/do gniazda" % (ID, nickname)
  376.      koniec = 1
  377.      break
  378.   if koniec == 1:
  379.    sock.close()
  380.    wpserv.close()
  381.    break
  382.  
  383. s = socket(AF_INET, SOCK_STREAM)
  384. try:
  385.  s.bind(('', BindPort))
  386. except:
  387.  dawaj_date()
  388.  print "ERROR: nie mozna zabindowac portu %s, wybierz inny" % BindPort
  389.  s.close()
  390.  sys.exit()
  391. dawaj_date()
  392. try:
  393.  print "server: %s:%d" % (gethostbyname(gethostname()), BindPort)
  394. except:
  395.  print "server: 127.0.0.1:%d" % BindPort
  396. s.listen(5)
  397. cID = 1
  398. while 1:
  399.  c, cinfo = s.accept()
  400.  dawaj_date()
  401.  sys.stdout.write("[%3d] %s:%s = " % ((cID, ) + cinfo))
  402.  start_new_thread(jedziesz, (c, cID, ))
  403.  cID = cID + 1
  404. s.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement