⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logit.cpp

📁 DES算法实现
💻 CPP
字号:
//////////////////////////////////////////////////////////////////////
// Class CLogIt Implementation File
//
// Created: 2/7/00
//
// Author: Daniel Madden
//         Daniel.Madden@Compaq.Com
//
// History: 
//
// Modifications (by other programmers): 
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LogIt.h"

#include "default.h"
#include "hex.h"

#include <direct.h>


/////////////////////////////////////////////////////////////////////////////
// CLogIt Contruction

CLogIt::CLogIt()
{

	// The below value is the Pass Phrase for the encrypted "Pass Phrase"
	// I do it this way so that the executable doesn't have any 
	// plain text strings in it that tell someone what the pass phrase is
	m_csAppsPPhrase.Format("%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",0x40,0x4D,0x49,0x4D,0x47,0x4D,0x50,0x4D,0x47,0x4D,0x50,0x4E,0x4D,0x53,0x50,0x4E,0x4D,0x53,0x65,0x63,0x4D,0x23,0x4D,0x31);

	// Set the Log dir
	//m_csLogDir = _T("\\\\SERVER\\Share\\Logs\\");
	m_csLogDir = _T("C:\\Logs\\");
	m_bDirExists = CreateLogPath(m_csLogDir);

	// Log file handle
	m_fLog = NULL;

	// used by the "GetSystemDomainUserName" function
	cchUser = UNLEN;
	cchDomain = DNLEN;

	// used by the "GetDomainUser" function
	m_csDomainUserName = _T("Domain\\Username");

}

//**********************************************************************************
//
// nEntryType:
//
//		0 = Error
//		1 = Warning
//		2 = Information
//		3 = Lock (Secure)
//		4 = Dir Folder (Closed)
//		5 = Dir Folder (Opened)
//		6 = Paper (Log)
//		7 = User (incognito)
//		8 = Note
//
//**********************************************************************************
void CLogIt::WriteLog(CString csMsg, int nEntryType)
{
	// We don't want to write to it 
	// if it's being writing to!
	if (!IsOpen()) {

		CString csText = _T("");
		if (m_bDirExists) {
			m_fLog = fopen(m_csFullLogName, "a");
			if (m_fLog != NULL) {
				csText.Format("%s~%i~%s~%s", m_csDomainUserName, nEntryType, m_csLogEntryTime, csMsg);
				CString szRet = EncryptString(LPCTSTR(csText), LPCTSTR(m_csAppsPPhrase));
				csText = szRet;
				fprintf(m_fLog, "%s\n", csText);
			}

			int nClosed = fclose(m_fLog);
			if (nClosed == 0)
				m_fLog = NULL;
			else
				MessageBox(NULL, "Problem closing log file", "Error: Closing file", MB_OK);

			Sleep(100); // Give it time to close

		}
		else {
			csText.Format("Logging Directory doesn't exist [%s]!", m_csLogDir);
			MessageBox(NULL, csText, "Error: Accessing Log Directory", MB_OK);
		}
	}
}


BOOL CLogIt::CreateLogDirectory(CString csDirName)
{

	BOOL bRet = CreateDirectory(csDirName, NULL);
	if (!bRet) {
		DWORD dwErrNum = GetLastError();
		if (dwErrNum != 183) {
			CString csErrTitle;
			csErrTitle.Format("Error: %u", dwErrNum);
			MessageBox(NULL, DisplayError(dwErrNum), csErrTitle, MB_OK|MB_ICONSTOP);
		}
		else 
			bRet = TRUE;
	}
	return bRet;
}


