mirror of
https://github.com/splunk/DECEIVE.git
synced 2025-07-02 00:57:26 -04:00
Added centralized logging
This commit is contained in:
4
accounts.json
Normal file
4
accounts.json
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"guest":"",
|
||||||
|
"user1":"secretpw"
|
||||||
|
}
|
@ -2,21 +2,25 @@
|
|||||||
# private key in it to use as a server host key. An SSH host certificate
|
# private key in it to use as a server host key. An SSH host certificate
|
||||||
# can optionally be provided in the file ``ssh_host_key-cert.pub``.
|
# can optionally be provided in the file ``ssh_host_key-cert.pub``.
|
||||||
|
|
||||||
import asyncio, asyncssh, crypt, sys
|
import asyncio
|
||||||
|
import asyncssh
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
import logging
|
||||||
passwords = {'guest': '', # guest account with no password
|
import datetime
|
||||||
'user123': 'qV2iEadIGV2rw' # password of 'secretpw'
|
|
||||||
}
|
|
||||||
|
|
||||||
async def handle_client(process: asyncssh.SSHServerProcess) -> None:
|
async def handle_client(process: asyncssh.SSHServerProcess) -> None:
|
||||||
|
# This is the main loop for handling SSH client connections.
|
||||||
|
# Any user interaction should be done here.
|
||||||
|
|
||||||
process.stdout.write('Welcome to my SSH server, %s!\n' %
|
process.stdout.write('Welcome to my SSH server, %s!\n' %
|
||||||
process.get_extra_info('username'))
|
process.get_extra_info('username'))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async for line in process.stdin:
|
async for line in process.stdin:
|
||||||
line = line.rstrip('\n')
|
line = line.rstrip('\n')
|
||||||
if line:
|
logging.info(f"INPUT: {line}")
|
||||||
process.stdout.write('You entered: %s\n' % line)
|
process.stdout.write('You entered: %s\n' % line)
|
||||||
except asyncssh.BreakReceived:
|
except asyncssh.BreakReceived:
|
||||||
pass
|
pass
|
||||||
@ -25,36 +29,60 @@ async def handle_client(process: asyncssh.SSHServerProcess) -> None:
|
|||||||
|
|
||||||
class MySSHServer(asyncssh.SSHServer):
|
class MySSHServer(asyncssh.SSHServer):
|
||||||
def connection_made(self, conn: asyncssh.SSHServerConnection) -> None:
|
def connection_made(self, conn: asyncssh.SSHServerConnection) -> None:
|
||||||
print('SSH connection received from %s.' %
|
logging.info(f"SSH connection received from {conn.get_extra_info('peername')[0]}.")
|
||||||
conn.get_extra_info('peername')[0])
|
|
||||||
|
|
||||||
def connection_lost(self, exc: Optional[Exception]) -> None:
|
def connection_lost(self, exc: Optional[Exception]) -> None:
|
||||||
if exc:
|
if exc:
|
||||||
print('SSH connection error: ' + str(exc), file=sys.stderr)
|
print('SSH connection error: ' + str(exc), file=sys.stderr)
|
||||||
|
logging.error('SSH connection error: ' + str(exc), file=sys.stderr)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print('SSH connection closed.')
|
print('SSH connection closed.')
|
||||||
|
logging.info("SSH connection closed.")
|
||||||
|
|
||||||
def begin_auth(self, username: str) -> bool:
|
def begin_auth(self, username: str) -> bool:
|
||||||
# If the user's password is the empty string, no auth is required
|
# If the user's password is the empty string, no auth is required
|
||||||
return passwords.get(username) != ''
|
return accounts.get(username) != ''
|
||||||
|
|
||||||
def password_auth_supported(self) -> bool:
|
def password_auth_supported(self) -> bool:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def validate_password(self, username: str, password: str) -> bool:
|
def validate_password(self, username: str, password: str) -> bool:
|
||||||
pw = passwords.get(username, '*')
|
pw = accounts.get(username, '*')
|
||||||
return crypt.crypt(password, pw) == pw
|
return ((pw != '*') and (password == pw))
|
||||||
|
|
||||||
async def start_server() -> None:
|
async def start_server() -> None:
|
||||||
await asyncssh.create_server(MySSHServer, '', 8022,
|
await asyncssh.create_server(MySSHServer, '', 8022,
|
||||||
server_host_keys=['ssh_host_key'],
|
server_host_keys=['ssh_host_key'],
|
||||||
process_factory=handle_client)
|
process_factory=handle_client)
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
|
|
||||||
try:
|
def read_accounts() -> dict:
|
||||||
loop.run_until_complete(start_server())
|
accounts = dict()
|
||||||
except (OSError, asyncssh.Error) as exc:
|
|
||||||
sys.exit('Error starting server: ' + str(exc))
|
|
||||||
|
|
||||||
|
with open('accounts.json', 'r') as f:
|
||||||
|
accounts = json.loads(f.read())
|
||||||
|
|
||||||
|
return accounts
|
||||||
|
|
||||||
|
#### MAIN ####
|
||||||
|
|
||||||
|
# Set up the logging
|
||||||
|
logging.basicConfig(
|
||||||
|
filename="ssh_log.log",
|
||||||
|
level=logging.INFO,
|
||||||
|
format="%(asctime)s %(levelname)s:%(message)s",
|
||||||
|
datefmt="%Y-%m-%d %H:%M:%S. %Z"
|
||||||
|
)
|
||||||
|
|
||||||
|
logging.Formatter.formatTime = (lambda self, record, datefmt=None: datetime.datetime.fromtimestamp(record.created, datetime.timezone.utc).astimezone().isoformat(sep="T",timespec="milliseconds"))
|
||||||
|
|
||||||
|
# Read the valid accounts
|
||||||
|
accounts = read_accounts()
|
||||||
|
|
||||||
|
# Kick off the server!
|
||||||
|
loop = asyncio.new_event_loop()
|
||||||
|
asyncio.set_event_loop(loop)
|
||||||
|
loop.run_until_complete(start_server())
|
||||||
loop.run_forever()
|
loop.run_forever()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user