|
7 | 7 | See the file 'doc/COPYING' for copying permission |
8 | 8 | """ |
9 | 9 |
|
10 | | -import codecs |
11 | | -import difflib |
12 | 10 | import os |
13 | 11 | import re |
14 | 12 | import shutil |
15 | 13 | import sys |
16 | 14 | import time |
17 | | -import urlparse |
18 | 15 |
|
19 | 16 | from distutils.dir_util import mkpath |
20 | | -from xml.dom.minidom import Document |
21 | 17 |
|
22 | 18 | from subprocess import PIPE |
23 | 19 | from subprocess import Popen as execute |
|
28 | 24 | from lib.core.data import conf |
29 | 25 | from lib.core.data import logger |
30 | 26 | from lib.core.data import paths |
31 | | -from lib.core.exception import sqlmapConnectionException |
32 | 27 | from lib.core.exception import sqlmapFilePathException |
33 | | -from lib.core.settings import MSSQL_VERSIONS_URL |
34 | 28 | from lib.core.settings import UNICODE_ENCODING |
35 | 29 | from lib.core.subprocessng import pollProcess |
36 | 30 | from lib.request.connect import Connect as Request |
37 | 31 |
|
38 | | -def __updateMSSQLXML(): |
39 | | - infoMsg = "updating Microsoft SQL Server XML versions file" |
40 | | - logger.info(infoMsg) |
41 | | - |
42 | | - try: |
43 | | - mssqlVersionsHtmlString, _ = Request.getPage(url=MSSQL_VERSIONS_URL, direct=True) |
44 | | - except sqlmapConnectionException, _: |
45 | | - __mssqlPath = urlparse.urlsplit(MSSQL_VERSIONS_URL) |
46 | | - __mssqlHostname = __mssqlPath[1] |
47 | | - |
48 | | - warnMsg = "sqlmap was unable to connect to %s," % __mssqlHostname |
49 | | - warnMsg += " check your Internet connection and retry" |
50 | | - logger.warn(warnMsg) |
51 | | - |
| 32 | +def update(): |
| 33 | + if not conf.updateAll: |
52 | 34 | return |
53 | 35 |
|
54 | | - releases = re.findall("class=\"BCC_DV_01DarkBlueTitle\">SQL Server ([\d\.]+) Builds", mssqlVersionsHtmlString, re.I | re.M) |
55 | | - releasesCount = len(releases) |
56 | | - |
57 | | - # Create the minidom document |
58 | | - doc = Document() |
59 | | - |
60 | | - # Create the <root> base element |
61 | | - root = doc.createElement("root") |
62 | | - doc.appendChild(root) |
63 | | - |
64 | | - for index in range(0, releasesCount): |
65 | | - release = releases[index] |
66 | | - |
67 | | - # Skip Microsoft SQL Server 6.5 because the HTML |
68 | | - # table is in another format |
69 | | - if release == "6.5": |
70 | | - continue |
71 | | - |
72 | | - # Create the <signatures> base element |
73 | | - signatures = doc.createElement("signatures") |
74 | | - signatures.setAttribute("release", release) |
75 | | - root.appendChild(signatures) |
76 | | - |
77 | | - startIdx = mssqlVersionsHtmlString.index("SQL Server %s Builds" % releases[index]) |
78 | | - |
79 | | - if index == releasesCount - 1: |
80 | | - stopIdx = len(mssqlVersionsHtmlString) |
81 | | - else: |
82 | | - stopIdx = mssqlVersionsHtmlString.index("SQL Server %s Builds" % releases[index + 1]) |
83 | | - |
84 | | - mssqlVersionsReleaseString = mssqlVersionsHtmlString[startIdx:stopIdx] |
85 | | - servicepackVersion = re.findall("</td><td>[7\.0|2000|2005|2008]*(.*?)</td><td.*?([\d\.]+)</td>[\r]*\n", mssqlVersionsReleaseString, re.I | re.M) |
86 | | - |
87 | | - for servicePack, version in servicepackVersion: |
88 | | - if servicePack.startswith(" "): |
89 | | - servicePack = servicePack[1:] |
90 | | - if "/" in servicePack: |
91 | | - servicePack = servicePack[:servicePack.index("/")] |
92 | | - if "(" in servicePack: |
93 | | - servicePack = servicePack[:servicePack.index("(")] |
94 | | - if "-" in servicePack: |
95 | | - servicePack = servicePack[:servicePack.index("-")] |
96 | | - if "*" in servicePack: |
97 | | - servicePack = servicePack[:servicePack.index("*")] |
98 | | - if servicePack.startswith("+"): |
99 | | - servicePack = "0%s" % servicePack |
100 | | - |
101 | | - servicePack = servicePack.replace("\t", " ") |
102 | | - servicePack = servicePack.replace("No SP", "0") |
103 | | - servicePack = servicePack.replace("RTM", "0") |
104 | | - servicePack = servicePack.replace("SP", "") |
105 | | - servicePack = servicePack.replace("Service Pack", "") |
106 | | - servicePack = servicePack.replace("<a href=\"http:", "") |
107 | | - servicePack = servicePack.replace(" ", " ") |
108 | | - servicePack = servicePack.replace("+ ", "+") |
109 | | - servicePack = servicePack.replace(" +", "+") |
110 | | - |
111 | | - if servicePack.endswith(" "): |
112 | | - servicePack = servicePack[:-1] |
113 | | - |
114 | | - if servicePack and version: |
115 | | - # Create the main <card> element |
116 | | - signature = doc.createElement("signature") |
117 | | - signatures.appendChild(signature) |
118 | | - |
119 | | - # Create a <version> element |
120 | | - versionElement = doc.createElement("version") |
121 | | - signature.appendChild(versionElement) |
122 | | - |
123 | | - # Give the <version> elemenet some text |
124 | | - versionText = doc.createTextNode(version) |
125 | | - versionElement.appendChild(versionText) |
126 | | - |
127 | | - # Create a <servicepack> element |
128 | | - servicepackElement = doc.createElement("servicepack") |
129 | | - signature.appendChild(servicepackElement) |
130 | | - |
131 | | - # Give the <servicepack> elemenet some text |
132 | | - servicepackText = doc.createTextNode(servicePack) |
133 | | - servicepackElement.appendChild(servicepackText) |
134 | | - |
135 | | - # Get the XML old file content to a local variable |
136 | | - mssqlXml = codecs.open(paths.MSSQL_XML, "r", UNICODE_ENCODING) |
137 | | - oldMssqlXml = mssqlXml.read() |
138 | | - oldMssqlXmlSignatures = oldMssqlXml.count("<signature>") |
139 | | - oldMssqlXmlList = oldMssqlXml.splitlines(1) |
140 | | - mssqlXml.close() |
141 | | - |
142 | | - # Backup the XML old file |
143 | | - shutil.copy(paths.MSSQL_XML, "%s.bak" % paths.MSSQL_XML) |
144 | | - |
145 | | - # Save our newly created XML to the signatures file |
146 | | - mssqlXml = codecs.open(paths.MSSQL_XML, "w", UNICODE_ENCODING) |
147 | | - doc.writexml(writer=mssqlXml, addindent=" ", newl="\n") |
148 | | - mssqlXml.close() |
149 | | - |
150 | | - # Get the XML new file content to a local variable |
151 | | - mssqlXml = codecs.open(paths.MSSQL_XML, "r", UNICODE_ENCODING) |
152 | | - newMssqlXml = mssqlXml.read() |
153 | | - newMssqlXmlSignatures = newMssqlXml.count("<signature>") |
154 | | - newMssqlXmlList = newMssqlXml.splitlines(1) |
155 | | - mssqlXml.close() |
156 | | - |
157 | | - # If the new XML versions file differs from the old one it probably |
158 | | - # means that we have got new Microsoft SQL Server versions |
159 | | - if oldMssqlXmlSignatures != newMssqlXmlSignatures: |
160 | | - infoMsg = "Microsoft SQL Server XML versions file updated successfully. " |
161 | | - |
162 | | - if oldMssqlXmlSignatures < newMssqlXmlSignatures: |
163 | | - infoMsg += "%d " % (newMssqlXmlSignatures - oldMssqlXmlSignatures) |
164 | | - infoMsg += "new signatures added since the last update" |
165 | | - |
166 | | - # NOTE: This should never happen, in this rare case it might |
167 | | - # be that the Microsoft SQL Server versions database |
168 | | - # (MSSQL_VERSIONS_URL) changed its structure |
169 | | - else: |
170 | | - infoMsg += "%d " % (oldMssqlXmlSignatures - newMssqlXmlSignatures) |
171 | | - infoMsg += "signatures removed since the last update" |
172 | | - |
173 | | - logger.info(infoMsg) |
174 | | - |
175 | | - message = "Do you want to see the differences? [Y/n] " |
176 | | - test = readInput(message, default="Y") |
177 | | - |
178 | | - if not test or test[0] in ("y", "Y"): |
179 | | - infoMsg = "Differences:" |
180 | | - logger.info(infoMsg) |
181 | | - |
182 | | - # Compare the old XML file with the new one |
183 | | - diff = difflib.unified_diff(oldMssqlXmlList, newMssqlXmlList, "%s.bak" % paths.MSSQL_XML, paths.MSSQL_XML) |
184 | | - sys.stdout.writelines(diff) |
185 | | - else: |
186 | | - infoMsg = "no new Microsoft SQL Server versions since the " |
187 | | - infoMsg += "last update" |
188 | | - logger.info(infoMsg) |
189 | | - |
190 | | -def __updateSqlmap(): |
191 | 36 | rootDir = paths.SQLMAP_ROOT_PATH |
192 | 37 |
|
193 | 38 | infoMsg = "updating sqlmap to latest development version from the " |
@@ -240,10 +85,3 @@ def notify(event_dict): |
240 | 85 | revision = re.search("revision\s+([\d]+)", svnStdout, re.I) |
241 | 86 | if revision: |
242 | 87 | logger.info('updated to the latest revision %s' % revision.group(1)) |
243 | | - |
244 | | -def update(): |
245 | | - if not conf.updateAll: |
246 | | - return |
247 | | - |
248 | | - __updateSqlmap() |
249 | | - __updateMSSQLXML() |
0 commit comments