Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/lib/core/replication.py
3554 views
1
#!/usr/bin/env python
2
3
"""
4
Copyright (c) 2006-2026 sqlmap developers (https://sqlmap.org)
5
See the file 'LICENSE' for copying permission
6
"""
7
8
import sqlite3
9
10
from lib.core.common import cleanReplaceUnicode
11
from lib.core.common import getSafeExString
12
from lib.core.common import unsafeSQLIdentificatorNaming
13
from lib.core.exception import SqlmapConnectionException
14
from lib.core.exception import SqlmapGenericException
15
from lib.core.exception import SqlmapValueException
16
from lib.core.settings import UNICODE_ENCODING
17
from lib.utils.safe2bin import safechardecode
18
19
class Replication(object):
20
"""
21
This class holds all methods/classes used for database
22
replication purposes.
23
"""
24
25
def __init__(self, dbpath):
26
try:
27
self.dbpath = dbpath
28
self.connection = sqlite3.connect(dbpath)
29
self.connection.isolation_level = None
30
self.cursor = self.connection.cursor()
31
except sqlite3.OperationalError as ex:
32
errMsg = "error occurred while opening a replication "
33
errMsg += "file '%s' ('%s')" % (dbpath, getSafeExString(ex))
34
raise SqlmapConnectionException(errMsg)
35
36
class DataType(object):
37
"""
38
Using this class we define auxiliary objects
39
used for representing sqlite data types.
40
"""
41
42
def __init__(self, name):
43
self.name = name
44
45
def __str__(self):
46
return self.name
47
48
def __repr__(self):
49
return "<DataType: %s>" % self
50
51
class Table(object):
52
"""
53
This class defines methods used to manipulate table objects.
54
"""
55
56
def __init__(self, parent, name, columns=None, create=True, typeless=False):
57
self.parent = parent
58
self.name = unsafeSQLIdentificatorNaming(name)
59
self.columns = columns
60
if create:
61
try:
62
self.execute('DROP TABLE IF EXISTS "%s"' % self.name)
63
if not typeless:
64
self.execute('CREATE TABLE "%s" (%s)' % (self.name, ','.join('"%s" %s' % (unsafeSQLIdentificatorNaming(colname), coltype) for colname, coltype in self.columns)))
65
else:
66
self.execute('CREATE TABLE "%s" (%s)' % (self.name, ','.join('"%s"' % unsafeSQLIdentificatorNaming(colname) for colname in self.columns)))
67
except Exception as ex:
68
errMsg = "problem occurred ('%s') while initializing the sqlite database " % getSafeExString(ex, UNICODE_ENCODING)
69
errMsg += "located at '%s'" % self.parent.dbpath
70
raise SqlmapGenericException(errMsg)
71
72
def insert(self, values):
73
"""
74
This function is used for inserting row(s) into current table.
75
"""
76
77
if len(values) == len(self.columns):
78
self.execute('INSERT INTO "%s" VALUES (%s)' % (self.name, ','.join(['?'] * len(values))), safechardecode(values))
79
else:
80
errMsg = "wrong number of columns used in replicating insert"
81
raise SqlmapValueException(errMsg)
82
83
def execute(self, sql, parameters=None):
84
try:
85
try:
86
self.parent.cursor.execute(sql, parameters or [])
87
except UnicodeError:
88
self.parent.cursor.execute(sql, cleanReplaceUnicode(parameters or []))
89
except sqlite3.OperationalError as ex:
90
errMsg = "problem occurred ('%s') while accessing sqlite database " % getSafeExString(ex, UNICODE_ENCODING)
91
errMsg += "located at '%s'. Please make sure that " % self.parent.dbpath
92
errMsg += "it's not used by some other program"
93
raise SqlmapGenericException(errMsg)
94
95
def beginTransaction(self):
96
"""
97
Great speed improvement can be gained by using explicit transactions around multiple inserts.
98
Reference: http://stackoverflow.com/questions/4719836/python-and-sqlite3-adding-thousands-of-rows
99
"""
100
self.execute('BEGIN TRANSACTION')
101
102
def endTransaction(self):
103
self.execute('END TRANSACTION')
104
105
def select(self, condition=None):
106
"""
107
This function is used for selecting row(s) from current table.
108
"""
109
query = 'SELECT * FROM "%s"' % self.name
110
if condition:
111
query += ' WHERE %s' % condition
112
113
self.execute(query)
114
return self.parent.cursor.fetchall()
115
116
def createTable(self, tblname, columns=None, typeless=False):
117
"""
118
This function creates Table instance with current connection settings.
119
"""
120
return Replication.Table(parent=self, name=tblname, columns=columns, typeless=typeless)
121
122
def __del__(self):
123
self.cursor.close()
124
self.connection.close()
125
126
# sqlite data types
127
NULL = DataType('NULL')
128
INTEGER = DataType('INTEGER')
129
REAL = DataType('REAL')
130
TEXT = DataType('TEXT')
131
BLOB = DataType('BLOB')
132
133