Advertisement
Guest User

emag challenge boot

a guest
Oct 26th, 2011
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.86 KB | None | 0 0
  1. # code by : tdxev
  2. # my blog : tdxev.com
  3. # teams   : insecurity.ro
  4. #         : rstcenter.com
  5. # version : 2011.10.26
  6.  
  7. import json
  8. import random
  9. import shelve
  10. import socket
  11. import codecs
  12. import threading
  13. import time
  14. import os.path
  15. import urllib2
  16. from urllib2            import URLError, HTTPError
  17.  
  18.  
  19. #-- [ USER CONFIG ]
  20. #-- browser-ul folosit (Firefox/7.0.1)
  21. appUserAgent                  = ''
  22. #-- ataca doar useri care au level in intervalul specificat ( interval 1-10 )
  23. attack_level              = [1, 10]
  24. #-- timpul maxim care va astepta inainte sa trimita raspunsul in cazul in care exita in baza de date (interval 0 - 5)
  25. wait_before_send_answer       = 2
  26. #showMarker(map,41.58630217651367,21.89090592529297,146696,'personal','on','7',false,'personala', 'Y',23424234); | user_id = 23424234
  27. #-- id-ul utilizatorului luat din sursa pagini html(Y)
  28. user_id = ''
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. #-- [ SYS CONFIG ]
  37. page_get_users_online         = "http://reteaua.emag.ro/show_more_markers"
  38. page_get_users_online_params  = "zoom_level=%s&from=ajax"
  39. page_get_user_info            = "http://reteaua.emag.ro/show_antenna_info"
  40. page_get_user_info_params     = "antenna_id=%s"
  41. db_file                       = "file.db"
  42.  
  43. #-- [ SYS BLOBAL VARIABLES ]
  44. go_attack = None
  45. s = None
  46. db = None
  47. antennas = None
  48.  
  49. class database():
  50.   def __init__(self, database_file):
  51.     if not os.path.isfile(database_file) :
  52.       self.create_db_file(database_file)
  53.     self.load_database(database_file)
  54.  
  55.   def create_db_file(database_file):
  56.     self.tables = shelve.open(db_file, "n")
  57.     self.tables["intrebari"] = {}
  58.     self.tables.close()
  59.    
  60.   def load_database(self, database_file):
  61.     self.tables = shelve.open(db_file, "w", writeback=1)
  62.     print "[INFO] Load database file, Questions loaded:", len(self.tables["intrebari"])
  63.  
  64.   def close_db(self, data):
  65.     self.tables.close()
  66.    
  67.   def get_rasp(self, intr_id):
  68.     if intr_id in self.tables["intrebari"].keys():
  69.       return self.tables["intrebari"][int(intr_id)]["rasp"]
  70.     else :
  71.       return False
  72.  
  73.   def add_intr(self, intr_id,intr,rasp,v1,v2,v3,v4,rc):
  74.     print "[INFO] Add question to database :", intr_id
  75.     self.tables["intrebari"][int(intr_id)] = {"intr":intr,"rasp":rasp,"v1":v1,"v2":v2,"v3":v3,"v4":v4,"rc":rc}
  76.     self.tables.sync()
  77.  
  78.   def add_rasp(self, intr_id, rasp_id, rasp_corect):
  79.     print "[INFO] Add answers to databse :", rasp_id, "for question :", intr_id
  80.     self.tables["intrebari"][int(intr_id)]["rasp"] = rasp_id
  81.     self.tables["intrebari"][int(intr_id)]["rc"]   = rasp_corect
  82.     self.tables.sync()
  83.  
  84. def getPage(url, params):
  85.   global user_cookies
  86.   global appUserAgent
  87.   headers = {}
  88.   headers['Cookie'] = user_cookies
  89.   headers['User-Agent'] = appUserAgent
  90.   headers['Referrer'] = 'http://reteaua.emag.ro/reteaua'
  91.   req = urllib2.Request(url,params, headers)
  92.   print url
  93.   try:
  94.     page = urllib2.urlopen(req)
  95.   except HTTPError, e :
  96.     print "[ERROR]:", str(e.code)
  97.   except URLError, e :
  98.     print "[ERROR]", str(e.reason)
  99.   else:
  100.     return page.read()
  101.  
  102. class gameAntennas():
  103.   def __init__(self):
  104.     self.onlineAntennas = {}
  105.     self.attackedAntennas = []
  106.     self.lastAttackAntennaID = -1
  107.  
  108.   def addAntenna(self, user_id, user_data):
  109.     if not (user_id in self.attackedAntennas) :
  110.       self.onlineAntennas[user_id] = user_data
  111.  
  112.   def delAntenna(self, user_id):
  113.     if user_id in self.onlineAntennas.keys():
  114.       del self.onlineAntennas[user_id]
  115.  
  116.   def addAntennaAsAttacked(self, user_id):
  117.     self.attackedAntennas.append(user_id)
  118.  
  119.   def getRandomUserID(self):
  120.     rand_user_id =  random.randrange(0, self.countOnlineUsers() - 1)
  121.     return self.onlineAntennas.keys()[rand_user_id]
  122.  
  123.   def getAntennaToAttack(self):
  124.     self.lastAttackAntennaID = self.getRandomUserID()
  125.     while self.lastAttackAntennaID in self.attackedAntennas :
  126.       self.lastAttackAntennaID = self.getRandomUserID()
  127.     return self.lastAttackAntennaID
  128.  
  129.   def countOnlineUsers(self):
  130.     return len(self.onlineAntennas)
  131.  
  132. class usersAutoUpdate(threading.Thread):
  133.   def __init__(self):
  134.     threading.Thread.__init__(self)
  135.     self.getUsers()
  136.     self.waitTime = 60
  137.  
  138.   def run(self):
  139.     import random
  140.     timeS = time.time()
  141.     while True:
  142.       rand = random.randrange(30,60)
  143.       if  time.time() - timeS > (self.waitTime + rand) :
  144.         timeS = time.time()
  145.         self.getUsers()
  146.  
  147.   def getUsers(self) :
  148.     global antennas
  149.     global attack_max_level
  150.     print "[INFO] Update Online Users"
  151.     rand = random.randrange(1,8)
  152.     data = getPage(page_get_users_online, page_get_users_online_params % str(rand))
  153.     if not data :
  154.       print "[ ERROR ] Check your cookies!!!"
  155.       exit()
  156.     uList = json.loads(data);
  157.     for user in uList :
  158.       if user['tip'] == 'oponent' and  int(user['level']) in range(attack_level[0], attack_level[1]+1) :
  159.         antennas.addAntenna(user['id'],{'nume':user['nume'], 'level':user['level'],'customer_id':user['customer_id']})
  160.     print "[INFO] Users online:", antennas.countOnlineUsers()
  161.  
  162. class gameClient(threading.Thread):
  163.   def __init__(self):
  164.     threading.Thread.__init__(self)
  165.     self.HOST = '188.215.38.73'
  166.     self.PORT = 8080
  167.     self.last_question_id = -1
  168.     self.last_question_answers = -1
  169.     self.last_question_answer = False
  170.  
  171.   def sendData(self, data):
  172.     global s
  173.     send_data = data + chr(00)
  174.     s.send(send_data)
  175.     print data
  176.    
  177.   def escape(self, data):
  178.     return data.replace("'",'\\\'')
  179.    
  180.   def run(self):
  181.     import random
  182.     global db
  183.     global antennas
  184.     global go_attack
  185.     global last_atack_id
  186.     global wait_before_send_answer
  187.     global s
  188.     global user_id
  189.     s.connect((self.HOST, self.PORT))
  190.     self.sendData('<policy-file-request/>')
  191.     data = s.recv(4096)
  192.     print data
  193.     self.sendData('{"id":' + user_id + ',"action":"login","avatar":"http://static.emag.ro/images/login/avatar.png","name":"scio"}')
  194.     while True :
  195.       rdata = s.recv(4096)
  196.       if rdata :
  197.         print "raw:",rdata
  198.         cmds = rdata.split("\x00")
  199.         for data in cmds :
  200.           if len(data) > 1 :
  201.                         #print data
  202.                         if data.find("\x00") != -1 :
  203.                           data = data[:data.find("\x00")]
  204.                          
  205.                         try:
  206.                           data = unicode(data.strip(codecs.BOM_UTF8), 'latin-1').encode("latin-1", "xmlcharrefreplace")
  207.                         except UnicodeDecodeError,e:
  208.                             print "ERROR", data
  209.  
  210.                         jData = json.loads(data)
  211.                        
  212.                         if jData['action'] == 'error':
  213.                           print "ERROR",jData['error']
  214.                           print jData
  215.                           s.close()
  216.                           exit()
  217.  
  218.                         if jData['action'] == 'ping':
  219.                           self.sendData('{"action":"pong"}')
  220.                          
  221.                          
  222.                         if jData['action'] == 'logout':
  223.                           antennas.delAntenna(jData['id'])
  224.                            
  225.                        
  226.                         # challenge DATA
  227.                         if jData['action'] == 'challenge':
  228.  
  229.                           # challenge propus / acceptat
  230.                           if 'from' in jData:
  231.                             if jData['credits'] < 101 and go_attack :
  232.                               self.sendData('{"action":"challenge","accept":true}')
  233.                              
  234.                           if 'response' in jData:
  235.                             if jData['response'] == "challenge_already":
  236.                               go_attack = True
  237.                               print "[INFO] Partenerul este angajat in alta confruntare."
  238.                              
  239.                             if jData['response'] == "challenge_denied":
  240.                               go_attack = True
  241.                               print "[INFO] Partenerul a refuzat provocarea."
  242.                              
  243.                             if jData['response'] == "challenge_accepted":
  244.                               go_attack = False
  245.                               print "[INFO] Provocarea a fost acceptata, pregatiti-va de duel!"
  246.  
  247.                             if jData['response'] == "challenge_waiting":
  248.                               go_attack = True
  249.                               print "[INFO] Se asteaptă raspunsul partenerului..."
  250.  
  251.                             if jData['response'] == "challenge_timeout":
  252.                               go_attack = True
  253.                               print "[INFO] Partenerul nu a raspuns in timp util."
  254.                              
  255.                             if jData['response'] == "challenge_yourself":
  256.                               go_attack = True
  257.                               print "[INFO] Te-ai provocat pe tine."                              
  258.  
  259.                             if jData['response'] == "challenge_not_logged":
  260.                               antennas.delAntenna(antennas.lastAttackAntennaID)
  261.                               go_attack = True
  262.                               print "[INFO] Partenerul nu este online."    
  263.                              
  264.                             if jData['response'] == "partner_disconnected":
  265.                               antennas.delAntenna(antennas.lastAttackAntennaID)
  266.                               go_attack = True
  267.                               print "[INFO] Partenerul s-a deconectat."    
  268.                              
  269.                              
  270.                           if 'winner' in jData:
  271.                             antennas.addAntennaAsAttacked(antennas.lastAttackAntennaID)
  272.                             go_attack = True
  273.                          
  274.                           # intrebarea trimisa de server
  275.                           if 'question' in jData:
  276.                             self.last_question = jData['question']['question'].encode("latin-1", "xmlcharrefreplace")
  277.                             self.last_question_id = jData['question']['id']
  278.                             self.last_question_answers = jData['question']['answers']
  279.                             if not (isinstance(self.last_question_answers[0], int) or isinstance(self.last_question_answers[0], float)) :
  280.                                 self.last_question_answers[0] = self.last_question_answers[0].encode("latin-1", "xmlcharrefreplace")
  281.                             if not (isinstance(self.last_question_answers[1], int) or isinstance(self.last_question_answers[1], float)) :
  282.                                 self.last_question_answers[1] = self.last_question_answers[1].encode("latin-1", "xmlcharrefreplace")
  283.                             if not (isinstance(self.last_question_answers[2], int) or isinstance(self.last_question_answers[2], float)) :
  284.                                 self.last_question_answers[2] = self.last_question_answers[2].encode("latin-1", "xmlcharrefreplace")
  285.                             if not (isinstance(self.last_question_answers[3], int) or isinstance(self.last_question_answers[3], float)) :
  286.                                 self.last_question_answers[3] = self.last_question_answers[3].encode("latin-1", "xmlcharrefreplace")
  287.  
  288.                             raspuns = db.get_rasp(self.last_question_id)
  289.                             if raspuns == False :
  290.                               self.last_question_answer = False
  291.                               db.add_intr(self.last_question_id, self.last_question, -1, self.last_question_answers[0], self.last_question_answers[1], self.last_question_answers[2], self.last_question_answers[3], '')
  292.                               self.sendData('{"answer":0,"action":"challenge"}')
  293.                             else:
  294.                               self.last_question_answer = True
  295.                               print "[INFO] Raspunsul a fost trimis din baza de date"
  296.                               time.sleep(random.randrange(0,wait_before_send_answer))
  297.                               self.sendData('{"answer":' + str(raspuns) + ',"action":"challenge"}')
  298.                              
  299.                              
  300.                        
  301.                           if 'correct_answer' in jData:
  302.                             self.last_correct_answer = jData['correct_answer'] # int
  303.                             if self.last_question_answer == False :
  304.                               db.add_rasp(self.last_question_id, self.last_correct_answer,self.last_question_answers[int(self.last_correct_answer)])
  305.                         #end challenge DATA
  306.     s.close()
  307.  
  308. class usersAtackAI(threading.Thread):
  309.   def __init__(self):
  310.     threading.Thread.__init__(self)
  311.     self.attackedUsers = []
  312.     self.waitTime = 10 # odata la 10 de secunde + random
  313.    
  314.   def getAntennaInfo(self, id):
  315.     global antennas
  316.     print "[INFO] Check antenna id :", id
  317.     data = getPage(page_get_user_info, page_get_user_info_params % (id))
  318.     if data.find('class="ataca"') != -1 :
  319.       s_pos = data.find('showBattleScreen(') + len('showBattleScreen(')
  320.       e_pos = data.find(')" class="ataca"')
  321.       uid, aid, credit = data[s_pos:e_pos].split(',')
  322.       return {'user_id':uid.encode('ascii', 'replace'), 'antena_id':aid.encode('ascii', 'replace'), 'credit':credit.encode('ascii', 'replace')}
  323.     elif data.find("Ai fost deja in duel cu acest utilizator.") != -1:
  324.       antennas.addAntennaAsAttacked(antennas.lastAttackAntennaID)
  325.       return False
  326.     else :
  327.       antennas.delAntenna(antennas.lastAttackAntennaID)
  328.       return False
  329.    
  330.  
  331.   def run(self):
  332.     import random
  333.     global antennas
  334.     global s
  335.     global go_attack
  336.     timeS = time.time()
  337.     while True:
  338.       rand = random.randrange(30,60)
  339.       if  time.time() - timeS > (self.waitTime + rand) :
  340.         timeS = time.time()
  341.         if antennas.countOnlineUsers() > 1 and go_attack:
  342.           antennas.getAntennaToAttack()
  343.           if not go_attack : break
  344.           u_data = self.getAntennaInfo(antennas.lastAttackAntennaID)
  345.           if u_data :
  346.             print "[INFO] Attack user:", antennas.onlineAntennas[antennas.lastAttackAntennaID]['nume']
  347.             go_attack = False
  348.             send_data = '{"id":' + u_data['user_id'] + ',"action":"challenge","antenna_id":' + u_data['antena_id'] + ',"credits":' + u_data['credit'] + '}'+ chr(00)
  349.             print send_data
  350.             s.send(send_data)
  351.           else :
  352.             print "[INFO] No Data"
  353.  
  354. def main():
  355.   global s
  356.   global go_attack
  357.   global db
  358.   global antennas
  359.  
  360.   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  361.   go_attack = True
  362.   db = database(db_file)  
  363.   antennas = gameAntennas()
  364.  
  365.   uAU = usersAutoUpdate()
  366.   uAU.setDaemon(True)
  367.   uAU.start()
  368.  
  369.   gC = gameClient()
  370.   gC.setDaemon(True)
  371.   gC.start()
  372.  
  373.   uAAI = usersAtackAI()
  374.   uAAI.setDaemon(True)
  375.   uAAI.start()
  376.  
  377.   while True:
  378.     pass
  379.  
  380.  
  381. if __name__ == "__main__" :
  382.   main()
  383.  
  384.  
  385.  
  386.  
  387.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement