예제 #1
0
	def handle_page_visit(self):
		if not self.message_id:
			return
		if self.message_id == self.config.get('server.secret_id'):
			return
		if not self.campaign_id:
			return
		client_ip = self.get_client_ip()
		headers = []

		campaign = db_manager.get_row_by_id(self._session, db_models.Campaign, self.campaign_id)
		if campaign.has_expired:
			self.logger.info("ignoring page visit for expired campaign id: {0} from IP address: {1}".format(self.campaign_id, client_ip))
			return
		self.logger.info("handling a page visit for campaign id: {0} from IP address: {1}".format(self.campaign_id, client_ip))
		message = db_manager.get_row_by_id(self._session, db_models.Message, self.message_id)

		if message.opened is None and self.config.get('server.set_message_opened_on_visit'):
			message.opened = db_models.current_timestamp()
			message.opener_ip = self.get_client_ip()
			message.opener_user_agent = self.headers.get('user-agent', None)

		query = self._session.query(db_models.LandingPage)
		query = query.filter_by(campaign_id=self.campaign_id, hostname=self.vhost, page=self.request_path[1:])
		landing_page = query.first()

		set_new_visit = True
		visit_id = None
		if self.visit_id:
			visit_id = self.visit_id
			set_new_visit = False
			if landing_page:
				visit = db_manager.get_row_by_id(self._session, db_models.Visit, self.visit_id)
				if visit.message_id == self.message_id:
					visit.count += 1
					visit.last_seen = db_models.current_timestamp()
					self._session.commit()
				else:
					set_new_visit = True
					visit_id = None

		if visit_id is None:
			visit_id = utilities.make_visit_uid()

		if landing_page and set_new_visit:
			kp_cookie_name = self.config.get('server.cookie_name')
			cookie = "{0}={1}; Path=/; HttpOnly".format(kp_cookie_name, visit_id)
			headers.append(('Set-Cookie', cookie))
			visit = db_models.Visit(id=visit_id, campaign_id=self.campaign_id, message_id=self.message_id)
			visit.ip = client_ip
			visit.first_landing_page_id = landing_page.id
			visit.user_agent = self.headers.get('user-agent', '')
			self._session.add(visit)
			self._session.commit()
			self.logger.debug("visit id: {0} created for message id: {1}".format(visit_id, self.message_id))
			visit_count = len(campaign.visits)
			if visit_count > 0 and ((visit_count in (1, 10, 25)) or ((visit_count % 50) == 0)):
				self.server.job_manager.job_run(self.issue_alert, (self.campaign_id, 'visits', visit_count))
			signals.send_safe('visit-received', self.logger, self)

		self._handle_page_visit_creds(campaign, visit_id)
		trained = self.get_query('trained')
		if isinstance(trained, str) and trained.lower() in ['1', 'true', 'yes']:
			message.trained = True
			self._session.commit()
		return headers
