Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

#!/usr/bin/env python 

# -*- coding: utf-8 -*- 

""" antikythera.py 

 

The main program manager. 

 

""" 

import os 

import logging 

import argparse 

import multiprocessing as mp 

from multiprocessing import Process, Queue 

 

from time import sleep 

 

import antikythera.pysharkpatch 

 

from antikythera.capture import Capture 

from antikythera.decoder import Decoder 

from antikythera.metrics import Metrics 

 

_logger = logging.getLogger(__name__) 

 

__projectname__ = "Finding Ray" 

__author__ = "Team Awesome" 

__copyright__ = "Finding Ray" 

__license__ = "GNU GPLv3+" 

 

 

class Anti(Process): 

""" Start and monitor the worker processes. 

 

""" 

def __init__(self, num_processes, headless, interface=None, 

capturefile=None, max_qsize=100000, *args, **kwargs): 

""" 

 

""" 

super(Anti, self).__init__(*args, **kwargs) 

self.MAX_QUEUE_SIZE = max_qsize 

self.pkt_queue = Queue(self.MAX_QUEUE_SIZE) 

self.error_queue = Queue() 

self.NUMBER_OF_PROCESSES = num_processes 

self.workers = [] 

self.interface = interface 

self.capturefile = capturefile 

self.headless = headless 

self.exit = mp.Event() 

#_logger.info(self) 

 

# Allocates shared memory 

self.sharedMemory = {'numPackets': mp.Value('i', 0), 

'numSuspectPackets': mp.Value('i', 0), 

'defconLevel':mp.Value('i',5)} 

 

def __str__(self): 

s = ("Initial Process Manager State:\n" + 

"[*] Headless: {}\n".format(self.headless) + 

"[*] Queue: {}\n".format(self.queue) + 

"[*] Queue Size: {}\n".format(self.queue.qsize()) + 

"[*] Max Queue Size: {}\n".format(self.MAX_QUEUE_SIZE) + 

"[*] Number of Processes to Create: {}\n".format(self.NUMBER_OF_PROCESSES) + 

"[*] Number of Processes Created: {}\n".format(len(self.workers)) + 

"[*] Network Interface: {}\n".format(self.interface) + 

"[*] Capture File: {}".format(self.capturefile) 

) 

return s 

 

 

def run(self): 

""" 

 

""" 

try: 

cpus = mp.cpu_count() 

_logger.info("Anti: system has {} CPUs available".format(cpus)) 

except NotImplementedError: 

_logger.info("Anti: could not get number of available CPUs") 

 

for i in range(self.NUMBER_OF_PROCESSES): 

name = "decoder-" + str(i) 

_logger.info("Anti: Creating decoder process {}".format(name)) 

decoder_worker = Decoder(name, self.pkt_queue, name=name, daemon=True) 

self.workers.append(decoder_worker) 

 

_logger.info("Anti: Creating capture process capture") 

if self.interface is not None: 

_logger.debug("Anti: Creating capture process with" + 

"network interface") 

capture_worker = Capture("capture", 

self.pkt_queue, 

interface=self.interface, 

name="capture", 

daemon=True) 

elif self.capturefile is not None: 

_logger.debug("Anti: Creating capture process with capture file") 

capture_worker = Capture("capture", 

self.pkt_queue, 

capturefile=self.capturefile, 

name="capture", 

daemon=True) 

else: 

_logger.critical("Anti: no capture method supplied aborting!") 

 

self.workers.append(capture_worker) 

 

metrics_worker = Metrics("metrics", self.sharedMemory, name="metrics", daemon=True) 

self.workers.append(metrics_worker) 

 

for worker in self.workers: 

_logger.info("Anti: Starting process {}".format(worker)) 

worker.start() 

 

_logger.info("Anti: spawned {} child processes".format(len(mp.active_children()))) 

_logger.info("Anti: successfully started") 

 

self.wait() 

 

def shutdown(self): 

""" 

 

""" 

_logger.info("Anti: received shutdown command") 

self.exit.set() 

 

 

def exit_process(self, p): 

""" 

 

""" 

_logger.debug("Anti: shutting down {} pid {}".format(p, p.pid)) 

p.shutdown() 

_logger.info("Anti: waiting for process {} pid {}".format(p.process_id, p.pid)) 

p.join(60) 

if p.is_alive(): 

_logger.warning("Anti: process {} pid {} still alive calling terminate()".format(p.process_id, p.pid)) 

p.terminate() 

if p.is_alive(): 

_logger.critical("Anti: could not terminate process {} pid {}".format(p.process_id, p.pid)) 

 

 

def wait(self): 

""" 

 

""" 

_logger.info("Anti: waiting for shutdown") 

while not self.exit.is_set(): 

sleep(1) 

 

_logger.info("Anti: shutting down child processes") 

_logger.debug("Anti: Active children {}".format(mp.active_children())) 

 

for p in mp.active_children(): 

if p.name == "capture": 

self.exit_process(p) 

 

for p in mp.active_children(): 

if p.name == "decoder-0": 

self.exit_process(p) 

 

for p in mp.active_children(): 

if p.name == "metrics": 

self.exit_process(p) 

 

_logger.debug("Anti: Active children {}".format(mp.active_children())) 

for p in mp.active_children(): 

p.terminate() 

_logger.critical("Anti: waiting forever on process {} pid {}".format(p.process_id, p.pid)) 

p.join() 

 

_logger.info("Anti: Exiting") 

 

 

def create_parser(): 

""" Parse command line parameters. 

 

:return: command line parameters as :obj:`argparse.Namespace` 

Args: 

args ([str]): List of strings representing the command line arguments. 

 

Returns: 

argparse.Namespace: Simple object with a readable string 

representation of the argument list. 

 

""" 

parser = argparse.ArgumentParser( 

description="IMSI Catcher Detector.") 

source=parser.add_mutually_exclusive_group() 

logs=parser.add_mutually_exclusive_group() 

parser.add_argument( 

'-t', 

'--threads', 

nargs='?', 

type=int, 

default=1, 

dest="threads", 

help="Number of threads to use.", 

action='store') 

parser.add_argument( 

'-q', 

'--qsize', 

nargs='?', 

type=int, 

default=None, 

dest="qsize", 

help="The maximum queue size for packets waiting to be processed.", 

action='store') 

parser.add_argument( 

'--headless', 

default=False, 

dest="headless", 

help="Run in headless mode without GUI.", 

action='store_true') 

logs.add_argument( 

'-v', 

'--verbose', 

dest="loglevel", 

help="set loglevel to INFO", 

action='store_const', 

const=logging.INFO) 

logs.add_argument( 

'-vv', 

'--very-verbose', 

dest="loglevel", 

help="set loglevel to DEBUG", 

action='store_const', 

const=logging.DEBUG) 

logs.add_argument( 

'-vvv', 

'--trace', 

dest="loglevel", 

help="set loglevel to TRACE", 

action='store_const', 

const=logging.TRACE) 

source.add_argument( 

'-c', 

'--capture', 

nargs='?', 

type=str, 

default=None, 

dest="pcap", 

help="Path to a capture file to use as input.", 

action='store') 

source.add_argument( 

'-i', 

'--interface', 

nargs='?', 

type=str, 

default=None, 

dest="interface", 

help="The identifier of the network interface to use.", 

action='store') 

 

return parser 

 

 

if __name__ == '__main__': 

a = Anti()