
    EOiˤ                         S r SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKJ	r	  SSK
Jr  SrSrSr " S S	\5      rS
 r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rS rSS jrg)a  
nmap.py - version and date, see below

Source code : https://bitbucket.org/xael/python-nmap

Author :

* Alexandre Norman - norman at xael.org

Contributors:

* Steve 'Ashcrow' Milner - steve at gnulinux.net
* Brian Bustin - brian at bustin.us
* old.schepperhand
* Johan Lundberg
* Thomas D. maaaaz
* Robert Bost
* David Peltier
* Ed Jones

Licence: GPL v3 or any later version for python-nmap


This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.


**************
IMPORTANT NOTE
**************

The Nmap Security Scanner used by python-nmap is distributed
under it's own licence that you can find at https://svn.nmap.org/nmap/COPYING

Any redistribution of python-nmap along with the Nmap Security Scanner
must conform to the Nmap Security Scanner licence

    N)Process)ElementTreez"Alexandre Norman (norman@xael.org)z0.7.1z
2021.10.26c                       \ rS rSrSr SS jrS rS rSS jr SS jr	    SS	 jr
S
 rS rS rS rS rS rS rSrg)PortScannerG   z3
PortScanner class allows to use nmap from python

c                    SU l         0 U l        SU l        SU l        SU l        SnSU l        [        R                  " S5      nU H  n [        R                  R                  S5      (       dH  [        R                  R                  S5      (       d$  [        R                  R                  S5      (       a(  [        R                  " US	/S
[        R                  SS9nO&[        R                  " US	/S
[        R                  S9nX@l           O$   [        S[         R"                  " S5       35      e[$        R'                  UR)                  5       S   5      U l        U R                  R+                  [         R,                  5       H  nUR/                  U5      c  M  Sn[        R                  " S5      n[        R                  " S5      nUR1                  U5      n	UR1                  U5      n
U	bb  U
b_  [3        XiR5                  5       U	R7                  5        5      U l        [3        XjR5                  5       S-   U
R7                  5        5      U l          O   U(       d  [        S5      eg! [         a     GM/  f = f)aM  
Initialize PortScanner module

* detects nmap on the system and nmap version
* may raise PortScannerError exception if nmap is not found in the path

:param nmap_search_path: tupple of string where to search for nmap executable.
                         Change this if you want to use a specific version of nmap.
:returns: nothing

 r   FNz4Nmap version [0-9]*\.[0-9]*[^ ]* \( http(|s)://.* \)freebsdlinuxdarwinz-Vi'  T)bufsizestdout	close_fds)r   r   z.nmap program was not found in path. PATH is : PATHz[0-9]+z\.[0-9]+   z"nmap program was not found in path)
_nmap_path_scan_result_nmap_version_number_nmap_subversion_number_nmap_last_output_PortScanner__processrecompilesysplatform
startswith
subprocessPopenPIPEOSErrorPortScannerErrorosgetenvbytesdecodecommunicatesplitlinesepmatchsearchintstartend)selfnmap_search_pathis_nmap_foundregex	nmap_pathplineregex_versionregex_subversionrvrsvs              MC:\Users\marco\AppData\Local\Temp\pip-target-mkzta7wy\lib\python\nmap/nmap.py__init__PortScanner.__init__M   s   * $%!'($!# 

RS *ILL++I66||..w77||..x88"(("D) %)"&	A #(("D)5A #,- *0 #@6AR@ST  "'ammoa.@!A**00<D{{4 , $ "

8 4#%::k#: "))$/&--d3>co03Dbffh4O0PD-36YY[1_swwy94D0 ! =$ "#GHHA  s   	B:I&&
I54I5c                     U R                   $ )z
Returns the last text output of nmap in raw text
this may be used for debugging purpose

:returns: string containing the last text output of nmap in raw text
)r   r.   s    r9   get_nmap_last_output PortScanner.get_nmap_last_output   s     %%%    c                 2    U R                   U R                  4$ )z
returns nmap version if detected (int version, int subversion)
or (0, 0) if unknown
:returns: (nmap_version_number, nmap_subversion_number)
)r   r   r=   s    r9   nmap_versionPortScanner.nmap_version   s     ))4+G+GHHr@   c                 ,   [        U5      [        L d   S[        U5       S35       eU R                  USS9nSUS   ;   aF  SUS   S   ;   a:  [        US   S   S   5      S:  a"  S	US   S   S   S   ;   a  U R                  US
S9  U R	                  5       $ )zB
do not scan but interpret target hosts and return a list a hosts
0Wrong type for [hosts], should be a string [was ]z-sL)	argumentsscaninfonmaperrorr   z'looks like an IPv6 target specificationz-sL -6)typestrscanlen	all_hosts)r.   hostsoutputs      r9   listscanPortScanner.listscan   s    
 K3	M=d5k]!L	M5E2 &.(6&>*55F6N:.w781<9f~j)'2156 IIexI0~~r@   Nc                 6   [         R                  S   S:X  a  [        U5      [        4;   d   S[        U5       S35       e[        U5      [        [        S5      4;   d   S[        U5       S35       e[        U5      [        4;   d   S[        U5       S35       eO~[        U5      [        L d   S[        U5       S35       e[        U5      [        [        S5      4;   d   S[        U5       S35       e[        U5      [        L d   S[        U5       S35       eS H  nXc;  a  M
   S	5       e   [        R
                  " U5      n[        R
                  " U5      nU R                  S
S/U-   SU/USL-  -   U-   n	U(       a  S/U	-   n	[        R                  " U	S[        R                  [        R                  [        R                  S9n
US:X  a  U
R                  5       u  U l        nO U
R                  US9u  U l        n[        R!                  U5      n/ n/ n[#        U5      S:  a  [$        R&                  " S[$        R(                  5      nUR                  [*        R,                  5       H]  n[#        U5      S:  d  M  UR/                  U5      nUb$  UR1                  U[*        R,                  -   5        ML  UR1                  U5        M_     U R3                  U R                  UUUS9$ ! [        R                   a    U
R                  5         [        S5      ef = f)a  
Scan given hosts

May raise PortScannerError exception if nmap output was not xml

Test existance of the following key to know
if something went wrong : ['nmap']['scaninfo']['error']
If not present, everything was ok.

:param hosts: string for hosts as nmap use it 'scanme.nmap.org' or '198.116.0-255.1-127' or '216.163.128.20/20'
:param ports: string for ports as nmap use it '22,53,110,143-4564'
:param arguments: string of arguments for nmap '-sU -sX -sC'
:param sudo: launch nmap with sudo if True
:param timeout: int, if > zero, will terminate scan after seconds, otherwise will wait indefintely

:returns: scan_result as dictionnary
r      rE   rF   N0Wrong type for [ports], should be a string [was 4Wrong type for [arguments], should be a string [was -oXz-oAvXml output can't be redirected from command line.
You can access it after a scan using:
nmap.nm.get_nmap_last_output()rY   -z-psudoi )r   stdinr   stderr)timeoutzTimeout from nmap processz^Warning: .*)nmap_xml_outputnmap_errnmap_err_keep_tracenmap_warn_keep_trace)r   version_inforK   rL   shlexr'   r   r   r   r   r&   r   TimeoutExpiredkillPortScannerTimeoutr$   r%   rN   r   r   
IGNORECASEr"   r(   r*   appendanalyse_nmap_xml_scan)r.   rP   portsrG   r\   r_   redirecting_outputh_argsf_argsargsr3   ra   rb   rc   regex_warningr4   rgws                    r9   rM   PortScanner.scan   sM   ( A!#;#  QA$u+aPQ  ;T
#  Q B$u+aPQ  	?'  YEd9oEVVWXY 
 Us"QA$u+aPQ";T
#  Q B$u+aPQ 
 Y3&YEd9oEVVWXY& #1"3J JJ3 #1
 U#Y' __eS)UmuD012  	 8d?D//????
 a<12.T#XF56]]7]5S2'
 <<) !!x=1JJ~r}}EM rzz2t9q='..t4C,33D2::4EF+228< 3 )) 22 3!5	 * 
 	
7 ,, F()DEEFs   (K( (0Lc                    Ub  Xl         0 n [        R                  " U R                   5      nUR                  S5      0 UR                  S5      R                  S5      UR                  S5      R                  S5      UR                  S5      R                  S5      UR                  S5      R                  S	5      UR                  S5      R                  S
5      S.S.US'   [	        U5      S:  a
  X5S   S   S'   [	        U5      S:  a
  XES   S   S'   UR                  S5       H>  nUR                  S5      UR                  S5      S.US   S   UR                  S5      '   M@     0 US'   UR                  S5       GH  nSn	0 n
0 nUR                  S5       He  nUR                  S5      nUR                  S5      X'   US:X  a  X   n	M3  US:X  d  M;  UR                  S5      c  MO  UR                  S5      XU   '   Mg     U	c   UR                  S5      R                  S5      n	/ n[	        UR                  S5      5      S:  aK  UR                  S5       H5  nUR                  UR                  S5      UR                  S5      S.5        M7     OUR                  S S S.5        [        S!U05      US   U	'   XS   U	   S"'   XS   U	   S'   UR                  S#5       H/  nUR                  S$5      UR                  S%5      S&.US   U	   S#'   M1     UR                  S'5       H/  nUR                  S(5      UR                  S)5      S*.US   U	   S''   M1     UR                  S+5       GH5  nUR                  S5      n[        UR                  S,5      5      nUR                  S$5      R                  S$5      nUR                  S$5      R                  S%5      nS =n=n=n=n=nnUR                  S-5       H  nUR                  S5      nUR                  S.5      (       a  UR                  S.5      nUR                  S/5      (       a  UR                  S/5      nUR                  S05      (       a  UR                  S05      nUR                  S15      (       a  UR                  S15      nUR                  S25       H  nUR                  nM     M     U[        US   U	   R                  5       5      ;  a  0 US   U	   U'   UUUUUUUUS3.US   U	   U   U'   S nS nUR                  S45       Hs  n U R                  S55      nU R                  S65      nS4[        US   U	   U   U   R                  5       5      ;  a  0 US   U	   U   U   S4'   UUS   U	   U   U   S4   U'   Mu     GM8     UR                  S75       H  n!U!R                  S45       Hp  nUR                  S55      n"UR                  S65      n#S7[        US   U	   R                  5       5      ;  a  / US   U	   S7'   US   U	   S7   R                  U"U#S8.5        Mr     M     UR                  S95       GH  n$/ n%/ n&U$R                  S:5       HK  n'U'R                  S$5      nU'R                  S;5      nU'R                  S,5      n(U&R                  UUU(S<.5        MM     U&US   U	   S:'   U$R                  S=5       GH	  n)U)R                  S5      nU)R                  S>5      n*U)R                  S?5      n+/ n,U)R                  S@5       H  n-U-R                  S5      n.U-R                  S5      n/U-R                  SA5      n0U-R                  SB5      n1U-R                  S>5      n*/ nU-R                  S25       H  nUR                  UR                  5        M      U,R                  U.U/U0U1U*USC.5        M     U%R                  UU*U+U,SD.5        GM     U%US   U	   S='   GM     UR                  SE5       H  nUR                  SF5      n2U2US   U	   SF'   M!     GM     XPl        U$ ! [         a0    [	        U5      S:  a  [        U5      e[        U R                   5      ef = f)Ga;  
Analyses NMAP xml scan ouput

May raise PortScannerError exception if nmap output was not xml

Test existance of the following key to know if something went wrong : ['nmap']['scaninfo']['error']
If not present, everything was ok.

:param nmap_xml_output: xml string to analyse
:returns: scan_result as dictionnary
Nr   rp   zrunstats/finishedtimestrelapsedzrunstats/hostsupdowntotal)ru   rv   uphosts	downhosts
totalhosts)command_linerH   	scanstatsrI   rH   rJ   warningrK   services)methodr   protocolrM   hostaddressaddrtypeaddripv4macvendorzhostnames/hostnamename)r   rK   r	   	hostnames	addressesstatusstatereason)r   r   uptimesecondslastboot)r   r   z
ports/portportidserviceproductversion	extrainfoconfcpe)r   r   r   r   r   r   r   r   scriptidrQ   
hostscript)r   rQ   r"   portusedproto)r   r   r   osmatchaccuracyr4   osclassosfamilyosgen)rK   r   r   r   r   r   )r   r   r4   r   osfingerprintfingerprint)r   ET
fromstring	ExceptionrN   r!   getfindfindallrj   PortScannerHostDictr+   textlistkeysr   )3r.   r`   ra   rb   rc   scan_resultdomdscidhostr   address_blockvendor_blockr   addtyper   	dhostnamedstatusdportr   portr   r   r   r   r   r   r   r   dnamedcpe	script_id
script_outdscriptdhostscripthsidhsoutputdosr   r   	dportusedr   dosmatchr   r4   r   dosclassostyper   r   r   r   s3                                                      r9   rk   !PortScanner.analyse_nmap_xml_scan9  sn   d &%4"	?-- 6 67C  GGFO88$78<<YG88$78<<YG88$4599$? XX&67;;FC!hh'78<<WE

F "#a'7J
+G4 #$q(9M
+I6 KK
+D((6* HHZ0EK
+DHHZ,@A , !F[[(EDML ==3!++j1)0V)<&f$(1D%'++h*?*K;B;;x;PLw!78 4 |zz),008I5==!567!;!&/C!DI$$!*v!6	f@UV "E
   "b!9:(;[)<T(UK%5B%k22>%h/ ==2 %[[1%kk(37F#D)(3 3 !==2  '{{95 'J 77F#D)(3 3 |4		*-599X./

7+//8G,00:DFFFwFF9Ftc"]]95E 99V,Dyy++"'))I"6yy++"'))I"6yy--$)IIk$:	yy(($yy0 %e 4"ii !5 6 [%8%>%C%C%E FF79K'-e4 #$ &&!* 	:F#D)%06 	
$}}X6G 'D 1I!(X!6Jt#F+D1%8>CCE(  LNF+D1%8>xH #  '-e4T:8D!  7S 5t  %}}\:(00:E 99T?D$yy2H#4F0CD0I0N0N0P+QQBDF+D1,?'-l;BB#x8 ;  ;( }}T*!$Z!8I%MM'2E%MM'2E&]]84FOOeev$VW "9 9AF#D)*5 #I 6H#<</D'||J7H#<</D G$,$4$4Y$?!)f!5!)h!7#+<<
#; (W 5#+<<
#; $,$4$4U$;DJJtyy1 %<  (.*0,4).,4'*	 %@. NN$((0$('.	= !7N <CK'-i8g +j 7#ii6;FF#D)-8	 8[ )f (q  	?8}q &x00&t'='=>>		?s    `- -:a'c                     [         R                  S   S:X  a)  [        U5      [        4;   d   S[        U5       S35       eO&[        U5      [        L d   S[        U5       S35       eU R                  S   U   $ )z
returns a host detail
r   rU   /Wrong type for [host], should be a string [was rF   rM   )r   rd   rK   rL   r   r.   r   s     r9   __getitem__PortScanner.__getitem__L  s     A!#:"  O@dANO 
 T
