2019-05-08 13:47:52 +03:00
#!/usr/bin/env python
2012-02-22 14:40:11 +04:00
"""
2019-01-05 23:38:52 +03:00
Copyright ( c ) 2006 - 2019 sqlmap developers ( http : / / sqlmap . org / )
2017-10-11 15:50:46 +03:00
See the file ' LICENSE ' for copying permission
2012-02-22 14:40:11 +04:00
"""
from lib . core . common import Backend
from lib . core . common import Format
2019-03-28 18:04:38 +03:00
from lib . core . compat import xrange
2012-02-22 14:40:11 +04:00
from lib . core . data import conf
from lib . core . data import kb
from lib . core . data import logger
from lib . core . enums import DBMS
2012-05-21 12:26:19 +04:00
from lib . core . enums import OS
2012-02-22 14:40:11 +04:00
from lib . core . session import setDbms
from lib . core . settings import DB2_ALIASES
from lib . request import inject
from plugins . generic . fingerprint import Fingerprint as GenericFingerprint
class Fingerprint ( GenericFingerprint ) :
def __init__ ( self ) :
GenericFingerprint . __init__ ( self , DBMS . DB2 )
2012-12-06 17:14:19 +04:00
def _versionCheck ( self ) :
2012-02-22 14:40:11 +04:00
minor , major = None , None
for version in reversed ( xrange ( 5 , 15 ) ) :
result = inject . checkBooleanExpression ( " (SELECT COUNT(*) FROM sysibm.sysversions WHERE versionnumber BETWEEN %d 000000 AND %d 999999)>0 " % ( version , version ) )
if result :
major = version
for version in reversed ( xrange ( 0 , 20 ) ) :
result = inject . checkBooleanExpression ( " (SELECT COUNT(*) FROM sysibm.sysversions WHERE versionnumber BETWEEN %d %02d 0000 AND %d %02d 9999)>0 " % ( major , version , major , version ) )
if result :
minor = version
version = " %s . %s " % ( major , minor )
break
break
if major and minor :
return " %s . %s " % ( major , minor )
else :
return None
def getFingerprint ( self ) :
2013-01-10 16:18:44 +04:00
value = " "
2012-02-22 14:40:11 +04:00
wsOsFp = Format . getOs ( " web server " , kb . headersFp )
if wsOsFp :
value + = " %s \n " % wsOsFp
if kb . data . banner :
dbmsOsFp = Format . getOs ( " back-end DBMS " , kb . bannerFp )
if dbmsOsFp :
value + = " %s \n " % dbmsOsFp
value + = " back-end DBMS: "
if not conf . extensiveFp :
value + = DBMS . DB2
return value
2018-03-13 15:45:42 +03:00
actVer = Format . getDbms ( )
blank = " " * 15
value + = " active fingerprint: %s " % actVer
2012-02-22 14:40:11 +04:00
if kb . bannerFp :
2018-08-25 23:57:49 +03:00
banVer = kb . bannerFp . get ( " dbmsVersion " )
2019-05-22 10:43:10 +03:00
if banVer :
banVer = Format . getDbms ( [ banVer ] )
value + = " \n %s banner parsing fingerprint: %s " % ( blank , banVer )
2012-02-22 14:40:11 +04:00
htmlErrorFp = Format . getErrorParsedDBMSes ( )
if htmlErrorFp :
value + = " \n %s html error message fingerprint: %s " % ( blank , htmlErrorFp )
return value
def checkDbms ( self ) :
2017-03-06 14:53:04 +03:00
if not conf . extensiveFp and Backend . isDbmsWithin ( DB2_ALIASES ) :
2012-02-22 14:40:11 +04:00
setDbms ( DBMS . DB2 )
return True
logMsg = " testing %s " % DBMS . DB2
logger . info ( logMsg )
2013-02-04 18:26:44 +04:00
result = inject . checkBooleanExpression ( " [RANDNUM]=(SELECT [RANDNUM] FROM SYSIBM.SYSDUMMY1) " )
2012-02-22 14:40:11 +04:00
if result :
logMsg = " confirming %s " % DBMS . DB2
logger . info ( logMsg )
2012-12-06 17:14:19 +04:00
version = self . _versionCheck ( )
2012-02-22 14:40:11 +04:00
if version :
Backend . setVersion ( version )
setDbms ( " %s %s " % ( DBMS . DB2 , Backend . getVersion ( ) ) )
return True
else :
warnMsg = " the back-end DBMS is not %s " % DBMS . DB2
logger . warn ( warnMsg )
return False
2012-05-21 12:26:19 +04:00
def checkDbmsOs ( self , detailed = False ) :
if Backend . getOs ( ) :
return
infoMsg = " fingerprinting the back-end DBMS operating system "
infoMsg + = " version and service pack "
logger . info ( infoMsg )
query = " (SELECT LENGTH(OS_NAME) FROM SYSIBMADM.ENV_SYS_INFO WHERE OS_NAME LIKE ' % WIN % ' )>0 "
result = inject . checkBooleanExpression ( query )
if not result :
Backend . setOs ( OS . LINUX )
else :
Backend . setOs ( OS . WINDOWS )
infoMsg = " the back-end DBMS operating system is %s " % Backend . getOs ( )
if result :
2018-03-13 15:45:42 +03:00
versions = {
" 2003 " : ( " 5.2 " , ( 2 , 1 ) ) ,
2012-05-21 12:26:19 +04:00
" 2008 " : ( " 7.0 " , ( 1 , ) ) ,
" 2000 " : ( " 5.0 " , ( 4 , 3 , 2 , 1 ) ) ,
" 7 " : ( " 6.1 " , ( 1 , 0 ) ) ,
" XP " : ( " 5.1 " , ( 2 , 1 ) ) ,
2018-03-13 15:45:42 +03:00
" NT " : ( " 4.0 " , ( 6 , 5 , 4 , 3 , 2 , 1 ) )
}
2012-05-21 12:26:19 +04:00
# Get back-end DBMS underlying operating system version
for version , data in versions . items ( ) :
query = " (SELECT LENGTH(OS_VERSION) FROM SYSIBMADM.ENV_SYS_INFO WHERE OS_VERSION = ' %s ' )>0 " % data [ 0 ]
result = inject . checkBooleanExpression ( query )
if result :
Backend . setOsVersion ( version )
infoMsg + = " %s " % Backend . getOsVersion ( )
break
if not Backend . getOsVersion ( ) :
return
# Get back-end DBMS underlying operating system service pack
for sp in versions [ Backend . getOsVersion ( ) ] [ 1 ] :
query = " (SELECT LENGTH(OS_RELEASE) FROM SYSIBMADM.ENV_SYS_INFO WHERE OS_RELEASE LIKE ' % Service Pack " + str ( sp ) + " % ' )>0 "
result = inject . checkBooleanExpression ( query )
if result :
Backend . setOsServicePack ( sp )
break
if not Backend . getOsServicePack ( ) :
Backend . setOsServicePack ( 0 )
debugMsg = " assuming the operating system has no service pack "
logger . debug ( debugMsg )
if Backend . getOsVersion ( ) :
infoMsg + = " Service Pack %d " % Backend . getOsServicePack ( )
logger . info ( infoMsg )