Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to limit the maximum size of the log file #21

Merged
merged 2 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# App files
py2kms_server.log*
py3kms_server.log*

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don’t work, or not
# install all needed dependencies.
#Pipfile.lock

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

42 changes: 22 additions & 20 deletions py2-kms/kmsBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
except ImportError:
pass

logger = logging.getLogger('root')

class UUID(Structure):
commonHdr = ()
structure = (
Expand Down Expand Up @@ -131,7 +133,7 @@ def serverLogic(self, kmsRequest):
skuId TEXT, licenseStatus TEXT, lastRequestTime INTEGER, kmsEpid TEXT, requestCount INTEGER)")

except sqlite3.Error, e:
logging.error("Error %s:" % e.args[0])
logger.error("Error %s:" % e.args[0])
sys.exit(1)

finally:
Expand All @@ -140,8 +142,8 @@ def serverLogic(self, kmsRequest):
con.close()

shell_message(nshell = 15)
logging.debug("KMS Request Bytes: \n%s\n" % justify(binascii.b2a_hex(str(kmsRequest))))
logging.debug("KMS Request: \n%s\n" % justify(kmsRequest.dump(print_to_stdout = False)))
logger.debug("KMS Request Bytes: \n%s\n" % justify(binascii.b2a_hex(str(kmsRequest))))
logger.debug("KMS Request: \n%s\n" % justify(kmsRequest.dump(print_to_stdout = False)))

clientMachineId = kmsRequest['clientMachineId'].get()
applicationId = kmsRequest['applicationId'].get()
Expand All @@ -156,10 +158,10 @@ def serverLogic(self, kmsRequest):
tz = get_localzone()
local_dt = tz.localize(requestDatetime)
except UnknownTimeZoneError:
logging.warning('Unknown time zone ! Request time not localized.')
logger.warning('Unknown time zone ! Request time not localized.')
local_dt = requestDatetime
except ImportError:
logging.warning('Module "tzlocal" not available ! Request time not localized.')
logger.warning('Module "tzlocal" not available ! Request time not localized.')
local_dt = requestDatetime

# Get SkuId, AppId and client threshold.
Expand Down Expand Up @@ -206,12 +208,12 @@ def serverLogic(self, kmsRequest):
"kmsEpid" : None
}

logging.info("Machine Name: %s" % infoDict["machineName"])
logging.info("Client Machine ID: %s" % infoDict["clientMachineId"])
logging.info("Application ID: %s" % infoDict["appId"])
logging.info("SKU ID: %s" % infoDict["skuId"])
logging.info("License Status: %s" % infoDict["licenseStatus"])
logging.info("Request Time: %s" % local_dt.strftime('%Y-%m-%d %H:%M:%S %Z (UTC%z)'))
logger.info("Machine Name: %s" % infoDict["machineName"])
logger.info("Client Machine ID: %s" % infoDict["clientMachineId"])
logger.info("Application ID: %s" % infoDict["appId"])
logger.info("SKU ID: %s" % infoDict["skuId"])
logger.info("License Status: %s" % infoDict["licenseStatus"])
logger.info("Request Time: %s" % local_dt.strftime('%Y-%m-%d %H:%M:%S %Z (UTC%z)'))

if self.config['sqlite'] and self.config['dbSupport']:
con = None
Expand Down Expand Up @@ -247,10 +249,10 @@ def serverLogic(self, kmsRequest):
clientMachineId=:clientMachineId;", infoDict)

except sqlite3.Error, e:
logging.error("Error %s:" % e.args[0])
logger.error("Error %s:" % e.args[0])

except sqlite3.Error, e:
logging.error("Error %s:" % e.args[0])
logger.error("Error %s:" % e.args[0])
sys.exit(1)
finally:
if con:
Expand Down Expand Up @@ -292,17 +294,17 @@ def createKmsResponse(self, kmsRequest, currentClientCount):
(str(response["kmsEpid"].decode('utf-16le')), str(kmsRequest['clientMachineId'].get())))

except sqlite3.Error, e:
logging.error("Error %s:" % e.args[0])
logger.error("Error %s:" % e.args[0])

except sqlite3.Error, e:
logging.error("Error %s:" % e.args[0])
logger.error("Error %s:" % e.args[0])
sys.exit(1)
finally:
if con:
con.commit()
con.close()

logging.info("Server ePID: %s" % response["kmsEpid"].decode('utf-16le').encode('utf-8'))
logger.info("Server ePID: %s" % response["kmsEpid"].decode('utf-16le').encode('utf-8'))