c!O@dANO!  (..r@   c                     S[        U R                  R                  5       5      ;  a  / $ [        U R                  S   R                  5       5      nUR                  5         U$ )z$
returns a sorted list of all hosts
rM   )r   r   r   sort)r.   lisths     r9   rO   PortScanner.all_hostsZ  sQ     d//44677IT&&v.3356

r@   c                     SU R                   ;   d   S5       eSU R                   S   ;   d   S5       eU R                   S   S   $ )zf
returns command line used for the scan

may raise AssertionError exception if called before scanning
rI   'Do a scan before trying to get result !r}   r   r=   s    r9   r}   PortScanner.command_lined  sY     ***U,UU*d//77	54	57   (88r@   c                     SU R                   ;   d   S5       eSU R                   S   ;   d   S5       eU R                   S   S   $ )z
returns scaninfo structure
{'tcp': {'services': '22', 'method': 'connect'}}

may raise AssertionError exception if called before scanning
rI   r   rH   r   r=   s    r9   rH   PortScanner.scaninfoq  sY     ***U,UU*$++F33	54	53   (44r@   c                     SU R                   ;   d   S5       eSU R                   S   ;   d   S5       eU R                   S   S   $ )z
returns scanstats structure
{'uphosts': '3', 'timestr': 'Thu Jun  3 21:45:07 2010', 'downhosts': '253', 'totalhosts': '256', 'elapsed': '5.79'}  # NOQA: E501

