U
    @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:   || _ |  | _d| _t| D ]}|| | j|< q"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dd d| _d S |	d d kr| jd|  d S zt|	d }|dkr|	dd W S |	dddkr^t
|	dg dkrDtd |r$||	d n
|	d}| |||d |W S ||	dg  |W S n|	dg W S W n@ tk
r }
 z | jdt|
 d W Y d S d }
~
X Y 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   strr   fetchUrlerror
errorStateinfojsonloadsgetlentimesleepextendquery	Exception)r   qry	querytypepageaccumr:   r,   urlrequestreser   r   r   rB   C   sN      

zsfp_securitytrails.queryc                 C   s  |j }|j}|j}| jrd S | jd| d |  | jd dkrZ| jdd d| _d S || jkr|| jd| d	  d S d| j|< |d
kr|}| 	|d}t
 }t
 }|d k	r|D ] }	d|	kr|	d D ]2}
|
|krqtd|
| j|}| | ||
 qd|	kr| j| jd kr"q|	d }| jd s`|  j|ddr`| jd| d  q||kr||kr| jd r| j||s| jd| d  q||  td|| j|}| | |  jd7  _q|dkr|}| 	|d}t
 }|d k	r|D ]}	d|	kr|	d s(q|	d }| |kr||  nqtd|| j|}| | | j|| jd rtd|| j|}| | q|dkr|}| 	|d}t
 }|d k	r|D ]J}| |kr||  nqtd|d  | | j|}| | qd 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#   .)	eventTypemoduledatar9   r   debugr   r8   r   rB   r   r   __name__notifyListenersappendr   	getTargetmatches
validateIPlowerisDomain)r   event	eventNamesrcModuleName	eventDataiprecmyresZhostersrdatrK   hemailevtr(   r   r   r   handleEventw   s    





 








 zsfp_securitytrails.handleEvent)r'   N)rV   
__module____qualname____doc__r   optdescsr   r9   r   dictr   r    r&   rB   rj   r   r   r   r   r      s&   	

4r   )r;   r?   sflibr   r   r   r   r   r   r   r   <module>   s   