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

📄 msrc4plugin.cpp

📁 DSM Plugin.加密文件
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//  Copyright (C) 2005 Sean E. Covel All Rights Reserved.
//
//  Created by Sean E. Covel based on UltraVNC's excellent TestPlugin project.
//
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
//  USA.
//
// If the source code for the program is not available from the place from
// which you received this file, check 
// http://home.comcast.net/~msrc4plugin
// or
// mail: msrc4plugin@comcast.net
//
//
//
/////////////////////////////////////////////////////////////////////////////
//  Copyright (C) 2005 Ultr@Vnc All Rights Reserved.
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
//  USA.
//
// If the source code for the program is not available from the place from
// which you received this file, check 
// http://ultravnc.sourceforge.net/
//
////////////////////////////////////////////////////////////////////////////
// IF YOU USE THIS GPL SOURCE CODE TO MAKE YOUR DSM PLUGIN, PLEASE ADD YOUR
// COPYRIGHT TO THIS FILE AND SHORTLY DESCRIBE/EXPLAIN THE MODIFICATIONS
// YOU'VE MADE. THANKS.
//
// IF YOU DON'T USE THIS CODE AS A BASE FOR YOUR PLUGIN, THE HEADER ABOVE AND
// ULTR@VNC COPYRIGHT SHOULDN'T BE FOUND IN YOUR PLUGIN SOURCE CODE.
//


//#include "stdafx.h"
#define _WIN32_WINNT 0x0410     //must be defined for the crypto api
#define _WIN32_WINDOWS 0x0410
#define WINVER 0x0400

#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <tchar.h>

#include <wincrypt.h>       //windows crypto api

#include "REGISTRY.h"
#include "MSRC4Plugin.h"
#include "crypto.h"
#include "utils.h"
#include "version.h"

static const TCHAR * INDEXVAL_LOG = _T("DebugLog");
static const TCHAR * INDEXVAL_KEYFILE = _T("KeyFile");

HINSTANCE hInstance = 0;
PLUGINSTRUCT* pPlugin = NULL;  // struct (or class instance) that handles all Plugin params.

char * sDefaultGenKey = "new_rc4.key";
char* sVariable = "msrc4pluginkey";
#define LOG_FILE  "msrc4.log"

//Salt flags
bool bSaltE = true;	//still need Salt/IV for Decryption
bool bSaltD = true; //still need Salt/IV for Encryption
bool bTransHeader = true;  //consider Salt/IV length in the calculation

int HeaderLen = SALT_SIZE + IV_SIZE; //length of additional header

// Plugin Description
// Please use the following format (with ',' (comma) as separator)
// Name,Version,Date,Author,FileName
// For the version, we recommend the following format: x.y.z
// The other fields (Name, Date, Authir, FileName) are format free (don't use ',' in them, of course)

#ifdef _WITH_REGISTRY
#define PLUGIN_FILE "MSRC4Plugin.dsm"
#define PLUGIN_DESCRIPTION  "MS RC4 Plugin,Sean E. Covel,Mar 12 2005," BUILD_NUMBER ",MSRC4Plugin.dsm"
#else
#define PLUGIN_FILE "MSRC4Plugin_noreg.dsm"
#define PLUGIN_DESCRIPTION  "MS RC4 NoReg Plugin,Sean E. Covel,Mar 12 2005," BUILD_NUMBER ",MSRC4Plugin_NoReg.dsm"
#endif

char getParams[BUFFER_SIZE];

// ----------------------------------------------------------------------
//
// A VNC DSM Plugin MUST export (extern "C" (__cdecl)) all the following
// functions (same names, same signatures)
//
// For return values, the rule is:
//    < 0, 0, and NULL pointer mean Error
//    > 0 and pointer != NULL mean Ok
//
// ----------------------------------------------------------------------
//

// Returns the ID string of the Plugin
//
PLUGIN_API char* Description(void)
{
    //PrintLog((DEST,"Plugin Description. %s",PLUGIN_DESCRIPTION));
    return PLUGIN_DESCRIPTION;
}


//
// Initialize the plugin and all its internals
// Return -1 if error
//

