Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8f32c74

Browse files
committed
code refactoring
1 parent 6700cab commit 8f32c74

2 files changed

Lines changed: 70 additions & 64 deletions

File tree

lib/controller/checks.py

Lines changed: 21 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
import socket
1212
import time
1313

14-
from difflib import SequenceMatcher
15-
1614
from lib.core.agent import agent
1715
from lib.core.common import beep
1816
from lib.core.common import extractRegexResult
17+
from lib.core.common import findDynamicContent
1918
from lib.core.common import getCompiledRegex
2019
from lib.core.common import getInjectionTests
2120
from lib.core.common import getUnicode
@@ -47,7 +46,6 @@
4746
from lib.core.exception import sqlmapUserQuitException
4847
from lib.core.session import setString
4948
from lib.core.session import setRegexp
50-
from lib.core.settings import DYNAMICITY_MARK_LENGTH
5149
from lib.core.settings import UPPER_RATIO_BOUND
5250
from lib.core.unescaper import unescaper
5351
from lib.request.connect import Connect as Request
@@ -494,8 +492,7 @@ def checkDynParam(place, parameter, value):
494492

495493
def checkDynamicContent(firstPage, secondPage):
496494
"""
497-
This function checks if the provided pages have dynamic content. If they
498-
are dynamic, proper markings will be made.
495+
This function checks for the dynamic content in the provided pages
499496
"""
500497

501498
if kb.nullConnection:
@@ -510,47 +507,29 @@ def checkDynamicContent(firstPage, secondPage):
510507
logger.debug(debugMsg)
511508
return
512509

513-
infoMsg = "searching for dynamic content"
514-
logger.info(infoMsg)
515-
516-
blocks = SequenceMatcher(None, firstPage, secondPage).get_matching_blocks()
517-
kb.dynamicMarkings = []
510+
conf.seqMatcher.set_seq1(firstPage)
511+
conf.seqMatcher.set_seq2(secondPage)
518512

519-
# Removing too small matching blocks
520-
i = 0
521-
while i < len(blocks):
522-
block = blocks[i]
523-
(_, _, length) = block
513+
# In case of an intolerable difference turn on dynamicity removal engine
514+
if conf.seqMatcher.quick_ratio() <= UPPER_RATIO_BOUND:
515+
findDynamicContent(firstPage, secondPage)
524516

525-
if length <= DYNAMICITY_MARK_LENGTH:
526-
blocks.remove(block)
527-
528-
else:
529-
i += 1
517+
count = 0
518+
while not Request.queryPage():
519+
count += 1
530520

531-
# Making of dynamic markings based on prefix/suffix principle
532-
if len(blocks) > 0:
533-
blocks.insert(0, None)
534-
blocks.append(None)
521+
if count > conf.retries:
522+
errMsg = "target url is too dynamic. unable to continue. "
523+
errMsg += "consider using other switches (e.g. "
524+
errMsg += "--longest-common, --string, --text-only, etc.)"
525+
raise sqlmapSiteTooDynamic, errMsg
535526

536-
for i in xrange(len(blocks) - 1):
537-
prefix = firstPage[blocks[i][0]:blocks[i][0] + blocks[i][2]] if blocks[i] else None
538-
suffix = firstPage[blocks[i + 1][0]:blocks[i + 1][0] + blocks[i + 1][2]] if blocks[i + 1] else None
539-
540-
if prefix is None and blocks[i + 1][0] == 0:
541-
continue
542-
543-
if suffix is None and (blocks[i][0] + blocks[i][2] >= len(firstPage)):
544-
continue
527+
warnMsg = "target url is heavily dynamic"
528+
warnMsg += ", sqlmap is going to retry the request"
529+
logger.critical(warnMsg)
545530

546-
prefix = trimAlphaNum(prefix)
547-
suffix = trimAlphaNum(suffix)
548-
549-
kb.dynamicMarkings.append((re.escape(prefix[-DYNAMICITY_MARK_LENGTH/2:]) if prefix else None, re.escape(suffix[:DYNAMICITY_MARK_LENGTH/2]) if suffix else None))
550-
551-
if len(kb.dynamicMarkings) > 0:
552-
infoMsg = "dynamic content marked for removal (%d region%s)" % (len(kb.dynamicMarkings), 's' if len(kb.dynamicMarkings) > 1 else '')
553-
logger.info(infoMsg)
531+
secondPage, _ = Request.queryPage(content=True)
532+
findDynamicContent(firstPage, secondPage)
554533