예제 #2
0
	def handle_page_visit(self):
		if not self.message_id:
			return
		if self.message_id == self.config.get('server.secret_id'):
			return
		if not self.campaign_id:
			return
		client_ip = self.get_client_ip()

		session = db_manager.Session()
		campaign = db_manager.get_row_by_id(session, db_models.Campaign, self.campaign_id)
		if campaign.has_expired:
			self.logger.info("ignoring page visit for expired campaign id: {0} from IP address: {1}".format(self.campaign_id, client_ip))
			session.close()
			return
		self.logger.info("handling a page visit for campaign id: {0} from IP address: {1}".format(self.campaign_id, client_ip))
		message = db_manager.get_row_by_id(session, db_models.Message, self.message_id)

		if message.opened is None and self.config.get_if_exists('server.set_message_opened_on_visit', True):
			message.opened = db_models.current_timestamp()
			message.opener_ip = self.get_client_ip()
			message.opener_user_agent = self.headers.get('user-agent', None)

		set_new_visit = True
		visit_id = None
		if self.visit_id:
			visit_id = self.visit_id
			set_new_visit = False
			query = session.query(db_models.LandingPage)
			query = query.filter_by(campaign_id=self.campaign_id, hostname=self.vhost, page=self.request_path[1:])
			if query.count():
				visit = db_manager.get_row_by_id(session, db_models.Visit, self.visit_id)
				if visit.message_id == self.message_id:
					visit.visit_count += 1
					visit.last_visit = db_models.current_timestamp()
				else:
					set_new_visit = True
					visit_id = None

		if visit_id is None:
			visit_id = utilities.make_visit_uid()

		if set_new_visit:
			kp_cookie_name = self.config.get('server.cookie_name')
			cookie = "{0}={1}; Path=/; HttpOnly".format(kp_cookie_name, visit_id)
			self.send_header('Set-Cookie', cookie)
			visit = db_models.Visit(id=visit_id, campaign_id=self.campaign_id, message_id=self.message_id)
			visit.visitor_ip = client_ip
			visit.visitor_details = self.headers.get('user-agent', '')
			session.add(visit)
			visit_count = len(campaign.visits)
			if visit_count > 0 and ((visit_count in (1, 10, 25)) or ((visit_count % 50) == 0)):
				alert_text = "{0} visits reached for campaign: {{campaign_name}}".format(visit_count)
				self.server.job_manager.job_run(self.issue_alert, (alert_text, self.campaign_id))
			signals.safe_send('visit-received', self.logger, self)

		if visit_id is None:
			self.logger.error('the visit id has not been set')
			raise RuntimeError('the visit id has not been set')
		self._handle_page_visit_creds(session, visit_id)
		trained = self.get_query('trained')
		if isinstance(trained, str) and trained.lower() in ['1', 'true', 'yes']:
			message.trained = True
		session.commit()
		session.close()
예제 #3
0
    def handle_page_visit(self):
        if not self.message_id:
            return
        if self.message_id == self.config.get('server.secret_id'):
            return
        if not self.campaign_id:
            return
        client_ip = self.get_client_ip()

        session = db_manager.Session()
        campaign = db_manager.get_row_by_id(session, db_models.Campaign,
                                            self.campaign_id)
        if campaign.has_expired:
            self.logger.info(
                "ignoring page visit for expired campaign id: {0} from IP address: {1}"
                .format(self.campaign_id, client_ip))
            session.close()
            return
        self.logger.info(
            "handling a page visit for campaign id: {0} from IP address: {1}".
            format(self.campaign_id, client_ip))
        message = db_manager.get_row_by_id(session, db_models.Message,
                                           self.message_id)

        if message.opened is None and self.config.get(
                'server.set_message_opened_on_visit'):
            message.opened = db_models.current_timestamp()
            message.opener_ip = self.get_client_ip()
            message.opener_user_agent = self.headers.get('user-agent', None)

        set_new_visit = True
        visit_id = None
        if self.visit_id:
            visit_id = self.visit_id
            set_new_visit = False
            query = session.query(db_models.LandingPage)
            query = query.filter_by(campaign_id=self.campaign_id,
                                    hostname=self.vhost,
                                    page=self.request_path[1:])
            if query.count():
                visit = db_manager.get_row_by_id(session, db_models.Visit,
                                                 self.visit_id)
                if visit.message_id == self.message_id:
                    visit.visit_count += 1
                    visit.last_visit = db_models.current_timestamp()
                else:
                    set_new_visit = True
                    visit_id = None

        if visit_id is None:
            visit_id = utilities.make_visit_uid()

        if set_new_visit:
            kp_cookie_name = self.config.get('server.cookie_name')
            cookie = "{0}={1}; Path=/; HttpOnly".format(
                kp_cookie_name, visit_id)
            self.send_header('Set-Cookie', cookie)
            visit = db_models.Visit(id=visit_id,
                                    campaign_id=self.campaign_id,
                                    message_id=self.message_id)
            visit.visitor_ip = client_ip
            visit.visitor_details = self.headers.get('user-agent', '')
            session.add(visit)
            visit_count = len(campaign.visits)
            if visit_count > 0 and ((visit_count in (1, 10, 25)) or
                                    ((visit_count % 50) == 0)):
                alert_text = "{0} visits reached for campaign: {{campaign_name}}".format(
                    visit_count)
                self.server.job_manager.job_run(self.issue_alert,
                                                (alert_text, self.campaign_id))
            signals.safe_send('visit-received', self.logger, self)

        if visit_id is None:
            self.logger.error('the visit id has not been set')
            raise RuntimeError('the visit id has not been set')
        self._handle_page_visit_creds(session, visit_id)
        trained = self.get_query('trained')
        if isinstance(trained,
                      str) and trained.lower() in ['1', 'true', 'yes']:
            message.trained = True
        session.commit()
        session.close()