return response

Expand All @@ -314,16 +316,16 @@ def generateKmsResponseData(data, config):
currentDate = time.strftime("%a %b %d %H:%M:%S %Y")

if version == 4:
logging.info("Received V%d request on %s." % (version, currentDate))
logger.info("Received V%d request on %s." % (version, currentDate))
messagehandler = kmsRequestV4.kmsRequestV4(data, config)
elif version == 5:
logging.info("Received V%d request on %s." % (version, currentDate))
logger.info("Received V%d request on %s." % (version, currentDate))
messagehandler = kmsRequestV5.kmsRequestV5(data, config)
elif version == 6:
logging.info("Received V%d request on %s." % (version, currentDate))
logger.info("Received V%d request on %s." % (version, currentDate))
messagehandler = kmsRequestV6.kmsRequestV6(data, config)
else:
logging.info("Unhandled KMS version V%d." % version)
logger.info("Unhandled KMS version V%d." % version)
messagehandler = kmsRequestUnknown.kmsRequestUnknown(data, config)

return messagehandler.executeRequestLogic()
10 changes: 6 additions & 4 deletions py2-kms/kmsRequestV4.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from aes import AES
from formatText import shell_message, justify

logger = logging.getLogger('root')

# v4 AES Key
key = bytearray([0x05, 0x3D, 0x83, 0x07, 0xF9, 0xE5, 0xF0, 0x88, 0xEB, 0x5E, 0xA6, 0x68, 0x6C, 0xF0, 0x37, 0xC7, 0xE4, 0xEF, 0xD2, 0xD6])

Expand Down Expand Up @@ -102,8 +104,8 @@ def generateResponse(self, responseBuffer, hash):

## Debug stuff.
shell_message(nshell = 16)
logging.debug("KMS V4 Response: \n%s\n" % justify(response.dump(print_to_stdout = False)))
logging.debug("KMS V4 Response Bytes: \n%s\n" % justify(binascii.b2a_hex(str(response))))
logger.debug("KMS V4 Response: \n%s\n" % justify(response.dump(print_to_stdout = False)))
logger.debug("KMS V4 Response Bytes: \n%s\n" % justify(binascii.b2a_hex(str(response))))

return str(response)

Expand All @@ -120,7 +122,7 @@ def generateRequest(self, requestBase):

## Debug stuff.
shell_message(nshell = 10)
logging.debug("Request V4 Data: \n%s\n" % justify(request.dump(print_to_stdout = False)))
logging.debug("Request V4: \n%s\n" % justify(binascii.b2a_hex(str(request))))
logger.debug("Request V4 Data: \n%s\n" % justify(request.dump(print_to_stdout = False)))
logger.debug("Request V4: \n%s\n" % justify(binascii.b2a_hex(str(request))))

return request
10 changes: 6 additions & 4 deletions py2-kms/kmsRequestV5.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from structure import Structure
from formatText import justify, shell_message

logger = logging.getLogger('root')

class kmsRequestV5(kmsBase):
class RequestV5(Structure):
class Message(Structure):
Expand Down Expand Up @@ -135,8 +137,8 @@ def generateResponse(self, iv, encryptedResponse, requestData):
response['padding'] = bytearray(self.getPadding(bodyLength))

shell_message(nshell = 16)
logging.info("KMS V%d Response: \n%s\n" % (self.ver, justify(response.dump(print_to_stdout = False))))
logging.info("KMS V%d Structure Bytes: \n%s\n" % (self.ver, justify(binascii.b2a_hex(str(response)))))
logger.info("KMS V%d Response: \n%s\n" % (self.ver, justify(response.dump(print_to_stdout = False))))
logger.info("KMS V%d Structure Bytes: \n%s\n" % (self.ver, justify(binascii.b2a_hex(str(response)))))

return str(response)

Expand Down Expand Up @@ -166,7 +168,7 @@ def generateRequest(self, requestBase):
request['message'] = message

shell_message(nshell = 10)
logging.info("Request V%d Data: \n%s\n" % (self.ver, justify(request.dump(print_to_stdout = False))))
logging.info("Request V%d: \n%s\n" % (self.ver, justify(binascii.b2a_hex(str(request)))))
logger.info("Request V%d Data: \n%s\n" % (self.ver, justify(request.dump(print_to_stdout = False))))
logger.info("Request V%d: \n%s\n" % (self.ver, justify(binascii.b2a_hex(str(request)))))

return request
14 changes: 8 additions & 6 deletions py2-kms/rpcBind.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from structure import Structure
from formatText import shell_message, justify

