From 6b56e2a8ca79d8d6f4305a68bcc634b7627b0aba Mon Sep 17 00:00:00 2001 From: Tal Aloni Date: Tue, 21 Feb 2017 12:43:44 +0200 Subject: [PATCH] Improved code readability and exception messages --- .../NetBios/SessionPackets/SessionPacket.cs | 2 +- SMBLibrary/SMB1/Commands/SMB1Command.cs | 4 ++-- SMBLibrary/Server/SMBServer.cs | 2 +- SMBLibrary/Win32/Security/LoginAPI.cs | 20 +++++++++---------- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/SMBLibrary/NetBios/SessionPackets/SessionPacket.cs b/SMBLibrary/NetBios/SessionPackets/SessionPacket.cs index df39f86..4ff60f4 100644 --- a/SMBLibrary/NetBios/SessionPackets/SessionPacket.cs +++ b/SMBLibrary/NetBios/SessionPackets/SessionPacket.cs @@ -73,7 +73,7 @@ namespace SMBLibrary.NetBios case SessionPacketTypeName.SessionKeepAlive: return new SessionKeepAlivePacket(buffer, offset); default: - throw new InvalidRequestException("Invalid NetBIOS session packet type"); + throw new InvalidRequestException("Invalid NetBIOS session packet type: 0x" + type.ToString("X")); } } } diff --git a/SMBLibrary/SMB1/Commands/SMB1Command.cs b/SMBLibrary/SMB1/Commands/SMB1Command.cs index 5fefc11..f1b758c 100644 --- a/SMBLibrary/SMB1/Commands/SMB1Command.cs +++ b/SMBLibrary/SMB1/Commands/SMB1Command.cs @@ -156,7 +156,7 @@ namespace SMBLibrary.SMB1 case CommandName.SMB_COM_NT_CREATE_ANDX: return new NTCreateAndXRequest(buffer, offset, isUnicode); default: - throw new NotImplementedException("SMB Command 0x" + ((byte)commandName).ToString("X")); + throw new NotImplementedException("SMB Command 0x" + commandName.ToString("X")); } } @@ -288,7 +288,7 @@ namespace SMBLibrary.SMB1 case CommandName.SMB_COM_NT_CREATE_ANDX: return new NTCreateAndXResponse(buffer, offset); default: - throw new NotImplementedException("SMB Command 0x" + ((byte)commandName).ToString("X")); + throw new NotImplementedException("SMB Command 0x" + commandName.ToString("X")); } } diff --git a/SMBLibrary/Server/SMBServer.cs b/SMBLibrary/Server/SMBServer.cs index 0bb6f41..57d5b81 100644 --- a/SMBLibrary/Server/SMBServer.cs +++ b/SMBLibrary/Server/SMBServer.cs @@ -191,7 +191,7 @@ namespace SMBLibrary.Server catch (Exception ex) { state.ClientSocket.Close(); - Log(Severity.Warning, "Rejected Invalid NetBIOS session packet: " + ex.ToString()); + Log(Severity.Warning, "Rejected Invalid NetBIOS session packet: " + ex.Message); break; } diff --git a/SMBLibrary/Win32/Security/LoginAPI.cs b/SMBLibrary/Win32/Security/LoginAPI.cs index ad340ce..cc613e1 100644 --- a/SMBLibrary/Win32/Security/LoginAPI.cs +++ b/SMBLibrary/Win32/Security/LoginAPI.cs @@ -43,15 +43,15 @@ namespace SMBLibrary.Win32.Security bool success = LogonUser(userName, String.Empty, password, (int)logonType, LOGON32_PROVIDER_WINNT40, out token); if (!success) { - uint error = (uint)Marshal.GetLastWin32Error(); - if (error == (uint)Win32Error.ERROR_ACCOUNT_RESTRICTION || - error == (uint)Win32Error.ERROR_ACCOUNT_DISABLED || - error == (uint)Win32Error.ERROR_LOGON_FAILURE || - error == (uint)Win32Error.ERROR_LOGON_TYPE_NOT_GRANTED) + Win32Error error = (Win32Error)Marshal.GetLastWin32Error(); + if (error == Win32Error.ERROR_ACCOUNT_RESTRICTION || + error == Win32Error.ERROR_ACCOUNT_DISABLED || + error == Win32Error.ERROR_LOGON_FAILURE || + error == Win32Error.ERROR_LOGON_TYPE_NOT_GRANTED) { return false; } - throw new Exception("ValidateUser failed, error: 0x" + ((uint)error).ToString("X")); + throw new Exception("ValidateUser failed, Win32 error: " + error.ToString("D")); } CloseHandle(token); return success; @@ -68,10 +68,10 @@ namespace SMBLibrary.Win32.Security } else { - uint error = (uint)Marshal.GetLastWin32Error(); - return (error == (uint)Win32Error.ERROR_ACCOUNT_RESTRICTION || - error == (uint)Win32Error.ERROR_ACCOUNT_DISABLED || - error == (uint)Win32Error.ERROR_LOGON_TYPE_NOT_GRANTED); + Win32Error error = (Win32Error)Marshal.GetLastWin32Error(); + return (error == Win32Error.ERROR_ACCOUNT_RESTRICTION || + error == Win32Error.ERROR_ACCOUNT_DISABLED || + error == Win32Error.ERROR_LOGON_TYPE_NOT_GRANTED); } } }