BOOL CLogIt::CreateLogPath(void)
{

	BOOL bRet = TRUE;

	if (!m_csLogDir.IsEmpty()) {

		// int's
		int n = -1;

		// CString's
		CString csDir = _T("");
		
		// CStringArray's
		CStringArray csaLogPath;


		// Gets the system time.
		CTime ctNow = CTime::GetCurrentTime();
		
		// Format the Month Directory name
		//
		// Formatted to: "July2000.logs"
		CString csMonthDir = ctNow.Format("%B%Y.logs\\");

		int nHour = ctNow.GetHour();
		int nMinute = ctNow.GetMinute();
		int nSecond = ctNow.GetSecond();
		int nDay = ctNow.GetDay();
		int nMonth = ctNow.GetMonth();
		int nYear = ctNow.GetYear();

		m_csLogEntryTime = _T("");

		// Formatted to: "09:20:54"
		m_csLogEntryTime.Format("%.2i:%.2i:%.2i", nHour, nMinute, nSecond);
		
		// Formatted to: "lg03072000.log"
		m_csLogName.Format("lg%.2i%.2i%i.log", nDay, nMonth, nYear);

		// Formatted to: "<Your Choice>\July2000.logs"
		m_csLogPath = m_csLogDir + csMonthDir;

		// Formatted to: "<Your Choice>\July2000.logs\lg03072000.log"
		m_csFullLogName = m_csLogDir + csMonthDir + m_csLogName;

		
		//////////////////////////////
		// Begin the work!
		//////////////////////////////
		//
		// put directory structure into an array (CString)
		Split(m_csFullLogName, _T("\\"), csaLogPath, FALSE);

		// Get the Drive (or Server) name
		CString csTmp = csaLogPath.GetAt(0);

		int nUBound = csaLogPath.GetUpperBound();

		// Notice "n<nUBound" below...
		// the path I Split, contains a filename and
		// that is not needed when creating the Dir Structure
		//
		// If you wanted just the filename from the path...just do this:
		//
		// CString csFilename = csaLogPath.GetAt(csaLogPath.GetUpperBound());
		//
		for (n=0; n<nUBound; n++) {

			if (n == 0) {
				if (csTmp.Mid(1, 1) == ":") { // We are working with (C:) format

					// Format to:  "C:\"
					csDir = csaLogPath.GetAt(n) + "\\";
				}
				else { // We are working with a "\\Server\share"...

					// Format to:  "\\SERVER\"
					csDir = _T("\\\\");
					csDir += csaLogPath.GetAt(n) + "\\";
				}
			}
			else {
				csDir += csaLogPath.GetAt(n) + "\\";
				if (csTmp.Mid(1, 1) == ":") { // We are working with (C:) format
					// Format to:  "C:\<Dir>..."
					//
					// and create it
					if (!CreateLogDirectory(csDir))
						return FALSE;

				}
				else { // We are working with a "\\Server\share"...

					if (n == 1) { // Share Name
						// Format to:  "\\SERVER\Share\"
						csDir += csaLogPath.GetAt(n) + "\\";
					}
					else {
						// Format to:  "\\SERVER\Share\<Dir>..."
						//
						// and create it
						csDir += csaLogPath.GetAt(n) + "\\";

						if (!CreateLogDirectory(csDir))
							return FALSE;
					}
				}
			}
		}
	}
	else {
		MessageBox(NULL, "You must have a directory name to Create the Log Path!", "Error: CreateLogPath", MB_OK|MB_ICONSTOP);
		return FALSE;
	}

	return TRUE;

}


BOOL CLogIt::CreateLogPath(CString csLogDir)
{

	// Simply sets the 'm_csLogDir' variable
	// and calls 'CreateLogPath()'
	BOOL bRet = FALSE;

	if (!csLogDir.IsEmpty()) {
		m_csLogDir = csLogDir;
		bRet = CreateLogPath();
	}

	return bRet;

}


void CLogIt::Split(CString Source, CString Deliminator, CStringArray& AddIt, BOOL bAddEmpty)
{
	// initialize the variables
	CString		 newCString = Source;
	CString		 tmpCString = "";
	CString		 AddCString = "";

	int pos1 = 0;
	int pos = 0;

	AddIt.RemoveAll();

	if (Deliminator.IsEmpty()) {
		// Add default [comma] if empty!
		// acknowledgement: Prasad [gprasad@rti.ie]
		Deliminator = ","; 
	}

	// do this loop as long as you have a deliminator
	do {
		// set to zero
		pos1 = 0;
		// position of deliminator starting at pos1 (0)
		pos = newCString.Find(Deliminator, pos1);
		// if the deliminator is found...
		if ( pos != -1 ) {

			// load a new var with the info left
			// of the position
			CString AddCString = newCString.Left(pos);// - 1);

			if (!AddCString.IsEmpty()) {
				// if there is a string to add, then
				// add it to the Array
				AddIt.Add(AddCString);
			}
			else if (bAddEmpty) {
				// if empty strings are ok, then add them
				AddIt.Add(AddCString);
			}

			// make a copy of the of this var. with the info
			// right of the deliminator
			tmpCString = newCString.Mid(pos + Deliminator.GetLength());
			
			// reset this var with new info
			newCString = tmpCString;
		}
	} while ( pos != -1 );
	
	if (!newCString.IsEmpty()) {
		// as long as the variable is not emty, add it
		AddIt.Add(newCString);
	}

}

BOOL CLogIt::GetOSType (CString& csOSVersion, int& nType)
{
	OSVERSIONINFO versionInfo;

	CString csMsg = _T("");

	// set the size of OSVERSIONINFO, before calling the function
	versionInfo.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);

	// Get the version information
	if (!::GetVersionEx (&versionInfo)) {
		CString csErrMsg = _T("");;
		csMsg.Format("Error(%u): GetVersionEx: %s", GetLastError(), DisplayError(GetLastError()));
		WriteLog(csMsg, 0);
		return FALSE;
	}

	// Check which OS it is
	if (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
		// Windows NT test
		if ((versionInfo.dwMajorVersion > 4) && (versionInfo.dwMinorVersion >= 0)) {
			nType = 5; 
			csOSVersion = _T("Win2000");
		}
		else if ((versionInfo.dwMajorVersion == 4) && (versionInfo.dwMinorVersion >= 0)) {
			nType = 4; 
			csOSVersion = _T("WinNT4");
		}
	}
	else if (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
		nType = 2; // Win95
		csOSVersion = _T("Win95");

		// Windows 98 test
		if ((versionInfo.dwMajorVersion > 4) || ((versionInfo.dwMajorVersion == 4) &&
			(versionInfo.dwMinorVersion > 0))) {
			nType = 3; // Win98
			csOSVersion = _T("Win98");
		}
	}
	else if (versionInfo.dwPlatformId == VER_PLATFORM_WIN32s) {
		nType = 1; // Win32s
		csOSVersion = _T("Win32s");
	}
	else {
		nType = -1;
		csOSVersion = _T("Not Recognized");
	}

	return TRUE;
}