may raise AssertionError exception if called before scanning
rI   r   r~   r   r=   s    r9   r~   PortScanner.scanstats  sY     ***U,UU*4,,V44	54	54   (55r@   c                     [        U5      [        L d   S[        U5       S35       eSU R                  ;   d   S5       eU[        U R                  S   R	                  5       5      ;   a  gg)z2
returns True if host has result, False otherwise
r   rF   rM   r   TF)rK   rL   r   r   r   r   s     r9   has_hostPortScanner.has_host  sp    
 J#	K<T$ZLJ	K***U,UU*4))&166899r@   c                 $   SU R                   ;   d   S5       e[        R                  S:  a  [        R                  " 5       nO[        R
                  " 5       n[        R                  " USS9n/ SQnUR                  U5        U R                  5        H  nX   R                  5        H  nUS;  a  M  [        X   U   R                  5       5      nUR                  5         U H  nSnX   S	    H  n	U	S
   nU	S   n
UUU
UUX   U   U   S
   X   U   U   S   X   U   U   S   X   U   U   S   X   U   U   S   X   U   U   S   X   U   U   S   X   U   U   S   /nUR                  U5        M     M     M     M     UR                  5       $ )ak  
returns CSV output as text

Example :
host;hostname;hostname_type;protocol;port;name;state;product;extrainfo;reason;version;conf;cpe
127.0.0.1;localhost;PTR;tcp;22;ssh;open;OpenSSH;protocol 2.0;syn-ack;5.9p1 Debian 5ubuntu1;10;cpe
127.0.0.1;localhost;PTR;tcp;23;telnet;closed;;;conn-refused;;3;
127.0.0.1;localhost;PTR;tcp;24;priv-mail;closed;;;conn-refused;;3;
rM   r   )   r   ;)	delimiter)r   hostnamehostname_typer   r   r   r   r   r   r   r   r   r   )tcpudpr	   r   r   rK   r   r   r   r   r   r   r   )r   r   rd   ioBytesIOStringIOcsvwriterwriterowrO   all_protocolsr   r   r   getvalue)r.   fd	csv_ouput
csv_headerr   r   lportr   r   hr   csv_rows               r9   r   PortScanner.csv  s    ***U,UU*f$BBJJrS1	

  	:&NN$D113.TZ.3356

