3
@6^                  @   s8   d dl Z d dlZd dlmZmZmZ G dd deZdS )    N)
SpiderFootSpiderFootPluginSpiderFootEventc               @   sh   e Zd ZdZdddddZddd	d
dZdZdZdZe	 fddZ
dd Zdd ZdddZdd ZdS )sfp_securitytrailszuSecurityTrails:Investigate,Passive:Search Engines:apikey:Obtain Passive DNS and other information from SecurityTrails TFd   )api_keyverifycohostsamedomain	maxcohostzSecurityTrails API key.zMVerify co-hosts are valid by checking if they still resolve to the shared IP.z>Treat co-hosted sites on the same target domain as co-hosting?zbStop reporting co-hosted sites after this many are found, as it would likely indicate web hosting.Nr   c             C   s>   || _ | j | _d| _x"t|j D ]}|| | j|< q$W d S )Nr   )sftempStorageresultscohostcountlistkeysopts)selfsfcuserOptsopt r   =/var/www/spiderfoot.crq.systems/modules/sfp_securitytrails.pysetup+   s
    
zsfp_securitytrails.setupc             C   s   dddddgS )N
IP_ADDRESSIPV6_ADDRESSDOMAIN_NAME	EMAILADDRNETBLOCK_OWNERr   )r   r   r   r   watchedEvents7   s    z sfp_securitytrails.watchedEventsc             C   s   ddddddgS )NCO_HOSTED_SITEr   AFFILIATE_DOMAIN_NAMEINTERNET_NAMEAFFILIATE_INTERNET_NAMEPROVIDER_HOSTINGr   )r   r   r   r   producedEvents<   s    z!sfp_securitytrails.producedEvents   c             C   s  d }d| j d i}|dkr,d| d }d }n(dt| }d| d | d	 }d
|d< | jj|| j d d||d}	|	d d kr| jjdd d| _d S |	d d kr| jjd|  d S ytj|	d }|dkr|j	dd S |j	dddkrXt
|j	dg dkr@tjd |r"|j|j	d n
|j	d}| j|||d |S |j|j	dg  |S n|j	dg S W n8 tk
r }
 z| jjdt|
 d d S d }
~
X nX d S )!NZAPIKEYr   domainz)https://api.securitytrails.com/v1/domain/z/subdomainsz4https://api.securitytrails.com/v1/search/list/?page=z{"filter": { "z": "z" } }zapplication/jsonzContent-Type_fetchtimeoutr   )timeout	useragentheaderspostDatacode400429500403zcSecurityTrails API key seems to have been rejected or you have exceeded usage limits for the month.FTcontentz!No SecurityTrails info found for 
subdomainsZrecord_countr   r   recordsr&   z4Error processing JSON response from SecurityTrails: )r.   r/   r0   r1   )r   strr   fetchUrlerror
errorStateinfojsonloadsgetlentimesleepextendquery	Exception)r   qry	querytypepageaccumr9   r+   urlrequestreser   r   r   rA   C   sF    

zsfp_securitytrails.queryc             C   s2  |j }|j}|j}| jrd S | jjd| d |  | jd dkrZ| jjdd d| _d S || jkr|| jjd| d	  d S d| j|< |d#kr|}| j	|d}t
 }t
 }|d k	rx0|D ]&}	d|	krx>|	d D ]2}
|
|krqtd|
| j|}| j| |j|
 qW d|	kr| j| jd kr*q|	d }| jd sh| j j|ddrh| jjd| d  q||kr||kr| jd r| jj|| r| jjd| d  q|j|j  td|| j|}| j| |  jd7  _qW |d$kr|}| j	|d}t
 }|d k	rx|D ]}	d|	kr|	d s6q|	d }|j |kr|j|j  nqtd|| j|}| j| | jj|| jd rtd|| j|}| j| qW |d%kr.|}| j	|d }t
 }|d k	r.xR|D ]J}|j |kr|j|j  nqtd!|d" | | j|}| j| qW d S )&NzReceived event, z, from r   r   zAYou enabled sfp_securitytrails but did not set an API uid/secret!FTz	Skipping z as already mapped.r   r   NETLBLOCK_OWNERipv4Zhost_providerr$   hostnamer   r
   )includeParentsz" because it is on the same domain.r	   zHost z no longer resolves to our IP.r    r&   r   Zwhois_emailr#   _internettldsr!   r   r'   r"   .)r   r   rK   )r   )r   )	eventTypemoduledatar8   r   debugr   r7   r   rA   r   r   __name__notifyListenersappendr   	getTargetmatches
validateIPlowerisDomain)r   event	eventNamesrcModuleName	eventDataiprecmyresZhostersrdatrJ   hemailevtr'   r   r   r   handleEventw   s    

















zsfp_securitytrails.handleEvent)r&   N)rU   
__module____qualname____doc__r   optdescsr   r8   r   dictr   r   r%   rA   ri   r   r   r   r   r      s"   
4r   )r:   r>   sflibr   r   r   r   r   r   r   r   <module>   s   