PLUGIN_API int Startup(void)
{
	char output[OUTPUT_BUFFER_SIZE];
    HANDLE      hKeyFile = 0;

#ifdef _WITH_REGISTRY
	char defaultKeyFile[BufSize];
	char defaultGenFile[BufSize];
	char sProgramFiles[KEYFILENAME_SIZE];
	char sLogit[BufSize];

		//get the local version of 'Program Files'
	GetEnvVar(PROGRAMFILES, sProgramFiles, BufSize);

		//add 'UltraVNC' to it.
	if (_snprintf(defaultKeyFile, sizeof(defaultKeyFile),"%s%s%s",sProgramFiles,sDefault,sDefaultKeyName) < 0)
		PrintLog((DEST,"_snprintf failed - defaultKeyFile too small"));
	if (_snprintf(defaultGenFile, sizeof(defaultGenFile),"%s%s%s",sProgramFiles,sDefault,sDefaultGenKey) < 0)
		PrintLog((DEST,"_snprintf failed - defaultGenFile too small"));

    char keyFile[KEYFILENAME_SIZE];

	//load some default values into the registry if the keys don't exist.
    REGISTRY *m_pREGISTRY;

    //HKLM Key
	m_pREGISTRY = new REGISTRY(HKEY_LOCAL_MACHINE, MSRC4_KEY_NAME_SERVER, true);
    m_pREGISTRY->ReadItem(keyFile, KEYFILENAME_SIZE,INDEXVAL_KEYFILE, defaultKeyFile);	//key file
    m_pREGISTRY->ReadItem(sLogit, 2,INDEXVAL_LOG, sLogItDef);							//debuglog
    m_pREGISTRY->ReadItem(keyFile, KEYFILENAME_SIZE,INDEXVAL_KEYGEN, defaultGenFile);	//GenKey
    delete m_pREGISTRY;
    
	//HKCU/Server
    m_pREGISTRY = new REGISTRY(HKEY_CURRENT_USER, MSRC4_KEY_NAME_SERVER, true);
    m_pREGISTRY->ReadItem(keyFile, KEYFILENAME_SIZE,INDEXVAL_KEYFILE, defaultKeyFile);	//key file
    m_pREGISTRY->ReadItem(sLogit, 2,INDEXVAL_LOG, sLogItDef);							//debuglog
    m_pREGISTRY->ReadItem(keyFile, KEYFILENAME_SIZE,INDEXVAL_KEYGEN, defaultGenFile);	//GenKey
    delete m_pREGISTRY;
	
	//HKCU/Viewer
    m_pREGISTRY = new REGISTRY(HKEY_CURRENT_USER, MSRC4_KEY_NAME_VIEWER, true);
    m_pREGISTRY->ReadItem(keyFile, KEYFILENAME_SIZE,INDEXVAL_KEYFILE, defaultKeyFile);	//key file
    m_pREGISTRY->ReadItem(sLogit, 2,INDEXVAL_LOG, sLogItDef);							//debuglog
    m_pREGISTRY->ReadItem(keyFile, KEYFILENAME_SIZE,INDEXVAL_KEYGEN, defaultGenFile);	//GenKey
    delete m_pREGISTRY;

	LOGIT = SetLogging(LOG_FILE, sLogit);
#else
	//set Logging based on an environment variable
	LOGIT = SetLogging(LOG_FILE);
#endif

    PrintLog((DEST,"StartUp"));
	//get some important info into the logs
#ifdef _WITH_REGISTRY
    PrintLog((DEST,"REGISTRY %s",PLUGIN_DESCRIPTION));
#else
    PrintLog((DEST,"NOREG %s",PLUGIN_DESCRIPTION));
#endif

    // Init everything
    memset(szExternalKey, 0, sizeof(szExternalKey));
    // Create threads if any

	if (FindKey(PLUGIN_FILE, sDefaultKeyName, sVariable, &hKeyFile, output))
		strcpy(getParams,"NothingNeeded");
	else
		strcpy(getParams,"VNCPasswordNeeded");

    return 1;
}


//
// Stop and Clean up the plugin 
// Return -1 if error
// 
PLUGIN_API int Shutdown(void)
{
    // Terminate Threads if any
    // Cleanup everything
	// Clean up: release handles, close files.
	
    //destroy the keys
    if (hKey) {
		CryptDestroyKey(hKey);
		CryptDestroyKey(hKey2);
    }
	
    if (hExchangeKey) {
		CryptDestroyKey(hExchangeKey);
		CryptDestroyKey(hExchangeKey2);
    }
	
	//We抮e finished using the CSP handle, so we must release it.
	if (hProvider) {
		CryptReleaseContext(hProvider, 0);
		CryptReleaseContext(hProvider2, 0);
	}

	PrintLog((DEST,"Shutting Down.\r\n"));
	
	return 1;
}


//
// Stop and Clean up the plugin 
// Return -1 if error
// 
PLUGIN_API int Reset(void)
{
int rc = 0;
	char output[OUTPUT_BUFFER_SIZE];
    HANDLE      hKeyFile = 0;

	//reset the salt flag so the password gets re-salted for the new session
	bSaltD = true;
	bSaltE = true;

	rc = ResetCrypto(hKey);
	rc = ResetCrypto(hKey2);

	PrintLog((DEST,"Reset Plugin."));

	if (FindKey(PLUGIN_FILE, sDefaultKeyName, sVariable, &hKeyFile, output))
		strcpy(getParams,"NothingNeeded");
	else
		strcpy(getParams,"VNCPasswordNeeded");

	CloseHandle(hKeyFile);


return 1;
}


