-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathopencanary.tac
182 lines (152 loc) · 5.11 KB
/
opencanary.tac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import traceback
import warnings
import sys
from twisted.application import service
from pkg_resources import iter_entry_points
from opencanary.config import config, is_docker
from opencanary.logger import getLogger
from opencanary.modules.http import CanaryHTTP
from opencanary.modules.https import CanaryHTTPS
from opencanary.modules.ftp import CanaryFTP
from opencanary.modules.ssh import CanarySSH
from opencanary.modules.telnet import Telnet
from opencanary.modules.httpproxy import HTTPProxy
from opencanary.modules.mysql import CanaryMySQL
from opencanary.modules.mssql import MSSQL
from opencanary.modules.ntp import CanaryNtp
from opencanary.modules.tftp import CanaryTftp
from opencanary.modules.vnc import CanaryVNC
from opencanary.modules.sip import CanarySIP
from opencanary.modules.git import CanaryGit
from opencanary.modules.redis import CanaryRedis
from opencanary.modules.tcpbanner import CanaryTCPBanner
from opencanary.modules.rdp import CanaryRDP
def warn(*args, **kwargs):
pass
warnings.warn = warn
# from opencanary.modules.example0 import CanaryExample0
# from opencanary.modules.example1 import CanaryExample1
ENTRYPOINT = "canary.usermodule"
MODULES = [
CanaryFTP,
CanaryGit,
CanaryHTTP,
CanaryHTTPS,
CanaryMySQL,
CanaryNtp,
CanaryRDP,
CanaryRedis,
CanarySIP,
CanarySSH,
CanaryTCPBanner,
CanaryTftp,
CanaryVNC,
HTTPProxy,
MSSQL,
Telnet,
# CanaryExample0,
# CanaryExample1,
]
if config.moduleEnabled("snmp"):
try:
# Module need Scapy, but the rest of OpenCanary doesn't
from opencanary.modules.snmp import CanarySNMP
MODULES.append(CanarySNMP)
except ImportError:
print("Can't import SNMP. Please ensure you have Scapy installed.")
pass
if config.moduleEnabled("llmnr"):
try:
# Module needs Scapy, but the rest of OpenCanary doesn't
from opencanary.modules.llmnr import CanaryLLMNR
MODULES.append(CanaryLLMNR)
except ImportError:
print("Can't import LLMNR. Please ensure you have Scapy installed.")
pass
# NB: imports below depend on inotify, only available on linux
if sys.platform.startswith("linux"):
from opencanary.modules.samba import CanarySamba
MODULES.append(CanarySamba)
if config.moduleEnabled("portscan") and is_docker():
# Remove portscan if running in DOCKER (specified in Dockerfile)
print("Can't use portscan in Docker. Portscan module disabled.")
else:
from opencanary.modules.portscan import CanaryPortscan
MODULES.append(CanaryPortscan)
logger = getLogger(config)
def start_mod(application, klass): # noqa: C901
try:
obj = klass(config=config, logger=logger)
except Exception:
err = "Failed to instantiate instance of class %s in %s. %s" % (
klass.__name__,
klass.__module__,
traceback.format_exc(),
)
logMsg({"logdata": err})
return
if hasattr(obj, "startYourEngines"):
try:
obj.startYourEngines()
msg = "Ran startYourEngines on class %s in %s" % (
klass.__name__,
klass.__module__,
)
logMsg({"logdata": msg})
except Exception:
err = "Failed to run startYourEngines on %s in %s. %s" % (
klass.__name__,
klass.__module__,
traceback.format_exc(),
)
logMsg({"logdata": err})
elif hasattr(obj, "getService"):
try:
service = obj.getService()
if not isinstance(service, list):
service = [service]
for s in service:
s.setServiceParent(application)
msg = "Added service from class %s in %s to fake" % (
klass.__name__,
klass.__module__,
)
logMsg({"logdata": msg})
except Exception:
err = "Failed to add service from class %s in %s. %s" % (
klass.__name__,
klass.__module__,
traceback.format_exc(),
)
logMsg({"logdata": err})
else:
err = "The class %s in %s does not have any required starting method." % (
klass.__name__,
klass.__module__,
)
logMsg({"logdata": err})
def logMsg(msg):
data = {}
data["logdata"] = {"msg": msg}
logger.log(data, retry=False)
application = service.Application("opencanaryd")
# List of modules to start
start_modules = []
# Add all custom modules
# (Permanently enabled as they don't officially use settings yet)
for ep in iter_entry_points(ENTRYPOINT):
try:
klass = ep.load(require=False)
start_modules.append(klass)
except Exception:
err = "Failed to load class from the entrypoint: %s. %s" % (
str(ep),
traceback.format_exc(),
)
logMsg({"logdata": err})
# Add only enabled modules
start_modules.extend(filter(lambda m: config.moduleEnabled(m.NAME), MODULES))
for klass in start_modules:
start_mod(application, klass)
msg = "Canary running!!!"
logMsg({"logdata": msg})