555534
def checkStability():
556535
"""
@@ -637,29 +616,7 @@ def checkStability():
637616
errMsg = "Empty value supplied"
638617
raise sqlmapNoneDataException, errMsg
639618
else:
640-
conf.seqMatcher.set_seq1(firstPage)
641-
conf.seqMatcher.set_seq2(secondPage)
642-
643-
# In case of an intolerable difference turn on dynamicity removal engine
644-
if conf.seqMatcher.quick_ratio() <= UPPER_RATIO_BOUND:
645-
checkDynamicContent(firstPage, secondPage)
646-
647-
count = 0
648-
while not Request.queryPage():
649-
count += 1
650-
651-
if count > conf.retries:
652-
errMsg = "target url is too dynamic. unable to continue. "
653-
errMsg += "consider using other switches (e.g. "
654-
errMsg += "--longest-common, --string, --text-only, etc.)"
655-
raise sqlmapSiteTooDynamic, errMsg
656-
657-
warnMsg = "target url is heavily dynamic"
658-
warnMsg += ", sqlmap is going to retry the request"
659-
logger.critical(warnMsg)
660-
661-
secondPage, _ = Request.queryPage(content=True)
662-
checkDynamicContent(firstPage, secondPage)
619+
checkDynamicContent(firstPage, secondPage)
663620

664621
return kb.pageStable
665622

lib/core/common.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
from lib.core.settings import DUMP_STOP_MARKER
7373
from lib.core.settings import MIN_TIME_RESPONSES
7474
from lib.core.settings import TIME_STDEV_COEFF
75+
from lib.core.settings import DYNAMICITY_MARK_LENGTH
7576
from lib.core.threads import getCurrentThreadData
7677

7778
class UnicodeRawConfigParser(RawConfigParser):
@@ -1739,6 +1740,54 @@ def aliasToDbmsEnum(value):
17391740

17401741
return retVal
17411742

1743+
def findDynamicContent(firstPage, secondPage):
1744+
"""
1745+
This function checks if the provided pages have dynamic content. If they
1746+
are dynamic, proper markings will be made.
1747+
"""
1748+
1749+
infoMsg = "searching for dynamic content"
1750+
logger.info(infoMsg)
1751+
1752+
blocks = SequenceMatcher(None, firstPage, secondPage).get_matching_blocks()
1753+
kb.dynamicMarkings = []
1754+
1755+
# Removing too small matching blocks
1756+
i = 0
1757+
while i < len(blocks):
1758+
block = blocks[i]
1759+
(_, _, length) = block
1760+
1761+
if length <= DYNAMICITY_MARK_LENGTH:
1762+
blocks.remove(block)
1763+
1764+
else:
1765+
i += 1
1766+
1767+
# Making of dynamic markings based on prefix/suffix principle
1768+
if len(blocks) > 0:
1769+
blocks.insert(0, None)
1770+
blocks.append(None)
1771+
1772+
for i in xrange(len(blocks) - 1):
1773+
prefix = firstPage[blocks[i][0]:blocks[i][0] + blocks[i][2]] if blocks[i] else None
1774+
suffix = firstPage[blocks[i + 1][0]:blocks[i + 1][0] + blocks[i + 1][2]] if blocks[i + 1] else None
1775+
1776+
if prefix is None and blocks[i + 1][0] == 0:
1777+
continue
1778+
1779+
if suffix is None and (blocks[i][0] + blocks[i][2] >= len(firstPage)):
1780+
continue
1781+
1782+
prefix = trimAlphaNum(prefix)
1783+
suffix = trimAlphaNum(suffix)
1784+
1785+
kb.dynamicMarkings.append((re.escape(prefix[-DYNAMICITY_MARK_LENGTH/2:]) if prefix else None, re.escape(suffix[:DYNAMICITY_MARK_LENGTH/2]) if suffix else None))
1786+
1787+
if len(kb.dynamicMarkings) > 0:
1788+
infoMsg = "dynamic content marked for removal (%d region%s)" % (len(kb.dynamicMarkings), 's' if len(kb.dynamicMarkings) > 1 else '')
1789+
logger.info(infoMsg)
1790+
17421791
def removeDynamicContent(page):
17431792
"""
17441793
Removing dynamic content from supplied

0 commit comments

Comments
 (0)