Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Windows Service and Auth Validator
#!/home/bocchrj/anaconda3/envs/myenv/bin/python
"""win-svc-auth-validate.py: Windows Service and Auth Validator"""
# Owned
__author__ = 'Rich Bocchinfuso'
__copyright__ = 'Copyright 2021, Windows Service and Auth Validator'
__credits__ = ['Rich Bocchinfuso']
__license__ = 'MIT'
__version__ = '0.1.0'
__maintainer__ = 'Rich Bocchinfuso'
__email__ = 'rbocchinfuso@gmail.com'
__status__ = 'Alpha'
__app__ = 'win-svc-auth-validate.py'
# Libs & Modules
import socket
from loguru import logger
from requests import auth
from winrm.protocol import Protocol
# Global Variables
LOG_LEVEL = 'DEBUG'
HOST = '192.168.1.99'
RDP_PORT = 3389
WINRM_PORT = 5985
TRANSPORT = 'ntlm'
USERNAME = 'user'
PASSWORD = 'pwd'
@logger.catch
def main():
"""Things starts here."""
svc_validate(HOST, RDP_PORT)
auth_validate(HOST, WINRM_PORT, TRANSPORT, USERNAME, PASSWORD)
@logger.catch
def svc_validate(host, port):
"""Validates that service is running on specified host port.
Args:
host (str): hostmane or IP address
port (int): port number
"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((host, port))
success = "{} is reachable on port {}".format(host, port)
logger.info (success)
except socket.error as e:
logger.error ("Error on connect: %s") % e
s.close()
@logger.catch
def auth_validate(host, port, transport, username, password):
"""[summary]
Args:
host (str): hostname or IP address
port (int): WinRM port number
transport (str): transport type
username (str): username for auth
password (str): password for auth
"""
p = Protocol(
endpoint='http://{}:{}/wsman'.format(host,port),
transport=transport,
username=username,
password=password,
server_cert_validation='ignore')
shell_id = p.open_shell()
command_id = p.run_command(shell_id, 'ipconfig', ['/all'])
command_id = p.run_command(shell_id, 'hostname')
std_out, std_err, status_code = p.get_command_output(shell_id, command_id)
p.cleanup_command(shell_id, command_id)
logger.info(std_out.decode("utf-8"))
logger.info(status_code)
p.close_shell(shell_id)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment