U
    @6^                     @   sP   d dl Z d dlZd dlZ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                   @   s^   e Zd ZdZddddZddddZdZd	Ze fd
dZ	dd Z
dd Zdd Zdd ZdS )sfp_darksearchz~Darksearch:Footprint,Investigate:Search Engines::Search the Darksearch.io Tor search engine for mentions of the target domain.T   )
fetchlinks	max_pages	fullnameszQFetch the darknet pages (via TOR, if enabled) to verify they mention your target.z,Maximum number of pages of results to fetch.zSearch for human names?NFc                 C   s@   || _ d| _|  | _d| _t| D ]}|| | j|< q(d S )NZ
DarksearchF)sf__dataSource__tempStorageresults
errorStatelistkeysopts)selfsfcuserOptsopt r   9/var/www/spiderfoot.crq.systems/modules/sfp_darksearch.pysetup'   s    
zsfp_darksearch.setupc                 C   s
   dddgS )NDOMAIN_NAME
HUMAN_NAME	EMAILADDRr   r   r   r   r   watchedEvents0   s    zsfp_darksearch.watchedEventsc                 C   s
   dddgS )NDARKNET_MENTION_URLDARKNET_MENTION_CONTENTSEARCH_ENGINE_WEB_CONTENTr   r   r   r   r   producedEvents3   s    zsfp_darksearch.producedEventsc              
   C   s   d| djddd d t|d}| jjdtj| | jd | jd	 d
}t	
d |d d krjd S zt|d }W n< tk
r } z| jdt|  W Y d S d }~X Y nX |S )N"raw_unicode_escapeasciireplace)errors)querypagez!https://darksearch.io/api/search?
_useragent_fetchtimeout)	useragenttimeout   contentz Error processing JSON response: )encodedecodestrr
   fetchUrlurllibparse	urlencoder   timesleepjsonloadsBaseExceptiondebug)r   qryr(   paramsresdataer   r   r   r'   7   s     
zsfp_darksearch.queryc                 C   s  |j }|j}|j}| jd s(|dkr(d S || jkr6d S d| j|< | jd| d |  d}| jd }||kr|  r|d S | ||}|d krd S |d7 }|	d}|d krd	}||k r|}|	d
}	|	d krd S |	D ]2}
|
d krqt
dt|
| j|}| | |
	d}|d krq|| jkr*q| j|ds@qd| j|< | jd|  | jd r~| jj|| jd | jd d}|d d kr| jd| d  q||d kr| jd| d |  qt
d|| j|}| | z&|d |d }|t| d }W n8 tk
rH } z| jd W Y qW 5 d }~X Y nX |d || }	t
dd|	 d | j|}| | qt
d|| j|}| | |
	dd kr|
	dd kr| jd| d |  qt
dd|
	d d  d |
	d d | j|}| | qqfd S )!Nr	   r   TzReceived event, z, from    r   	last_pager   r?   r    linkz.onionzFound a darknet mention: r   r*   r)   )r,   r+   r.   z	Ignoring z as no data returnedz as no mention of r   x      zString not found in content.r   z...titledescriptionzTitle: z

)	eventTypemoduler?   r   r   r
   r;   checkForStopr'   getr   r1   __name__notifyListenersurlFQDNendswithr2   indexlenr:   )r   event	eventNamesrcModuleName	eventDatar(   pagesr>   rB   r?   resultevtrC   
startIndexendIndexr@   r   r   r   handleEventO   s    













 zsfp_darksearch.handleEvent)rL   
__module____qualname____doc__r   optdescsr   r   dictr   r   r!   r'   r[   r   r   r   r   r      s    	r   )r8   r6   urllib.requestr3   urllib.parseurllib.errorsflibr   r   r   r   r   r   r   r   <module>   s   