!D!H!Z4#$V9()&	 $)!  Ju-d3F; Ju-d3G< Ju-d3I> Ju-d3K@ Ju-d3H= Ju-d3I> Ju-d3F; Ju-d3E:# "**73% 5 " 4 %8 {{}r@   )	__processr   r   r   r   r   ))rI   z/usr/bin/nmapz/usr/local/bin/nmapz/sw/bin/nmapz/opt/local/bin/nmap)	127.0.0.1r   N-sVFr   )Nr	   r	   r	   )__name__
__module____qualname____firstlineno____doc__r:   r>   rB   rR   rM   rk   r   rO   r}   rH   r~   r   r   __static_attributes__ r@   r9   r   r   G   sj    
Un&I * STn
d Qf/956@r@   r   c                     U R                   R                  U5       H.  n U R                   R                  XrX5U5      nUc  M&  U" Xx5        M0     g! [         a    Sn N f = f)z'
Used by PortScannerAsync for callback
N)_nmrR   rM   r!   )	r.   rP   rl   rG   callbackr\   r_   r   	scan_datas	            r9   __scan_progressive__r    sc     !!%(	d9GLI T% )    	I	s   AAAc                   P    \ rS rSrSrS rS r      SS jrS rSS jr	S	 r
S
rg)PortScannerAsynci  z
PortScannerAsync allows to use nmap from python asynchronously
for each host scanned, callback is called with scan result for the host

