Пример #1
0
 def __init__(self, hop, ip, timings, location_id=None, location_name=None,
              country_name=None, country_code=None, state_region=None,
              city=None, zipcode=None, lat=None, lon=None, is_final=0):
     
     self.hop = hop
     self.ip = ip
     self.timings = timings
     self.is_final = is_final
     
     if location_id is not None:
         self.location_id = location_id
         self.location_name = location_name
         self.country_name = country_name
         self.country_code = country_code
         self.state_region = state_region
         self.city = city
         self.zipcode = zipcode
         self.lat = decimal.Decimal(lat)
         self.lon = decimal.Decimal(lon)
     else:
         iprange = IPRange.ip_location(ip)
         self.location_id = iprange.location_id
         self.location_name = iprange.location.fullname
         self.country_name = iprange.location.country_name
         self.country_code = iprange.location.country_code
         self.state_region = iprange.location.state_region
         self.city = iprange.location.city
         self.zipcode = iprange.location.zipcode
         self.lat = decimal.Decimal(iprange.location.lat)
         self.lon = decimal.Decimal(iprange.location.lon)
    def initLogin(self, ip, port, crypto_v1):
        from geoip.models import IPRange

        if crypto_v1:
            from agents.CryptoLib_v1 import crypto, CryptoLib, aggregatorKey, aes_decrypt
        else:
            from agents.CryptoLib import crypto, CryptoLib, aggregatorKey, aes_decrypt

        # get new challenge
        if self.banned:
            return False

        from geoip.models import IPRange

        # Check if network is banned
        iprange = IPRange.ip_location(ip)
        if iprange and iprange.banned:
            return False
        
        crypto = CryptoLib()
        challenge = crypto.generateChallenge()
        
        loginProcess = LoginProcess()
        loginProcess.agent_id = self.id
        loginProcess.ip = ip
        loginProcess.port = port
        loginProcess.challenge = challenge
        loginProcess.save()

        return loginProcess
    def create(versionNo, agentType, ip,
               publicKeyMod, publicKeyExp,
               username, password, AESKey):
        from geoip.models import IPRange
        # check username and password
        user = authenticate(username=username, password=password)
        if user is not None and agentType in ["MOBILE", "DESKTOP"]:
            agent = Agent()
            agent.agentVersion = versionNo
            agent.agent_type = agentType
            agent.registered_ip = ip
            agent.uptime = 0
            agent.publicKeyMod = publicKeyMod
            agent.publicKeyExp = publicKeyExp
            agent.user = user
            agent.AESKey = AESKey
            agent.activated = True

            # get country by geoip
            iprange = IPRange.ip_location(ip)
            agent.country = iprange.location.country_code
            agent.latitude = iprange.location.lat
            agent.longitude = iprange.location.lon
            agent.iprange_id = iprange.id
            agent.location_id = iprange.location_id
            if agent.registered_at is None:
                agent.registered_at = datetime.datetime.now()

            agent.token = Agent.gen_token(agent)

            agent.save()
            
            return agent
        else:
            raise Exception("User not registered or login failed: user '%s'" % username)
Пример #4
0
    def initLogin(self, ip, port, crypto_v1):
        from geoip.models import IPRange

        if crypto_v1:
            from agents.CryptoLib_v1 import crypto, CryptoLib, aggregatorKey, aes_decrypt
        else:
            from agents.CryptoLib import crypto, CryptoLib, aggregatorKey, aes_decrypt

        # get new challenge
        if self.banned:
            return False

        from geoip.models import IPRange

        # Check if network is banned
        iprange = IPRange.ip_location(ip)
        if iprange and iprange.banned:
            return False

        crypto = CryptoLib()
        challenge = crypto.generateChallenge()

        loginProcess = LoginProcess()
        loginProcess.agent_id = self.id
        loginProcess.ip = ip
        loginProcess.port = port
        loginProcess.challenge = challenge
        loginProcess.save()

        return loginProcess
Пример #5
0
    def create(versionNo, agentType, ip, publicKeyMod, publicKeyExp, username,
               password, AESKey):
        from geoip.models import IPRange
        # check username and password
        user = authenticate(username=username, password=password)
        if user is not None and agentType in ["MOBILE", "DESKTOP"]:
            agent = Agent()
            agent.agentVersion = versionNo
            agent.agent_type = agentType
            agent.registered_ip = ip
            agent.uptime = 0
            agent.publicKeyMod = publicKeyMod
            agent.publicKeyExp = publicKeyExp
            agent.user = user
            agent.AESKey = AESKey
            agent.activated = True

            # get country by geoip
            iprange = IPRange.ip_location(ip)
            agent.country = iprange.location.country_code
            agent.latitude = iprange.location.lat
            agent.longitude = iprange.location.lon
            agent.iprange_id = iprange.id
            agent.location_id = iprange.location_id
            if agent.registered_at is None:
                agent.registered_at = datetime.datetime.now()

            agent.token = Agent.gen_token(agent)

            agent.save()

            return agent
        else:
            raise Exception("User not registered or login failed: user '%s'" %
                            username)
 def logout(self):
     # TODO: update uptime
     LoggedAgent.objects.filter(agent_id=self.id).delete()
     
     # Remove from networks list
     iprange = IPRange.ip_location(self.lastKnownIP)
     iprange.nodes_count -= 1
     iprange.save()
     
     location = iprange.location
     location.nodes_count -= 1
     location.save()
