예제 #1
0
bool SuInstallDriverInner(bool force)
{
    wchar_t sys_fullpath[MAX_PATH];
    UINT current_sl_ver = 0;
    bool ret = false;
    wchar_t src_cat[MAX_PATH];
    wchar_t src_inf[MAX_PATH];
    wchar_t src_sys[MAX_PATH];
    wchar_t dst_cat[MAX_PATH];
    wchar_t dst_inf[MAX_PATH];
    wchar_t dst_sys[MAX_PATH];
    wchar_t tmp_dir[MAX_PATH];
    char *cpu_type = MsIsX64() ? "x64" : "x86";

    if (SuIsSupportedOs(true) == false)
    {
        // Unsupported OS
        return false;
    }

    CombinePathW(tmp_dir, sizeof(tmp_dir), MsGetWindowsDirW(), L"Temp");
    MakeDirExW(tmp_dir);

    UniStrCat(tmp_dir, sizeof(tmp_dir), L"\\selowtmp");
    MakeDirExW(tmp_dir);

    // Confirm whether the driver is currently installed
    CombinePathW(sys_fullpath, sizeof(sys_fullpath), MsGetSystem32DirW(), L"drivers\\SeLow_%S.sys");
    UniFormat(sys_fullpath, sizeof(sys_fullpath), sys_fullpath, cpu_type);

    if (IsFileExistsW(sys_fullpath))
    {
        char *path;

        // Read the current version from the registry
        current_sl_ver = MsRegReadIntEx2(REG_LOCAL_MACHINE, SL_REG_KEY_NAME,
                                         (MsIsWindows10() ? SL_REG_VER_VALUE_WIN10 : SL_REG_VER_VALUE),
                                         false, true);

        path = MsRegReadStrEx2(REG_LOCAL_MACHINE, SL_REG_KEY_NAME, "ImagePath", false, true);

        if (IsEmptyStr(path) || IsFileExists(path) == false || MsIsServiceInstalled(SL_PROTOCOL_NAME) == false)
        {
            current_sl_ver = 0;
        }

        Free(path);
    }

    if (force == false && current_sl_ver >= SL_VER)
    {
        // Newer version has already been installed
        Debug("Newer SeLow is Installed. %u >= %u\n", current_sl_ver, SL_VER);
        return true;
    }

    // Copy necessary files to a temporary directory
    UniFormat(src_sys, sizeof(src_sys), L"|DriverPackages\\%S\\%S\\SeLow_%S.sys",
              (MsIsWindows10() ? "SeLow_Win10" : "SeLow_Win8"),
              cpu_type, cpu_type);
    if (MsIsWindows8() == false)
    {
        // Windows Vista and Windows 7 uses SHA-1 catalog files
        UniFormat(src_cat, sizeof(src_cat), L"|DriverPackages\\SeLow_Win8\\%S\\inf.cat", cpu_type);
    }
    else
    {
        // Windows 8 or above uses SHA-256 catalog files
        UniFormat(src_cat, sizeof(src_cat), L"|DriverPackages\\SeLow_Win8\\%S\\inf2.cat", cpu_type);

        if (MsIsWindows10())
        {
            // Windows 10 uses WHQL catalog files
            UniFormat(src_cat, sizeof(src_cat), L"|DriverPackages\\SeLow_Win10\\%S\\SeLow_Win10_%S.cat", cpu_type, cpu_type);
        }
    }
    UniFormat(src_inf, sizeof(src_inf), L"|DriverPackages\\%S\\%S\\SeLow_%S.inf",
              (MsIsWindows10() ? "SeLow_Win10" : "SeLow_Win8"),
              cpu_type, cpu_type);

    UniFormat(dst_sys, sizeof(dst_cat), L"%s\\SeLow_%S.sys", tmp_dir, cpu_type);
    UniFormat(dst_cat, sizeof(dst_cat), L"%s\\SeLow_%S_%S.cat", tmp_dir,
              (MsIsWindows10() ? "Win10" : "Win8"),
              cpu_type);

    UniFormat(dst_inf, sizeof(dst_inf), L"%s\\SeLow_%S.inf", tmp_dir, cpu_type);

    if (FileCopyW(src_sys, dst_sys) &&
            FileCopyW(src_cat, dst_cat) &&
            FileCopyW(src_inf, dst_inf))
    {
        NO_WARNING *nw;

        nw = MsInitNoWarningEx(SL_USER_AUTO_PUSH_TIMER);

        if (MsIsWindows10())
        {
            if (MsIsServiceInstalled(SL_PROTOCOL_NAME) == false && MsIsServiceRunning(SL_PROTOCOL_NAME) == false)
            {
                // On Windows 10, if there are no SwLow service installed, then uinstall the protocol driver first.
                // TODO: currently do nothing. On some versions of Windows 10 beta builds it is necessary to do something...
            }
        }

        if (MsIsWindows10())
        {
            // Delete garbage INFs
            SuDeleteGarbageInfs();
        }

        // Call the installer
        if (InstallNdisProtocolDriver(dst_inf, L"SeLow", SL_USER_INSTALL_LOCK_TIMEOUT) == false)
        {
            Debug("InstallNdisProtocolDriver Error.\n");
        }
        else
        {
            Debug("InstallNdisProtocolDriver Ok.\n");

            // Copy manually because there are cases where .sys file is not copied successfully for some reason
            FileCopyW(src_sys, sys_fullpath);

            ret = true;

            // Write the version number into the registry
            MsRegWriteIntEx2(REG_LOCAL_MACHINE, SL_REG_KEY_NAME,
                             (MsIsWindows10() ? SL_REG_VER_VALUE_WIN10 : SL_REG_VER_VALUE),
                             SL_VER, false, true);

            // Set to automatic startup
            MsRegWriteIntEx2(REG_LOCAL_MACHINE, SL_REG_KEY_NAME, "Start", SERVICE_SYSTEM_START, false, true);
        }

        MsFreeNoWarning(nw);
    }
    else
    {
        Debug("Fail Copying Files.\n");
    }

    if (ret)
    {
        // If the service is installed this time, start and wait until the enumeration is completed
        SuFree(SuInitEx(180 * 1000));
    }

    return ret;
}
예제 #2
0
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, char *CmdLine, int CmdShow)
{
	bool uninstall_mode = false;
	char exe_name[MAX_PATH];
	char exe_dir[MAX_PATH];
	UINT os_ver = GetWindowsVersion();

	CoInitialize(NULL);

	DisableWow64FsRedirection();

	if (SeStartWith(CmdLine, "/uninstall"))
	{
		uninstall_mode = true;
	}

	GetModuleFileNameA(hInst, exe_name, sizeof(exe_name));
	SeGetDirNameFromFilePath(exe_dir, sizeof(exe_dir), exe_name);

	if (uninstall_mode == false)
	{
		char driver_inf_filename[MAX_PATH] = {0};
		bool install_driver = false;

		// Check the Windows version
		if (os_ver == OS_UNKNOWN)
		{
			MessageBoxA(NULL, "This operating system is not supported by Win10Pcap.\r\n\r\n"
				"Win10Pcap requires Windows 7, Server 2008 R2, Windows 8, Windows 8.1, Windows Server 2012, Windows Server 2012 R2 or Windows 10.",
				INSTALLER_TITLE,
				MB_ICONSTOP | MB_SYSTEMMODAL);
			return -1;
		}

		SeStrCpy(driver_inf_filename, sizeof(driver_inf_filename), exe_dir);
		if (os_ver == OS_WIN10)
		{
			SeStrCat(driver_inf_filename, sizeof(driver_inf_filename), "\\drivers\\win10");
		}
		else
		{
			SeStrCat(driver_inf_filename, sizeof(driver_inf_filename), "\\drivers\\win78");
		}
		SeStrCat(driver_inf_filename, sizeof(driver_inf_filename), "\\Win10Pcap.inf");

		// Install the device driver
		if (Is64BitCode())
		{
			// x64
			install_driver = true;
		}
		else if (IsWow64() == false)
		{
			// x86
			install_driver = true;
		}
		else
		{
			// Do nothing.
		}

		if (install_driver)
		{
LABEL_RETRY_INSTALL_DRIVER:

			if (InstallNdisProtocolDriver(driver_inf_filename, L"Win10Pcap", 60 * 1000) == false)
			{
				if (MessageBoxA(NULL, "The install process of the Win10Pcap NDIS device driver failed.", 
					INSTALLER_TITLE,
					MB_ICONEXCLAMATION | MB_SYSTEMMODAL | MB_RETRYCANCEL) == IDRETRY)
				{
					goto LABEL_RETRY_INSTALL_DRIVER;
				}
				else
				{
					return -1;
				}
			}

			MsStartService("Win10Pcap");
		}

		if (InstallDllToSystem32(exe_dir, "Packet.dll", "Packet.dll") == false ||
			InstallDllToSystem32(exe_dir, "wpcap.dll", "wpcap.dll") == false)
		{
			return -1;
		}

		if (Is64BitCode() == false && Is64BitWindows())
		{
			// Run x64
			char x64_exe[MAX_PATH];

			wsprintfA(x64_exe, "%s\\..\\x64\\Installer.exe", exe_dir);

			Win32RunAndWaitProcess(x64_exe, CmdLine, false, false, INFINITE);
		}
	}
	else
	{
		bool uninstall_driver = false;

		UninstallDllFromSystem32("Packet.dll");
		UninstallDllFromSystem32("wpcap.dll");

		// Install the device driver
		if (Is64BitCode())
		{
			// x64
			uninstall_driver = true;
		}
		else if (IsWow64() == false)
		{
			// x86
			uninstall_driver = true;
		}
		else
		{
			// Do nothing.
		}

		if (uninstall_driver)
		{
LABEL_RETRY_UNINSTALL_DRIVER:
			if (UninstallNdisProtocolDriver(L"Win10Pcap", 60 * 1000) == false)
			{
				if (MessageBoxA(NULL, "The uninstall process of the Win10Pcap NDIS device driver failed.", 
					INSTALLER_TITLE,
					MB_ICONEXCLAMATION | MB_SYSTEMMODAL | MB_RETRYCANCEL) == IDRETRY)
				{
					goto LABEL_RETRY_UNINSTALL_DRIVER;
				}
			}
		}

		if (Is64BitCode() == false && Is64BitWindows())
		{
			// Run x64
			char x64_exe[MAX_PATH];

			wsprintfA(x64_exe, "%s\\..\\x64\\Installer.exe", exe_dir);

			Win32RunAndWaitProcess(x64_exe, CmdLine, false, false, INFINITE);
		}
	}

	CoUninitialize();

	return 0;
}