Skip to content

Commit df1c2f7

Browse files
committed
✨ add module write database
1 parent edb92d2 commit df1c2f7

File tree

7 files changed

+140
-12
lines changed

7 files changed

+140
-12
lines changed

WebPocket.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from lib.pocket import Pocket
1+
from lib.Pocket import Pocket
22

33

44
class WebPocket(Pocket):

database/pocket.db

2 KB
Binary file not shown.

lib/BaseExploit.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@ def __init__(self):
1717
pass
1818

1919
def get_info(self):
20-
return {
21-
"name": self.name,
22-
"description": self.description,
23-
"author": self.author,
24-
"references": self.references,
25-
"service_name": self.service_name,
26-
"service_version": self.service_version,
27-
}
20+
info = {}
21+
for field_name in self.info_fields:
22+
info[field_name] = getattr(self, field_name)
23+
return info
2824

2925
def update_info(self, info):
3026
for name in info:

lib/Database.py

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import os
2+
import sqlite3
3+
from fnmatch import fnmatchcase
4+
from utils.files import ROOT_PATH
5+
from utils.module import name_convert
6+
from importlib import import_module
7+
8+
9+
class Database:
10+
db_file = '{root_path}/database/pocket.db'.format(root_path=ROOT_PATH)
11+
connection = None
12+
cursor = None
13+
14+
def __init__(self):
15+
self.connection = sqlite3.connect(self.db_file)
16+
self.cursor = self.connection.cursor()
17+
18+
self.create_table()
19+
20+
# 初始化数据
21+
if self.get_module_count() == 0:
22+
self.db_rebuild()
23+
24+
def get_module_count(self):
25+
sql = 'select count(*) from modules;'
26+
rs = self.cursor.execute(sql)
27+
(count, ) = rs.fetchone()
28+
return count
29+
30+
def create_table(self):
31+
init_table_sql = (
32+
'CREATE TABLE IF NOT EXISTS "modules" ('
33+
'"id" INTEGER NOT NULL,'
34+
'"name" TEXT,'
35+
'"module_name" TEXT,'
36+
'"description" TEXT,'
37+
'"author" TEXT,'
38+
'"references" TEXT,'
39+
'"disclosure_date" TEXT,'
40+
'"service_name" TEXT,'
41+
'"service_version" TEXT,'
42+
'PRIMARY KEY("id")'
43+
');'
44+
)
45+
self.cursor.execute(init_table_sql)
46+
47+
def delete_table(self):
48+
delete_table_sql = "delete from modules;"
49+
self.cursor.execute(delete_table_sql)
50+
51+
def insert_module(self, info):
52+
with self.connection:
53+
self.connection.execute(
54+
"insert into modules \
55+
(name, module_name, description, author, 'references', disclosure_date, service_name, service_version) \
56+
values (?, ?, ?, ?, ?, ?, ?, ?)",
57+
(info.get('name'), info.get('module_name'), info.get('description'), '|'.join(info.get('author')),
58+
'|'.join(info.get('references')), info.get('disclosure_date'), info.get('service_name'),
59+
info.get('service_version'))
60+
)
61+
62+
def db_rebuild(self):
63+
self.delete_table()
64+
self.create_table()
65+
66+
for directory_name, directories, filenames in os.walk('modules/'):
67+
for filename in filenames:
68+
if filename not in ['__init__.py']\
69+
and not fnmatchcase(filename, "*.pyc")\
70+
and fnmatchcase(filename, "*.py"):
71+
full_name = "{directory}/{filename}".format(directory=directory_name, filename=filename)
72+
module_name = name_convert(full_name)
73+
module_class = import_module("modules.{module_name}".format(
74+
module_name=module_name.replace("/", ".")
75+
))
76+
module_instance = module_class.Exploit()
77+
module_info = module_instance.get_info()
78+
module_info['module_name'] = module_name
79+
self.insert_module(module_info)

lib/pocket.py lib/Pocket.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
from lib.cmd2 import Cmd
22
from utils.files import ROOT_PATH
3+
from utils.module import name_convert
34
from pathlib import Path
45
from colorama import Fore, Style
56
from tabulate import tabulate
67
from importlib import import_module
8+
from lib.Database import Database
79
from lib.ExploitOption import ExploitOption
810
from lib.exception.Module import ModuleNotUseException
911

1012

11-
class Pocket(Cmd):
13+
class Pocket(Cmd, Database):
1214
colors = "Always"
1315

1416
console_prompt = "{COLOR_START}WebPocket{COLOR_END}".format(COLOR_START="\033[4m", COLOR_END="\033[0m")
@@ -17,7 +19,8 @@ class Pocket(Cmd):
1719
module_instance = None
1820

1921
def __init__(self):
20-
super().__init__()
22+
super(Pocket, self).__init__()
23+
Database.__init__(self)
2124
self.prompt = self.console_prompt + self.console_prompt_end
2225
self.hidden_commands.extend(['alias', 'edit', 'macro', 'py', 'pyscript', 'shell', 'shortcuts', 'load'])
2326

@@ -32,7 +35,7 @@ def do_set(self, args):
3235
self.module_instance.options.set_option(arg, value)
3336

3437
def do_use(self, module_name):
35-
module_file = "{ROOT}/modules/{MODULE}.py".format(ROOT=ROOT_PATH, MODULE=module_name)
38+
module_file = name_convert(module_name)
3639
module_type = module_name.split("/")[0]
3740

3841
if Path(module_file).is_file():
@@ -94,6 +97,10 @@ def do_exploit(self, args):
9497
style_end=Style.RESET_ALL
9598
))
9699

100+
def do_db_rebuild(self, args):
101+
self.db_rebuild()
102+
self.poutput("Database rebuild done.", color=Fore.GREEN)
103+
97104
def set_prompt(self, module_type, module_name):
98105
module_prompt = " {module_type}({color}{module_name}{color_end})".format(
99106
module_type=module_type,
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from lib.BaseExploit import BaseExploit
2+
from lib.ExploitOption import ExploitOption
3+
4+
5+
class Exploit(BaseExploit):
6+
7+
def __init__(self):
8+
super(Exploit, self).__init__()
9+
self.update_info(info={
10+
"name": "zabbix 2.0.3 sqli",
11+
"description": "zabbix 2.0.3 jsrpc.php sqli",
12+
"author": ["TuuuNya", "unknown"],
13+
"references": [
14+
"https://www.hackersb.cn/hacker/219.html",
15+
"https://www.hackersb.cn/hacker/167.html",
16+
],
17+
"disclosure_date": "2016-08-22",
18+
"service_name": "zabbix",
19+
"service_version": "2.0.6",
20+
})
21+
self.register_options([
22+
ExploitOption(
23+
name="host",
24+
required=True,
25+
description="The target domain",
26+
value=None
27+
),
28+
])
29+
30+
def check(self):
31+
pass
32+
33+
def exploit(self):
34+
super(Exploit, self).exploit()
35+
print(self.options.get_option("host"))
36+
return "Exploit success, webshell: http://www.hackersb.cn/a.php"

utils/module.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from utils.files import ROOT_PATH
2+
3+
4+
def name_convert(name):
5+
if name.find(".py") is not -1:
6+
module_name = name.replace("modules/", "").replace(".py", "")
7+
return module_name
8+
else:
9+
full_name = "{ROOT}/modules/{MODULE}.py".format(ROOT=ROOT_PATH, MODULE=name)
10+
return full_name

0 commit comments

Comments
 (0)