Пример #7
0
    def logout(self):
        # TODO: update uptime
        LoggedAgent.objects.filter(agent_id=self.id).delete()

        # Remove from networks list
        iprange = IPRange.ip_location(self.lastKnownIP)
        iprange.nodes_count -= 1
        iprange.save()

        location = iprange.location
        location.nodes_count -= 1
        location.save()
Пример #8
0
    def create(websiteReportMsg, agent):
        report = WebsiteReport()

        website_report = websiteReportMsg.report
        icm_report     = website_report.header
        website_report_detail = website_report.report

        # read WebsiteReportDetail
        report.url = website_report_detail.websiteURL
        report.status_code = website_report_detail.statusCode
        print "Report status code: '%s'" % report.status_code

        if website_report_detail.HasField('responseTime'):
            report.response_time = website_report_detail.responseTime
            print "Response time: '%s'" % report.response_time
        if website_report_detail.HasField('bandwidth'):
            report.bandwidth = website_report_detail.bandwidth
            print "Bandwidth: '%s'" % report.bandwidth

        if website_report_detail.HasField('redirectLink'):
            report.redirect_link = website_report_detail.redirectLink
            print "Redirect link: '%s'" % report.redirect_link
        if website_report_detail.HasField('htmlResponse'):
            report.html_response = website_report_detail.htmlResponse
            print "Html response: '%s'" % report.html_response
        
        if website_report_detail.HasField('htmlMedia'):
            html_media = tarfile.open(mode='r:gz',
                                      fileobj=website_report_detail.htmlResponse)
            for member in html_media.getmembers():
                f = File(html_media.extractfile(member))
                media_obj = WebsiteReportMedia()
                media_obj.file.save(member.name, f)
                media_obj.save()
                
                report.media_ids.append(media_obj.id)
                print "Adding media id: %s" % media_obj.id
                
                ################################################################
                #TODO:Need to adapt the html code to link to these media files #
                ################################################################

        report.user_id = agent.user.id
        print "User id: '%s'" % agent.user.id

        # read ICMReport
        logging.critical("STATUS CODE: %s" % website_report_detail.statusCode)
        #report.report_id = icm_report.reportID
        report.agent_id = icm_report.agentID
        print "Agent id: '%s'" % report.agent_id
        report.test_id = icm_report.testID if icm_report.testID else None
        print "Test id: '%s'" % report.test_id
        report.time = datetime.datetime.utcfromtimestamp(icm_report.timeUTC)
        report.time_zone = icm_report.timeZone

        # read ICMReport passedNodes
        for node in icm_report.passedNode:
            report.nodes.append(node)

        # read ICMReport TraceRoute
        if icm_report.HasField('traceroute'):
            report.target = icm_report.traceroute.target
            report.hops = icm_report.traceroute.hops
            report.packet_size = icm_report.traceroute.packetSize

            # read ICMReport TraceRoute Traces
            for rcvTrace in icm_report.traceroute.traces:
                report.trace.append(
                        Trace(hop=rcvTrace.hop,
                              ip=rcvTrace.ip,
                              timings=[t for t in rcvTrace.packetsTiming],
                              is_final=(report.target==rcvTrace.ip and 1 or 0))
                )

            # update target location
            iprange = IPRange.ip_location(report.target)
            report.target_country_code = iprange.location.country_code
            print "Target country code: '%s'" % report.target_country_code
            report.target_country_name = iprange.location.country_name
            report.target_lat = iprange.location.lat
            report.target_lon = iprange.location.lon
            report.target_location_id = iprange.location_id
            print "Target location id: '%s'" % iprange.location_id
            report.target_location_name = iprange.location.fullname
            report.target_zipcode = iprange.location.zipcode
            report.target_state_region = iprange.location.state_region
            report.target_city = iprange.location.city

        # get info about target ip
        loggedAgent = agent.getLoginInfo()
        report.agent_ip = loggedAgent.current_ip
        report.agent_location_id = loggedAgent.location_id if loggedAgent.location_id != '' else None
        print "Agent location id: %s" % report.agent_location_id
        report.agent_location_name = loggedAgent.location_name
        report.agent_country_name = loggedAgent.country_name
        report.agent_country_code = loggedAgent.country_code
        report.agent_state_region = loggedAgent.state_region
        report.agent_city = loggedAgent.city
        report.agent_zipcode = loggedAgent.zipcode
        report.agent_lat = loggedAgent.latitude
        report.agent_lon = loggedAgent.longitude
        
        report.save()
        
        return report
    def finishLogin(loginProcessID, cipheredChallenge, crypto_v1=False, ip=None):
        from geoip.models import IPRange
        # get login process
        loginProcess = LoginProcess.objects.get(processID=loginProcessID)

        # get agent instance
        agent = Agent.get_agent(loginProcess.agent_id)

        # check challenge
        if agent.checkChallenge(str(loginProcess.challenge), cipheredChallenge,crypto_v1=crypto_v1):
            # delete already logged agent info
            LoggedAgent.objects.filter(agent_id=agent.id).delete()

            loggedAgent = LoggedAgent()
            loggedAgent.agent_id = agent.id
            loggedAgent.agentInfo = agent
            loggedAgent.current_ip = ip if ip is not None else loginProcess.ip
            loggedAgent.port = loginProcess.port
            loggedAgent.superPeer = agent.superPeer
            loggedAgent.publicKeyMod = agent.publicKeyMod
            loggedAgent.publicKeyExp = agent.publicKeyExp
            loggedAgent.AESKey = agent.AESKey

            # get country by geoip
            from geoip.models import IPRange
            iprange = IPRange.ip_location(loginProcess.ip)
            loggedAgent.country_code = iprange.location.country_code
            loggedAgent.country_name = iprange.location.country_name
            loggedAgent.latitude = iprange.location.lat
            loggedAgent.longitude = iprange.location.lon
            loggedAgent.location_id = iprange.location_id
            loggedAgent.location_name = iprange.location.fullname
            loggedAgent.zipcode = iprange.location.zipcode
            loggedAgent.state_region = iprange.location.state_region
            loggedAgent.city = iprange.location.city
            loggedAgent.save()
            
            
            iprange.nodes_count += 1
            iprange.save()
            
            location = iprange.location
            location.nodes_count += 1
            location.save()
            
            
            # update agent information
            agent.lastKnownCountry = iprange.location.country_code
            agent.lastKnownLatitude = iprange.location.lat
            agent.lastKnownLongitude = iprange.location.lon
            agent.lastKnownIP = loginProcess.ip
            agent.lastKnownPort = loginProcess.port
            agent.save()

            # delete login process
            loginProcess.delete()
            
            return agent

        else:
            logging.error('Challenge not ok')
            raise Exception('Challenge not ok')
            return None