c                 0    SU l         [        5       U l        gz
Initialize the module

* detects nmap on the system and nmap version
* may raise PortScannerError exception if nmap is not found in the path

N)_processr   r   r=   s    r9   r:   PortScannerAsync.__init__  s     =r@   c                     U R                   b:   U R                   R                  5       (       a  U R                   R                  5         SU l         g! [         a     Nf = f)z
Cleanup when deleted

N)r  is_alive	terminateAssertionErrorr=   s    r9   __del__PortScannerAsync.__del__  sX    
 ==$==))++MM++-  "  s   9A 
AANc           
      0   [         R                  S   S:X  a  [        U5      [        4;   d   S[        U5       S35       e[        U5      [        [        S5      4;   d   S[        U5       S35       e[        U5      [        4;   d   S[        U5       S35       eO~[        U5      [        L d   S[        U5       S35       e[        U5      [        [        S5      4;   d   S[        U5       S35       e[        U5      [        L d   S[        U5       S35       e[	        U5      (       d  Ub   S[        U5       S	35       eS
 H  nXs;  a  M
   S5       e   [        [        XX#XEU4S9U l        SU R                  l        U R                  R                  5         g)a  
Scan given hosts in a separate process and return host by host result using callback function

PortScannerError exception from standard nmap is catched and you won't know about but get None as scan_data

:param hosts: string for hosts as nmap use it 'scanme.nmap.org' or '198.116.0-255.1-127' or '216.163.128.20/20'
:param ports: string for ports as nmap use it '22,53,110,143-4564'
:param arguments: string of arguments for nmap '-sU -sX -sC'
:param callback: callback function which takes (host, scan_data) as arguments
:param sudo: launch nmap with sudo if true
:param timeout: int, if > zero, will terminate scan after seconds, otherwise will wait indefintely

r   rU   rE   rF   NrV   rW   zThe [callback] z should be callable or None.rX   rZ   )targetrp   T)
r   rd   rK   rL   callabler   r  r  daemonr,   )r.   rP   rl   rG   r  r\   r_   rm   s           r9   rM   PortScannerAsync.scan  s   . A!#;#  QA$u+aPQ  ;T
#  Q B$u+aPQ  	?'  YEd9oEVVWXY 
 Us"QA$u+aPQ";T
#  Q B$u+aPQ 
 Y3&YEd9oEVVWXY& X("2	IS]O+GH	I2 #1"3J JJ3 #1
  'uI
  $r@   c                 R    U R                   b  U R                   R                  5         g)z 
Stop the current scan process

N)r  r  r=   s    r9   stopPortScannerAsync.stopX  s!    
 ==$MM##%r@   c                     [        U5      [        [        S5      4;   d   S[        U5       S35       eU R                  R                  U5        g)zp
Wait for the current scan process to finish, or timeout

:param timeout: default = None, wait timeout seconds

Nz8Wrong type for [timeout], should be an int or None [was rF   )rK   r+   r  joinr.   r_   s     r9   waitPortScannerAsync.waita  s[     G}J!
 
 	W Fd7m_TUV	W 

 	7#r@   c                 X     U R                   R                  5       $ ! [         a     gf = f)zA
:returns: True if a scan is currently running, False otherwise

F)r  r  r   r=   s    r9   still_scanningPortScannerAsync.still_scanningp  s,    
	==))++ 		s    
)))r   r  )r   Nr   NFr   N)r   r   r   r   r   r:   r  rM   r  r  r  r   r   r@   r9   r  r    s:    
& =~r@   r  c                   @    \ rS rSrSrS r S
S jrS rSS jrS r	S	r
g)PortScannerYieldi~  z
PortScannerYield allows to use nmap from python with a generator
for each host scanned, yield is called with scan result for the host

c                 .    [         R                  U 5        gr  )r  r:   r=   s    r9   r:   PortScannerYield.__init__  s     	!!$'r@   Nc              #     #    [        U5      [        L d   S[        U5       S35       e[        U5      [        [        S5      4;   d   S[        U5       S35       e[        U5      [        L d   S[        U5       S35       eS H  nXc;  a  M
   S5       e   U R                  R                  U5       H&  n U R                  R	                  XrX4U5      nXx4v   M(     g! [
         a    Sn Nf = f7f)a  
Scan given hosts in a separate process and return host by host result using callback function

PortScannerError exception from standard nmap is catched and you won't know about it

:param hosts: string for hosts as nmap use it 'scanme.nmap.org' or '198.116.0-255.1-127' or '216.163.128.20/20'
:param ports: string for ports as nmap use it '22,53,110,143-4564'
:param arguments: string of arguments for nmap '-sU -sX -sC'
:param callback: callback function which takes (host, scan_data) as arguments
:param sudo: launch nmap with sudo if true
:param timeout: int, if > zero, will terminate scan after seconds, otherwise will wait indefintely

rE   rF   NrV   rW   rX   rZ   )rK   rL   r   rR   rM   r!   )	r.   rP   rl   rG   r\   r_   rm   r   r  s	            r9   rM   PortScannerYield.scan  s"    $ K3	M=d5k]!L	ME{J
 
 	M >d5k]!L	M 

 Os"	UA$y/ARRST	U" #1"3J JJ3 #1
 HH%%e,D! HHMM$yP	 ## - 	 $ ! 	!s0   BC.'C.5C
C.C+(C.*C++C.c                     g r   r   r=   s    r9   r  PortScannerYield.stop      r@   c                     g r   r   r  s     r9   r  PortScannerYield.wait  r)  r@   c                     g r   r   r=   s    r9   r  PortScannerYield.still_scanning  r)  r@   r   r   r   )r   r   r   r   r   r:   rM   r  r  r  r   r   r@   r9   r"  r"  ~  s(    	 ST'Rr@   r"  c                   ~    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rS rS rSrg)r   i  zG
Special dictionnary class for storing and accessing host scan result

c                     U S   $ )z
:returns: list of hostnames

r   r   r=   s    r9   r   PortScannerHostDict.hostnames  s    
 K  r@   c                     SnU S    H  nUS   S:X  d  M  US   s  $    [        U S   5      S:  a  SU S   S   ;   a  U S   S   S   $ g)zs
For compatibility purpose...
:returns: try to return the user record or the first hostname of the list hostnames

r	   r   rK   userr   r   )rN   )r.   r   r   s      r9   r   PortScannerHostDict.hostname  sk     k"AyF"y  # 4$%)f[8I!8L.LK(+F33r@   c                     U S   S   $ )
:returns: host state

r   r   r   r=   s    r9   r   PortScannerHostDict.state  s    
 H~g&&r@   c                     U S   $ )r5  r   r   r=   s    r9   r   PortScannerHostDict.uptime  s    
 H~r@   c           	          S n[        [        U[        U R                  5       5      5      5      nUR                  5         U$ )z,
:returns: a list of all scanned protocols

c                     U S;   $ )N)ipr   r   sctpr   )xs    r9   _proto_filter8PortScannerHostDict.all_protocols.<locals>._proto_filter  s    444r@   )r   filterr   r   )r.   r>  lps      r9   r   !PortScannerHostDict.all_protocols  s3    	5 &TYY[(9:;
		r@   c                     S[        U R                  5       5      ;   a.  [        U S   R                  5       5      nUR                  5         U$ / $ )z
:returns: list of tcp ports

r   r   r   r   )r.   ltcps     r9   all_tcpPortScannerHostDict.all_tcp   A    
 D%%U((*+DIIKK	r@   c                     [        U5      [        L d   S[        U5       S35       eS[        U R                  5       5      ;   a!  U[        U S   R                  5       5      ;   a  gg)zS
:param port: (int) tcp port
:returns: True if tcp port has info, False otherwise

-Wrong type for [port], should be an int [was rF   r   TFrK   r+   r   r   r.   r   s     r9   has_tcpPortScannerHostDict.has_tcp  sc     J#	I:4:,aH	I D%%$$tE{7G7G7I2J*Jr@   c                 ^    [        U5      [        L d   S[        U5       S35       eU S   U   $ )z:
:param port: (int) tcp port
:returns: info for tpc port

rJ  rF   r   rK   r+   rL  s     r9   r   PortScannerHostDict.tcp  s>     J#	I:4:,aH	IE{4  r@   c                     S[        U R                  5       5      ;   a.  [        U S   R                  5       5      nUR                  5         U$ / $ )z
:returns: list of udp ports

r   rD  )r.   ludps     r9   all_udpPortScannerHostDict.all_udp$  rH  r@   c                     [        U5      [        L d   S[        U5       S35       eS[        U R                  5       5      ;   a!  S[        U S   R                  5       5      ;   a  gg)zS
:param port: (int) udp port
:returns: True if udp port has info, False otherwise

rJ  rF   r   r   TFrK  rL  s     r9   has_udpPortScannerHostDict.has_udp/  sc     J#	I:4:,aH	I D%%&De9I9I9K4L*Lr@   c                 ^    [        U5      [        L d   S[        U5       S35       eU S   U   $ )z:
:param port: (int) udp port
:returns: info for udp port

rJ  rF   r   rP  rL  s     r9   r   PortScannerHostDict.udp=  s@     J#	I:4:,aH	I E{4  r@   c                     S[        U R                  5       5      ;   a.  [        U S   R                  5       5      nUR                  5         U$ / $ )z
:returns: list of ip ports

r;  rD  )r.   lips     r9   all_ipPortScannerHostDict.all_ipI  s?    
 4		$$tDz()CHHJJ	r@   c                     [        U5      [        L d   S[        U5       S35       eS[        U R                  5       5      ;   a!  U[        U S   R                  5       5      ;   a  gg)zQ
:param port: (int) ip port
:returns: True if ip port has info, False otherwise

rJ  rF   r;  TFrK  rL  s     r9   has_ipPortScannerHostDict.has_ipT  sa     J#	I:4:,aH	I 4		$$d4joo6G1H)Hr@   c                 ^    [        U5      [        L d   S[        U5       S35       eU S   U   $ )z8
:param port: (int) ip port
:returns: info for ip port

rJ  rF   r;  rP  rL  s     r9   r;  PortScannerHostDict.ipb  s@     J#	I:4:,aH	I Dz$r@   c                     S[        U R                  5       5      ;   a.  [        U S   R                  5       5      nUR                  5         U$ / $ )z
:returns: list of sctp ports

r<  rD  )r.   lsctps     r9   all_sctpPortScannerHostDict.all_sctpn  sA    
 T$))+&&f**,-EJJLL	r@   c                     [        U5      [        L d   S[        U5       S35       eS[        U R                  5       5      ;   a!  U[        U S   R                  5       5      ;   a  gg)z8
:returns: True if sctp port has info, False otherwise

rJ  rF   r<  TFrK  rL  s     r9   has_sctpPortScannerHostDict.has_sctpy  sc     J#	I:4:,aH	I T$))+&&44V8I8I8K3L+Lr@   c                 ^    [        U5      [        L d   S[        U5       S35       eU S   U   $ )z
:returns: info for sctp port

rJ  rF   r<  rP  rL  s     r9   r<  PortScannerHostDict.sctp  s@     J#	I:4:,aH	I F|D!!r@   r   N)r   r   r   r   r   r   r   r   r   r   rF  rM  r   rT  rW  r   r]  r`  r;  rf  ri  r<  r   r   r@   r9   r   r     s\    
!$'		!	
!	
 		"r@   r   c                   *    \ rS rSrSrS rS rS rSrg)r!   i  z.
Exception error class for PortScanner class

c                     Xl         g r   value)r.   rp  s     r9   r:   PortScannerError.__init__  s    
r@   c                 ,    [        U R                  5      $ r   )reprrp  r=   s    r9   __str__PortScannerError.__str__  s    DJJr@   c                      SU R                    3$ )NzPortScannerError exception ro  r=   s    r9   __repr__PortScannerError.__repr__  s    ,TZZL99r@   ro  N)	r   r   r   r   r   r:   rt  rw  r   r   r@   r9   r!   r!     s    
 :r@   r!   c                       \ rS rSrSrg)rh   i  r   N)r   r   r   r   r   r   r@   r9   rh   rh     s    r@   rh   c                      SSK n U R                  R                  S5      nUR                  SS5        [        R                  UR                  5       R                  5       5      R                  5       nU$ )z
Gets last python-nmap published version

WARNING : it does an http connection to http://xael.org/pages/python-nmap/python-nmap_CURRENT_VERSION.txt

:returns: a string which indicate last published version (example :'0.4.3')

r   Nzxael.orgGETz2/pages/python-nmap/python-nmap_CURRENT_VERSION.txt)	http.clientclientHTTPConnectionrequestr$   r%   getresponsereadstrip)httpconnonline_versions      r9   __get_last_online_versionr    sY     ;;%%j1DLLLM\\$"2"2"4"9"9";<BBDNr@   c                    0 nU  H{  n[        X   5      [        [        4;   a  [        X   U5      X#'   M0  [        X   5      [        L a"  X    Vs/ s H  n[        XA5      PM     snX#'   Mf  X   R                  U5      X#'   M}     U$ s  snf )z
Change encoding for scan_result object from unicode to whatever

:param value: scan_result as dictionnary
:param code: default = "ascii", encoding destination

:returns: scan_result as dictionnary with new encoding
)rK   dictr   convert_nmap_output_to_encodingr   encode)rp  code	new_valuekr=  s        r9   r  r    s     I>d$788:58TJILEH~%FKh FN3A<h 	  %xt4	   s   B)ascii)r   r   r   r"   r   re   r   r   multiprocessingr   	xml.etreer   r   
__author____version____last_modification__objectr   r  r  r"  r  r   r   r!   rh   r  r  r   r@   r9   <module>r     s   /`  	 	 	   
 # ' 2
$ T
& T
t(Cv CRB' BPI"$ I"^:y : 	) 	(r@   