//
// Set the plugin params (Key or password )
// If several params are needed, they can be transmitted separated with ',' (comma)
// then translated if necessary. They also can be taken from the internal Plugin config
// 
// WARNING: The plugin is responsible for implementing necessary GUI or File/Registry reading
// to acquire additionnal parameters and to ensure their persistence if necessary.
// Same thing for events/errors logging.
// 
// This function can be called 2 times, both from vncviewer and WinVNC:
// 
// 1.If the user clicks on the Plugin's "config" button in vncviewer and WinVNC dialog boxes
//   In this case this function is called with hVNC != 0 (CASE 1)
//
//   -> szParams is a string formatted as follow: "Part1,Part2"
//   Part1 = "NoPassword"
//   Part2 = type of application that has loaded the plugin
//     "viewer"     : for vncviewer
//     "server-svc" : for WinVNC run as a service
//     "server-app" : for WINVNC run as an application
//
//   -> The Plugin Config dialog box is displayed if any.
// 
// 2.When then plugin is Inited from VNC viewer or Server, right after Startup() call (CASE 2);
//   In this case, this function is called with hVNC = 0 and
//   szParams is a string formatted as follows: "part1,Part2"
//   Part1 = The VNC password, if required by the GetParams() function return value
//   Part2 = type of application that has loaded the plugin
//      "viewer"     : for vncviewer
//      "server-svc" : for WinVNC run as a service
//      "server-app" : for WINVNC run as an application
//   (this info can be used for application/environnement dependent
//    operations (config saving...))
//   
// 
PLUGIN_API int SetParams(HWND hVNC, char* szParams)
{
#ifdef _WITH_REGISTRY
    REGISTRY *m_pREGISTRY;
	int rc = 0;
#else
//	char sEnvVar[BufSize];
	DWORD rc = 0;
#endif

	char sLogit[BufSize];
	char output[OUTPUT_BUFFER_SIZE];
    HANDLE      hKeyFile = 0;
	//BYTE *		pbBuff;

	//BYTE		*pbBuffer = new BYTE[OUT_BUFFER_SIZE];
    //char        keyFile[KEYFILENAME_SIZE];
    PLUGINSTRUCT strPlugin;
	long iWinVer=0;
	long iCryptVer=0;
//	char *stop;
	char		sProgramFiles[KEYFILENAME_SIZE];
	char  CSPName[70];

	strPlugin.szHKLMServer[0] = '\0';
	strPlugin.szHKCUViewer[0] = '\0';
	strPlugin.szHKCUServer[0] = '\0';
	strPlugin.szHKCUGenKey[0] = '\0';

	//setup logging
#ifdef _WITH_REGISTRY
    m_pREGISTRY = new REGISTRY(HKEY_CURRENT_USER, MSRC4_KEY_NAME_VIEWER, false);
    m_pREGISTRY->ReadItem(sLogit, 2,INDEXVAL_LOG, sLogItDef);
    delete m_pREGISTRY;
	LOGIT = SetLogging(LOG_FILE, sLogit);
#else
	LOGIT = SetLogging(LOG_FILE);
#endif

//LOGIT = strtol( sLogit, &stop, 10 );

PrintLog((DEST,"Set Params"));

	//get some important info into the logs
#ifdef _WITH_REGISTRY
    PrintLog((DEST,"REGISTRY %s",PLUGIN_DESCRIPTION));
#else
    PrintLog((DEST,"NOREG %s",PLUGIN_DESCRIPTION));
#endif

	//figure out what OS, provider, CSP, yada yada, we are running on currently
	InitVars(CSPName, &iWinVer, &iCryptVer, &maxKeyLen);

	PrintLog((DEST,"Crypto Version = %d",iCryptVer));
	PrintLog((DEST,"OS is '%s'",WindowsName[iWinVer]));
	PrintLog((DEST,"Using provider '%s'",CSPName));
	PrintLog((DEST,"Max Key Length %d",(DWORD)(HIWORD(maxKeyLen))));

    // Get the environnement (szLoaderType) value that is always sent from 
    // VNC viewer or server
    MyStrToken(szLoaderType, szParams, 2, ',');

	GetEnvVar(PROGRAMFILES, sProgramFiles, BufSize);
	if (strlen(sProgramFiles)== 0)
		PrintLog((DEST,"ProgramFiles not found"));
    

// ***CASE 1 - CONFIG***
    // If hVNC != 0, display for instance the Plugin Config Dialog box 
    if (hVNC)
    {
        PrintLog((DEST,"SetParams. - Config."));
        
#ifdef _WITH_REGISTRY

		char defaultKeyFile[BufSize];
		char defaultGenFile[BufSize];

			//add 'UltraVNC' to it.
		if (_snprintf(defaultKeyFile, sizeof(defaultKeyFile),"%s%s%s",sProgramFiles,sDefault,sDefaultKeyName) < 0)
			PrintLog((DEST,"_snprintf failed - defaultKeyFile too small"));
		if (_snprintf(defaultGenFile, sizeof(defaultGenFile),"%s%s%s",sProgramFiles,sDefault,sDefaultGenKey) < 0)
			PrintLog((DEST,"_snprintf failed - defaultGenFile too small"));

		//load some default values into the registry if the keys don't exist.
		REGISTRY *m_pREGISTRY;

		//HKLM Key
		m_pREGISTRY = new REGISTRY(HKEY_LOCAL_MACHINE, MSRC4_KEY_NAME_SERVER, true);
		m_pREGISTRY->ReadItem(keyFile, KEYFILENAME_SIZE,INDEXVAL_KEYFILE, defaultKeyFile);	//key file
        strcpy(strPlugin.szHKLMServer,keyFile);

⌨️ 快捷键说明

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