-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathc2s.py
executable file
·181 lines (130 loc) · 4.17 KB
/
c2s.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os, sys, glob, time
import argparse, configparser
import threading
import queue
import multiprocessing
from pprint import pprint
from core import messages
from core import searching
from core import execute
from core import utils
# Console colors
W = '\033[1;0m' # white
R = '\033[1;31m' # red
G = '\033[1;32m' # green
O = '\033[1;33m' # orange
B = '\033[1;34m' # blue
Y = '\033[1;93m' # yellow
P = '\033[1;35m' # purple
C = '\033[1;36m' # cyan
GR = '\033[1;37m' # gray
colors = [G,R,B,P,C,O,GR]
#############
# C2S - Command and Control server on Slack
#############
__author__ = '@j3ssiejjj'
__version__ = '1.0'
### Global stuff
# process_queue = queue.Queue()
current_path = os.path.dirname(os.path.realpath(__file__))
###
def cowsay():
print ("""{1}
-----------------------------
< You didn't say the {2}MAGIC WORD{1} >
-----------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/
\||----w |
|| || Contact: {2}{3}{1}
""".format(C, G, P, __author__))
#the config stuff
options = {
'user_token' : '',
'sender_token' : '',
'local_name' : '',
'irc_channel' : '',
'irc_channel_name' : '',
'status_channel' : '',
'report_channel' : '',
'control_bot' : '',
# 'process_queue' : process_queue
}
#skeleton for message body
mess = {
'title' : '',
'content' : '',
'title' : '',
'filename' : '',
'style' : '',
'comment' : '',
}
#loading config file
def config_parser(config_file):
config = configparser.ConfigParser()
config.read(config_file)
options['sender_token'] = config['configs']['sender_token']
options['user_token'] = config['configs']['user_token']
options['local_name'] = config['configs']['local_name']
options['control_bot'] = config['bots']['control']
#channels stuff
options['status_channel'] = config['channels']['status']
options['status_channel_name'] = config['channels']['status_name']
options['irc_channel'] = config['channels']['irc']
options['irc_channel_name'] = config['channels']['irc_name']
options['report_channel'] = config['channels']['report']
options['report_channel_name'] = config['channels']['report_name']
#checking if some command is done or not
def pull_process_queue(pq):
utils.print_good("Pulling the process queue")
while True:
utils.print_info("Process in Queue: {0}".format(str(pq.q.qsize())))
pq.pop_to_check()
time.sleep(2)
def pull_irc():
search = searching.Searching(options)
##keep search
while True:
utils.print_good("Finding IRC messages")
search.search_cmd()
#Slack API some how delay the result, so you can only get the lastest message about 30s-45s
utils.print_info("Waiting 45s to next sync")
time.sleep(45)
def daemon():
pq = execute.ProcessQueue(options)
options['process_queue'] = pq
#start new thread to get command
t = threading.Thread(target=pull_process_queue, args=(pq,))
t.start()
#keep search in irc channel to pull a command
pull_irc()
def parsing_argument(args):
#reading config
config_file = args.config
config_parser(config_file)
utils.print_good("Loading config from {0}".format(config_file))
if args.mode:
mode = args.mode
if mode == 'daemon':
daemon()
def update():
utils.run1('git fetch --all && git reset --hard origin/master')
sys.exit(0)
def main():
cowsay()
parser = argparse.ArgumentParser(description="Comand and Control on Slack")
parser.add_argument('-c','--config' , action='store', dest='config', help='config file', default='config.conf')
parser.add_argument('-m','--mode' , action='store', dest='mode', help='Choose mode to run', default='daemon')
parser.add_argument('--update', action='store_true', help='update lastest from git')
args = parser.parse_args()
# if len(sys.argv) == 1:
# # help_message()
# sys.exit(0)
if args.update:
update()
parsing_argument(args)
if __name__ == '__main__':
main()