예제 #4
0
	def handle_page_visit(self):
		if not self.message_id:
			return
		if self.message_id == self.config.get('server.secret_id'):
			return
		if not self.campaign_id:
			return
		client_ip = self.get_client_ip()
		headers = []

		campaign = db_manager.get_row_by_id(self._session, db_models.Campaign, self.campaign_id)
		if campaign.has_expired:
			self.logger.info("ignoring page visit for expired campaign id: {0} from IP address: {1}".format(self.campaign_id, client_ip))
			return
		self.logger.info("handling a page visit for campaign id: {0} from IP address: {1}".format(self.campaign_id, client_ip))
		message = db_manager.get_row_by_id(self._session, db_models.Message, self.message_id)

		if message.opened is None and self.config.get('server.set_message_opened_on_visit'):
			message.opened = db_models.current_timestamp()
			message.opener_ip = self.get_client_ip()
			message.opener_user_agent = self.headers.get('user-agent', None)

		query = self._session.query(db_models.LandingPage)
		query = query.filter_by(campaign_id=self.campaign_id, hostname=self.vhost, page=self.request_path[1:])
		landing_page = query.first()

		set_new_visit = True
		visit_id = None
		if self.visit_id:
			visit_id = self.visit_id
			set_new_visit = False
			if landing_page:
				visit = db_manager.get_row_by_id(self._session, db_models.Visit, self.visit_id)
				if visit.message_id == self.message_id:
					visit.count += 1
					visit.last_seen = db_models.current_timestamp()
					self._session.commit()
				else:
					set_new_visit = True
					visit_id = None

		if visit_id is None:
			visit_id = utilities.make_visit_uid()

		if landing_page and set_new_visit:
			kp_cookie_name = self.config.get('server.cookie_name')
			cookie = "{0}={1}; Path=/; HttpOnly".format(kp_cookie_name, visit_id)
			headers.append(('Set-Cookie', cookie))
			visit = db_models.Visit(id=visit_id, campaign_id=self.campaign_id, message_id=self.message_id)
			visit.ip = client_ip
			visit.first_landing_page_id = landing_page.id
			visit.user_agent = self.headers.get('user-agent', '')
			self._session.add(visit)
			self._session.commit()
			self.logger.debug("visit id: {0} created for message id: {1}".format(visit_id, self.message_id))
			visit_count = len(campaign.visits)
			if visit_count > 0 and ((visit_count in (1, 10, 25)) or ((visit_count % 50) == 0)):
				self.server.job_manager.job_run(self.issue_alert, (self.campaign_id, 'visits', visit_count))
			signals.send_safe('visit-received', self.logger, self)

		self._handle_page_visit_creds(campaign, visit_id)
		trained = self.get_query('trained')
		if isinstance(trained, str) and trained.lower() in ['1', 'true', 'yes']:
			message.trained = True
			self._session.commit()
		return headers