logger = logging.getLogger('root')

uuidNDR32 = uuid.UUID('8a885d04-1ceb-11c9-9fe8-08002b104860')
uuidNDR64 = uuid.UUID('71710533-beba-4937-8319-b5dbef9ccc36')
uuidTime = uuid.UUID('6cb71c2c-9812-4540-0300-000000000000')
Expand Down Expand Up @@ -74,8 +76,8 @@ class handler(rpcBase.rpcBase):
def parseRequest(self):
request = MSRPCHeader(self.data)
shell_message(nshell = 3)
logging.debug("RPC Bind Request Bytes: \n%s\n" % justify(binascii.b2a_hex(self.data)))
logging.debug("RPC Bind Request: \n%s\n%s\n" % (justify(request.dump(print_to_stdout = False)),
logger.debug("RPC Bind Request Bytes: \n%s\n" % justify(binascii.b2a_hex(self.data)))
logger.debug("RPC Bind Request: \n%s\n%s\n" % (justify(request.dump(print_to_stdout = False)),
justify(MSRPCBind(request['pduData']).dump(print_to_stdout = False))))

return request
Expand Down Expand Up @@ -117,8 +119,8 @@ def generateResponse(self, request):
response['ctx_items'] += str(resp)

shell_message(nshell = 4)
logging.debug("RPC Bind Response: \n%s\n" % justify(response.dump(print_to_stdout = False)))
logging.debug("RPC Bind Response Bytes: \n%s\n" % justify(binascii.b2a_hex(str(response))))
logger.debug("RPC Bind Response: \n%s\n" % justify(response.dump(print_to_stdout = False)))
logger.debug("RPC Bind Response Bytes: \n%s\n" % justify(binascii.b2a_hex(str(response))))

return response

Expand Down Expand Up @@ -157,9 +159,9 @@ def generateRequest(self):
request['pduData'] = str(bind)

shell_message(nshell = 0)
logging.debug("RPC Bind Request: \n%s\n%s\n" % (justify(request.dump(print_to_stdout = False)),
logger.debug("RPC Bind Request: \n%s\n%s\n" % (justify(request.dump(print_to_stdout = False)),
justify(MSRPCBind(request['pduData']).dump(print_to_stdout = False))))
logging.debug("RPC Bind Request Bytes: \n%s\n" % justify(binascii.b2a_hex(str(request))))
logger.debug("RPC Bind Request Bytes: \n%s\n" % justify(binascii.b2a_hex(str(request))))

return request

Expand Down
14 changes: 8 additions & 6 deletions py2-kms/rpcRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import rpcBase
from formatText import justify, shell_message

logger = logging.getLogger('root')

class handler(rpcBase.rpcBase):
def parseRequest(self):
request = MSRPCRequestHeader(self.data)
shell_message(nshell = 14)
logging.debug("RPC Message Request Bytes: \n%s\n" % justify(binascii.b2a_hex(self.data)))
logging.debug("RPC Message Request: \n%s\n" % justify(request.dump(print_to_stdout = False)))
logger.debug("RPC Message Request Bytes: \n%s\n" % justify(binascii.b2a_hex(self.data)))
logger.debug("RPC Message Request: \n%s\n" % justify(request.dump(print_to_stdout = False)))

return request

Expand All @@ -36,8 +38,8 @@ def generateResponse(self, request):
response['pduData'] = responseData

shell_message(nshell = 17)
logging.debug("RPC Message Response: \n%s\n" % justify(response.dump(print_to_stdout = False)))
logging.debug("RPC Message Response Bytes: \n%s\n" % justify(binascii.b2a_hex(str(response))))
logger.debug("RPC Message Response: \n%s\n" % justify(response.dump(print_to_stdout = False)))
logger.debug("RPC Message Response Bytes: \n%s\n" % justify(binascii.b2a_hex(str(response))))

return response

Expand All @@ -54,8 +56,8 @@ def generateRequest(self):
request['pduData'] = str(self.data)

shell_message(nshell = 11)
logging.debug("RPC Message Request: \n%s\n" % justify(request.dump(print_to_stdout = False)))
logging.debug("RPC Message Request Bytes: \n%s\n" % justify(binascii.b2a_hex(str(request))))
logger.debug("RPC Message Request: \n%s\n" % justify(request.dump(print_to_stdout = False)))
logger.debug("RPC Message Request Bytes: \n%s\n" % justify(binascii.b2a_hex(str(request))))

return request

Expand Down
Loading