CString CLogIt::DisplayError(LONG lCode)
{
	
	LPVOID lpMsgBuf;
	FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 
		          NULL,
				  lCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
				  (LPTSTR) &lpMsgBuf,
				  0,
				  NULL );// Process any inserts in lpMsgBuf.

	CString csRet = (LPCTSTR)lpMsgBuf;

	// Free the buffer.
	LocalFree( lpMsgBuf );

	// Return the string.
	return csRet;

}


BOOL CLogIt::GetSystemDomainUserName(void)
{

	LPTSTR UserName = User;
	LPDWORD cchUserName = &cchUser;
	LPTSTR DomainName = Domain;
	LPDWORD cchDomainName = &cchDomain;

	HANDLE hToken;

	#define MY_BUFSIZE 512  // highly unlikely to exceed 512 bytes

	UCHAR InfoBuffer[ MY_BUFSIZE ];
	DWORD cbInfoBuffer = MY_BUFSIZE;
	SID_NAME_USE snu;

	BOOL bSuccess;

	BOOL bRet = FALSE;

	CString csMsg = _T("");

	if(!OpenThreadToken(GetCurrentThread(),	TOKEN_QUERY, TRUE, &hToken)) {

		if(GetLastError() == ERROR_NO_TOKEN) {
			//
			// attempt to open the process token, since no thread token
			// exists
			//
			if(!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken )) {
				csMsg.Format("Error(%u): OpenProcessToken: %s", GetLastError(), DisplayError(GetLastError()));
				WriteLog(csMsg, 0);
				return FALSE;
			}

		} 
		else {
		    //
		    // error trying to get thread token
		    //
			csMsg.Format("Error(%u): OpenThreadToken: %s", GetLastError(), DisplayError(GetLastError()));
			WriteLog(csMsg, 0);
		    return FALSE;
		}
	
		bSuccess = GetTokenInformation(hToken, TokenUser, InfoBuffer, cbInfoBuffer, &cbInfoBuffer);
		if(!bSuccess) {
			csMsg.Format("Error(%u): GetTokenInformation: %s", GetLastError(), DisplayError(GetLastError()));
			WriteLog(csMsg, 0);
			return FALSE;
		}
		else {
			bRet = LookupAccountSid(NULL, ((PTOKEN_USER)InfoBuffer)->User.Sid, UserName, cchUserName, DomainName, cchDomainName, &snu );
			if (!bRet) {
				csMsg.Format("Error(%u): LookupAccountSid: %s", GetLastError(), DisplayError(GetLastError()));
				WriteLog(csMsg, 0);
				CloseHandle(hToken);
				return FALSE;
			}
			else {
				m_csDomainUserName.Format("%s\\%s", DomainName, UserName);
			}
		}
	}

	return TRUE;

}

using namespace CryptoPP;

CString CLogIt::EncryptString(CString csInStr, CString csPassPhrase)
{

	unsigned int unLen = csInStr.GetLength();
	char* szOutstr;

	DefaultEncryptorWithMAC encryptor((LPCTSTR)csPassPhrase, new HexEncoder());
	encryptor.Put((byte *)(LPCTSTR)csInStr, unLen);
	encryptor.Close();

	unsigned int unOutputLength = encryptor.MaxRetrieveable();
	szOutstr = new char[unOutputLength+1];
	encryptor.Get((byte *)szOutstr, unOutputLength);
	szOutstr[unOutputLength] = 0;

	CString csRet = szOutstr;
    delete szOutstr;
	return csRet;
}


CString CLogIt::DecryptString(CString csInStr, CString csPassPhrase)
{

	unsigned int unLen = csInStr.GetLength();
	char* szOutstr;

	DefaultDecryptorWithMAC *p;

	HexDecoder decryptor(p=new DefaultDecryptorWithMAC((LPCTSTR)csPassPhrase));
	decryptor.Put((byte *)(LPCTSTR)csInStr, unLen);
	decryptor.Close();
	assert(p->CurrentState() == DefaultDecryptorWithMAC::MAC_GOOD);

	unsigned int unOutputLength = decryptor.MaxRetrieveable();
	szOutstr = new char[unOutputLength+1];
	decryptor.Get((byte *)szOutstr, unOutputLength);
	szOutstr[unOutputLength] = 0;

	CString csRet = szOutstr;
    delete szOutstr;
	return csRet;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -