o
    �õ±j¢"  ã                   @   sü   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZmZ d dlmZ d dlmZ d d	l
mZmZmZmZmZmZmZmZmZmZ d d
lmZ G dd„ dƒZG dd„ dƒZ G dd„ dƒZ!dd„ Z"e#dkr|e"ƒ  dS dS )é    N)Úlogger)Ú
__banner__)ÚHTTPProxyAuthFailed)ÚSOCKS5AuthFailed)ÚSOCKSClientÚSocksTunnelError)ÚSocksCredential)ÚSocksQueueComms)
ÚSOCKS5MethodÚ
SOCKS5NegoÚSOCKS5NegoReplyÚSOCKS5RequestÚSOCKS5ReplyÚSOCKS5ReplyTypeÚSOCKS5CommandÚSOCKS5PlainAuthÚSOCKS5PlainAuthReplyÚSOCKS5ServerErrorReply)ÚSocksClientURLc                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚFileStringGenz
	Parses a file line by line
	c                 C   ó
   || _ d S ©N)Úfilename)Úselfr   © r   ú�/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/asysocks/examples/socksbrute.pyÚ__init__   ó   
zFileStringGen.__init__c              
   Ã   s®   �z?d}t | jdƒ�*}|D ]}| ¡ }|dkrq| |¡I d H  t d¡I d H  |d7 }qW d   ƒ n1 s7w   Y  |d fW S  tyV } z
||fW  Y d }~S d }~ww )Nr   ÚrÚ é   )Úopenr   ÚstripÚputÚasyncioÚsleepÚ	Exception)r   Útarget_qÚcntÚfÚlineÚer   r   r   Úrun   s$   €
ùÿ

€ÿzFileStringGen.runN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r,   r   r   r   r   r      s    r   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚListStringGenc                 C   r   r   )Útargets)r   r2   r   r   r   r   +   r   zListStringGen.__init__c              
   Ã   sr   �z!d}| j D ]}| |¡I d H  t d¡I d H  |d7 }q|d fW S  ty8 } z
||fW  Y d }~S d }~ww )Nr   r    )r2   r#   r$   r%   r&   )r   r'   r(   Útargetr+   r   r   r   r,   .   s   €


€ÿzListStringGen.runN)r-   r.   r/   r   r,   r   r   r   r   r1   *   s    r1   c                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )Ú
SOCKSBrutec                 C   sv   || _ g | _g | _i | _d| _d| _d| _d| _d| _d| _	d | _
d| _g | _d | _d | _d | _d| _d| _d| _d S )Néd   r    é   z8.8.8.8é5   Fr   )ÚproxiesÚ	user_gensÚpassword_gensÚ	passwordsÚ
worker_cntÚtimeoutÚmax_retriesÚretries_sleepÚverify_targetÚverify_portÚoutput_fileÚonly_positiveÚ_SOCKSBrute__worker_tasksÚ_SOCKSBrute__addr_queueÚ_SOCKSBrute__target_queueÚ_SOCKSBrute__result_queueÚ_SOCKSBrute__total_addrsÚ_SOCKSBrute__total_targetsÚ_SOCKSBrute__total_finished)r   r8   r   r   r   r   <   s&   
zSOCKSBrute.__init__c           	   
   Ã   s¬  �z³	 z“| j  ¡ I d H }|d u rW W d S |\}}}| j}|dkr„|| jkr.t | j¡I d H  |d8 }| ¡ I d H \}}|d urvt|tt	fƒrT| j
 ||dd f¡I d H  n0t|tƒret dt |j¡ ¡ nt|tƒrut dt |j¡ ¡ q| j
 ||dd f¡I d H  	 |dkr•| j
 ||d |f¡I d H  W n tjy¢   Y W d S  ty³ } zW Y d }~nd }~ww q tjy¿   Y d S  tyÕ } zt|ƒ W Y d }~d S d }~ww )NTr   r    FzTunnel setup failed!. Reason %sz5Failed socket connection to initial proxy!. Reason %s)rF   Úgetr>   r$   r%   r?   Úhandle_queueÚ
isinstancer   r   rG   r#   r   r   ÚdebugÚ	tracebackÚ	format_tbÚ__traceback__ÚOSErrorÚCancelledErrorr&   Úprint)	r   ÚtempÚusernameÚpasswordÚclientÚretriesÚ_Úerrr+   r   r   r   Ú	__scannerQ   sR   €



€€ÿÞ%€ÿzSOCKSBrute.__scannerc           
   
   Ã   s2  �z€	 | j  ¡ I d H }|d u rnd| j|< q	 | j ¡ I d H }|d u r$nV| jD ]Q}|  jd7  _tƒ }||_||_t 	| j
¡}d|d _d|d _| j|d _| j|d _||d _t ¡ }t ¡ }t||ƒ}t||ƒ}| j |||f¡I d H  q'q| j ¡  W d S  ty˜ }	 zt|	ƒ W Y d }	~	d S d }	~	ww )NTr    éÿÿÿÿ)Ú_SOCKSBrute__passwords_queuerK   r;   Ú_SOCKSBrute__users_queuerI   r   rV   rW   ÚcopyÚdeepcopyr8   Ú	only_openÚ	only_authr@   Úendpoint_iprA   Úendpoint_portÚ
credentialr$   ÚQueuer	   r   rF   r#   Ú$_SOCKSBrute__target_gen_finished_evtÚsetr&   rT   )
r   rW   Úuserrf   r8   Úin_qÚout_qÚcommsrX   r+   r   r   r   Ú__gen_targets~   sD   €
ü





è€ÿzSOCKSBrute.__gen_targetsc              
   Ã   óŠ   �z,| j D ]}| | j¡I d H \}}|d urt|ƒ |  j|7  _q| j d ¡I d H  W d S  tyD } zt|ƒ W Y d }~d S d }~ww r   )r9   r,   r_   rT   rH   r#   r&   )r   Úuser_genr(   r[   r+   r   r   r   Ú__gen_users¥   ó   €
€ÿzSOCKSBrute.__gen_usersc              
   Ã   ro   r   )r:   r,   r^   rT   rH   r#   r&   )r   Úpassword_genr(   r[   r+   r   r   r   Ú__gen_passwords²   rr   zSOCKSBrute.__gen_passwordsc           	   
   Ã   sô  �zât  ¡ | _t  ¡ | _t  | j¡| _t  ¡ | _t  ¡ | _t  	|  
¡ ¡ t  	|  ¡ ¡ t  	|  ¡ ¡ t| jƒD ]}| j t  	|  ¡ ¡¡ q7d }| jd urTt| jddd�}	 | j ¡ I d H \}}}}|  jd7  _|d urxt|ƒ tt |j¡ƒ |d u r§|d urŠ| d||f ¡ n+|du r˜td|||f ƒ n| jdu r¦td|||f ƒ n| jdu rµtd|||f ƒ | j ¡ du rÃ| j| jkrÃnqUtt| jƒƒD ]}| j d ¡I d H  qË| jD ]}| ¡  qÚW d	S  t yù } z
d|fW  Y d }~S d }~ww )
NÚwr   )ÚnewlineTr    z%s:%s
z%s:%s -> %sF)TN)!r$   rg   r^   r_   r<   rF   rG   ÚEventrh   Úcreate_taskÚ_SOCKSBrute__gen_passwordsÚ_SOCKSBrute__gen_usersÚ_SOCKSBrute__gen_targetsÚrangerD   ÚappendÚ_SOCKSBrute__scannerrB   r!   rK   rJ   rT   rO   rP   rQ   ÚwriterC   Úis_setrI   Úlenr#   Úcancelr&   )	r   rZ   ÚoutfilerV   rW   Ústatusr[   Úworkerr+   r   r   r   r,   ¿   sX   €





€
é

€ÿzSOCKSBrute.runN)	r-   r.   r/   r   r~   r{   rz   ry   r,   r   r   r   r   r4   ;   s    -'r4   c            
      C   s¦  dd l } | jdd�}|jddddd� |jd	d
ddd� |jddtd dd� |jddtddd� |jdddddd� |jddddd� |jdddd � |jd!dd"d� |jd#d$d%d&� | ¡ }|jd'u rhttƒ t 	d(¡ |j
dkryt 	tj¡ n
|j
d)krƒt 	d¡ t |j¡}t|ƒ}|j|_|j|_|j|_|j|_|jd u s§|jd u r­td*ƒ d S g }|jD ]}zt|d+ƒ}| ¡  |j t|ƒ¡ W q²   | |¡ Y q²t|ƒdkrà|j t |ƒ¡ t|jƒdkrítd,ƒ d S g }|jD ]}zt|d+ƒ}| ¡  |j! t|ƒ¡ W qò   | |¡ Y qòt|ƒdk�r!|j! t |ƒ¡ t|j!ƒdk�r/td-ƒ d S t" #| #¡ ¡\}}	|	d u�rEtd.|	 ƒ d S |jd'u �rQtd/ƒ d S d S )0Nr   zSOCKS5 proxy auth bruteforcer)Údescriptionz-uz--usersr}   z9User or users file with one user per line. can be stacked)ÚactionÚhelpz-pz--passwordszDPassword or password file with one password per line. can be stackedz-tz	--timeoutzBrute retries sleep time)ÚtypeÚdefaultrˆ   z-wz--worker-countr    ÚParallelismz-vz	--verboseÚcountÚ	Verbosity)r‡   rŠ   rˆ   z-sz--silentÚ
store_truezdont print bannerz-oz
--out-filezoutput file)rˆ   z
--positivezonly show sucsessful resultsr8   Ú*zµconnection string(s) of the target proxy. if you want a chain of proxies to use before reaching the target then supply the chain in order, the target must be the LAST OF THE URLS!!!)Únargsrˆ   Fr5   é   z2Users "-u" and Passwords "-p" must be set! Exitingr   z![-] No suitable users were found!z%[-] No suitable passwords were found!z%Failed to perform bruting! Reason: %szDone!)$ÚargparseÚArgumentParserÚadd_argumentÚintÚ
parse_argsÚsilentrT   r   r   ÚsetLevelÚverboseÚloggingÚDEBUGr   Ú	from_urlsr8   r4   r=   Úworker_countr<   Úout_filerB   ÚpositiverC   Úusersr;   r!   Úcloser9   r}   r   r�   r1   r:   r$   r,   )
r’   ÚparserÚargsÚ
proxychainÚbruteÚnotfiler3   r)   rZ   r[   r   r   r   Úmainó   sz   









ÿr§   Ú__main__)$r$   rš   rO   r`   Úasysocksr   Úasysocks._versionr   Úasysocks.protocol.httpr   Úasysocks.protocol.socks5r   Úasysocks.clientr   r   Úasysocks.common.credentialsr   Úasysocks.common.commsr	   r
   r   r   r   r   r   r   r   r   r   Úasysocks.common.clienturlr   r   r1   r4   r§   r-   r   r   r   r   Ú<module>   s*   0 9P
ÿ