Пример #10
0
    def finishLogin(loginProcessID, cipheredChallenge, crypto_v1=False):
        from geoip.models import IPRange
        # get login process
        loginProcess = LoginProcess.objects.get(processID=loginProcessID)

        # get agent instance
        agent = Agent.get_agent(loginProcess.agent_id)

        # check challenge
        if agent.checkChallenge(str(loginProcess.challenge),
                                cipheredChallenge,
                                crypto_v1=crypto_v1):
            # delete already logged agent info
            LoggedAgent.objects.filter(agent_id=agent.id).delete()

            loggedAgent = LoggedAgent()
            loggedAgent.agent_id = agent.id
            loggedAgent.agentInfo = agent
            loggedAgent.current_ip = loginProcess.ip
            loggedAgent.port = loginProcess.port
            loggedAgent.superPeer = agent.superPeer
            loggedAgent.publicKeyMod = agent.publicKeyMod
            loggedAgent.publicKeyExp = agent.publicKeyExp
            loggedAgent.AESKey = agent.AESKey

            # get country by geoip
            from geoip.models import IPRange
            iprange = IPRange.ip_location(loginProcess.ip)
            loggedAgent.country_code = iprange.location.country_code
            loggedAgent.country_name = iprange.location.country_name
            loggedAgent.latitude = iprange.location.lat
            loggedAgent.longitude = iprange.location.lon
            loggedAgent.location_id = iprange.location_id
            loggedAgent.location_name = iprange.location.fullname
            loggedAgent.zipcode = iprange.location.zipcode
            loggedAgent.state_region = iprange.location.state_region
            loggedAgent.city = iprange.location.city
            loggedAgent.save()

            iprange.nodes_count += 1
            iprange.save()

            location = iprange.location
            location.nodes_count += 1
            location.save()

            # update agent information
            agent.lastKnownCountry = iprange.location.country_code
            agent.lastKnownLatitude = iprange.location.lat
            agent.lastKnownLongitude = iprange.location.lon
            agent.lastKnownIP = loginProcess.ip
            agent.lastKnownPort = loginProcess.port
            agent.save()

            # delete login process
            loginProcess.delete()

            return agent

        else:
            logging.error('Challenge not ok')
            raise Exception('Challenge not ok')
            return None