U
    @6^M                     @   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ZddiZdZdZe fdd	Z	d
d Z
dd Zdd Zdd Zdd Zdd ZdS )sfp_azureblobstoragezAzure Blob Finder:Footprint,Passive:Crawling and Scanning::Search for potential Azure blobs associated with the target and attempt to list their contents.ztest,dev,web,beta,bucket,space,files,content,data,prod,staging,production,stage,app,media,development,-test,-dev,-web,-beta,-bucket,-space,-files,-content,-data,-prod,-staging,-production,-stage,-app,-media,-development   )suffixes_maxthreadsr   zAList of suffixes to append to domains tried as blob storage namesNc                 C   sH   || _ |  | _|  | _t | _t| D ]}|| | j	|< q0d S )N)
sftempStorage	s3resultsresults	threadingLocklocklistkeysopts)selfsfcuserOptsopt r   ?/var/www/spiderfoot.crq.systems/modules/sfp_azureblobstorage.pysetup$   s    


zsfp_azureblobstorage.setupc                 C   s   ddgS )NDOMAIN_NAMELINKED_URL_EXTERNALr   r   r   r   r   watchedEvents.   s    z"sfp_azureblobstorage.watchedEventsc                 C   s   dgS )NCLOUD_STORAGE_BUCKETr   r   r   r   r   producedEvents4   s    z#sfp_azureblobstorage.producedEventsc              	   C   s<   | j j|dddd}|d r8| j d| j|< W 5 Q R X d S )N
   r   T)timeout	useragentnoLogcode)r	   fetchUrlr   r   )r   urlresr   r   r   	checkSite7   s    zsfp_azureblobstorage.checkSitec           	      C   s   t  }t | _d}d}g }|D ]T}|  r0 d S | jd|  |tjd| | j	|fd || 
  |d7 }q|rd}t D ]}|jdrd}q|sd}td qt| jS )	NTr   z!Spawning thread to check bucket: Zthread_sfp_azureblobstorages_)nametargetargs   Fg      ?)r   dictr   checkForStopr	   infoappendr   Threadr(   start	enumerater)   
startswithtimesleep)	r   siteListretrunningitsitefoundrtr   r   r   threadSites>   s0     
z sfp_azureblobstorage.threadSitesc                 C   s   d}t  }t  }|D ]l}|| jd krn| |}|d kr@|  S t | D ]}|| rL|| qLd}t  }|| |d7 }q|S )Nr   r   r,   )r   r   r?   r   r0   )r   Zsitesr:   r'   r7   r<   datar8   r   r   r   
batchSites^   s     


zsfp_azureblobstorage.batchSitesc                 C   s.  |j }|j}|j}|| jkr d S d| j|< | jd| d |  |dkr|d|krx| j|}td|| j|}| 	| d S |
dd| j|| jd	 g}t }|D ]R}	dg| jd
 d }
|
D ]2}|  r  d S |	| d }d| }|| qq| |}|D ] }td|| j|}| 	| qd S )NTzReceived event, z, from r   z.blob.core.windows.netr   . _internettldsr   ,zhttps://)	eventTypemoduler@   r   r	   debugurlFQDNr   __name__notifyListenersreplacedomainKeywordr   r   splitr.   r0   rA   )r   event	eventNamesrcModuleName	eventDatabevttargetsurlsr;   r   sr&   r8   r   r   r   handleEventu   s6    


 
z sfp_azureblobstorage.handleEvent)rJ   
__module____qualname____doc__r   optdescsr   r   r-   r   r   r   r(   r?   rA   rX   r   r   r   r   r      s     
 r   )r   r5   sflibr   r   r   r   r   r   r   r   <module>   s   