Ayala 发表于 2017-3-23 15:39:37

【openssl】尝试将openssl转移到windows内核

本帖最后由 Ayala 于 2017-3-25 21:29 编辑

最小修改原则 先把openssl编译成静态库 自己修改路径 没使用vs 一堆警告 好坑

@echo off

set orgpath=%path%

set srcdir=F:\openssl-1.1.0e\
set tagdir=F:\openssl\
set perldir=C:\Perl64\site\bin;C:\Perl64\bin
set nasmdir="C:\Program Files (x86)\NASM"


set winddk=F:\WinDDK\7600.16385.1
set winsdk=F:\Microsoft SDKs\Windows
set incdir=%winddk%\inc\crt;%winddk%\inc\api;%winsdk%\v7.0A\include
set libdir_x86=%winddk%\lib\crt\i386\;%winddk%\lib\wxp\i386\;%winddk%\lib\wnet\i386\;%winsdk%\v10.0\lib
set libdir_x64=%winddk%\lib\crt\amd64\;%winddk%\lib\win7\amd64\;%winddk%\lib\wnet\amd64\;%winsdk%\v10.0\lib\amd64

set nmakedir=%winddk%\bin\x86
set cl_x86=%winddk%\bin\x86\x86
set cl_x64=%winddk%\bin\x86\amd64
set lib_x86=F:\wrk\tools\x86
set lib_x64=F:\wrk\tools\amd64

set args=no-filenames no-ui no-stdio --release -static


echo   cd %srcdir%
cd %srcdir%


:X86

echo '-------------------------------------
echo '                i386
echo '-------------------------------------
set path=%perldir%;%nasmdir%;%cl_x86%;%orgpath%;%nmakedir%;%lib_x86%
set include=%incdir%
set lib=%libdir_x86%

perl Configure VC-WIN32 --prefix=%tagdir%i386 %args%
nmake clean
nmake test
nmake install

:AMD64
echo '-------------------------------------
echo '                Amd64
echo '-------------------------------------

set path=%perldir%;%nasmdir%;%cl_x64%;%orgpath%;%nmakedir%;%lib_x64%
set include=%incdir%
set lib=%libdir_x64%

perl Configure VC-WIN64A --prefix=%tagdir%amd64 %args%
nmake clean
nmake test
nmake install



:done
pause
exit


开始写代码 先重载openssl需要的符号

/*openssl_lib.c 后续需要将函数具体实现 */
int main() {return 0;}
//__cdecl
int _close(){return 0;}
int _lseek(){return 0;}
int _write(){return 0;}
int _read(){return 0;}
int _open(){return 0;}
//int fflush(){return 0;}
int ftell(){return 0;}
int fseek(){return 0;}
int fopen(){return 0;}
int fclose(){return 0;}
int fwrite(){return 0;}
int fread(){return 0;}
int fgets(){return 0;}
int fprintf(){return 0;}
//int _iob(){return 0;}
int feof(){return 0;}
int ferror(){return 0;}
int clearerr(){return 0;}
int _fileno(){return 0;}
int _setmode(){return 0;}


//这里偷懒 为了兼容应该声明为函数原型 __stdcall api(args...);
int sendto(){return 0;}
int* __imp_sendto=(int *)&sendto;                              
int recvfrom(){return 0;}
int* __imp_recvfrom=(int *)&recvfrom;                              
int send(){return 0;}
int* __imp_send=(int *)&send;                                 
int WSASetLastError(){return 0;}
int* __imp_WSASetLastError=(int *)&WSASetLastError;                     
int recv(){return 0;}
int* __imp_recv=(int *)&recv;                                 
int shutdown(){return 0;}
int* __imp_shutdown=(int *)&shutdown;                              
int accept(){return 0;}
int* __imp_accept=(int *)&accept;                              
int closesocket(){return 0;}
int* __imp_closesocket=(int *)&closesocket;                           
int bind(){return 0;}
int* __imp_bind=(int *)&bind;                                 
int listen(){return 0;}
int* __imp_listen=(int *)&listen;                              
int connect(){return 0;}
int* __imp_connect=(int *)&connect;                              
int socket(){return 0;}
int* __imp_socket=(int *)&socket;                              
int getsockname(){return 0;}
int* __imp_getsockname=(int *)&getsockname;                           
int ioctlsocket(){return 0;}
int* __imp_ioctlsocket=(int *)&ioctlsocket;                           
int WSACleanup(){return 0;}
int* __imp_WSACleanup=(int *)&WSACleanup;                           
int freeaddrinfo(){return 0;}
int* __imp_freeaddrinfo=(int *)&freeaddrinfo;                        
int WSAStartup(){return 0;}
int* __imp_WSAStartup=(int *)&WSAStartup;                           
int gethostbyname(){return 0;}
int* __imp_gethostbyname=(int *)&gethostbyname;                        
int getsockopt(){return 0;}
int* __imp_getsockopt=(int *)&getsockopt;                           
int WSAGetLastError(){return 0;}
int* __imp_WSAGetLastError=(int *)&WSAGetLastError;                     
int getaddrinfo(){return 0;}
int* __imp_getaddrinfo=(int *)&getaddrinfo;                           
int getnameinfo(){return 0;}
int* __imp_getnameinfo=(int *)&getnameinfo;                           
int ntohs(){return 0;}
int* __imp_ntohs=(int *)&ntohs;                                 
int setsockopt(){return 0;}
int* __imp_setsockopt=(int *)&setsockopt;                           



int CryptGenRandom(){return 0;}
int* __imp_CryptGenRandom=(int *)&CryptGenRandom;                        
int CryptReleaseContext(){return 0;}
int* __imp_CryptReleaseContext=(int *)&CryptReleaseContext;                  
int RegisterEventSourceW(){return 0;}
int* __imp_RegisterEventSourceW=(int *)&RegisterEventSourceW;                  
int ReportEventW(){return 0;}
int* __imp_ReportEventW=(int *)&ReportEventW;                        
int DeregisterEventSource(){return 0;}
int* __imp_DeregisterEventSource=(int *)&DeregisterEventSource;               
int CryptAcquireContextW(){return 0;}
int* __imp_CryptAcquireContextW=(int *)&CryptAcquireContextW;                  
int SystemTimeToFileTime(){return 0;}
int* __imp_SystemTimeToFileTime=(int *)&SystemTimeToFileTime;                  
int GetSystemTime(){return 0;}
int* __imp_GetSystemTime=(int *)&GetSystemTime;                        
int GetLastError(){return 0;}
int* __imp_GetLastError=(int *)&GetLastError;                        
int SetLastError(){return 0;}
int* __imp_SetLastError=(int *)&SetLastError;                        
int GetProcAddress(){return 0;}
int* __imp_GetProcAddress=(int *)&GetProcAddress;                        
int GetModuleHandleW(){return 0;}
int* __imp_GetModuleHandleW=(int *)&GetModuleHandleW;                     
int GetVersion(){return 0;}
int* __imp_GetVersion=(int *)&GetVersion;                           
int MultiByteToWideChar(){return 0;}
int* __imp_MultiByteToWideChar=(int *)&MultiByteToWideChar;                  
int FormatMessageW(){return 0;}
int* __imp_FormatMessageW=(int *)&FormatMessageW;                        
int GetFileType(){return 0;}
int* __imp_GetFileType=(int *)&GetFileType;                           
int GetStdHandle(){return 0;}
int* __imp_GetStdHandle=(int *)&GetStdHandle;                        
int FreeLibrary(){return 0;}
int* __imp_FreeLibrary=(int *)&FreeLibrary;                           
int LoadLibraryA(){return 0;}
int* __imp_LoadLibraryA=(int *)&LoadLibraryA;                        
int CloseHandle(){return 0;}
int* __imp_CloseHandle=(int *)&CloseHandle;                           
int LoadLibraryW(){return 0;}
int* __imp_LoadLibraryW=(int *)&LoadLibraryW;                        
int GetModuleHandleExW(){return 0;}
int* __imp_GetModuleHandleExW=(int *)&GetModuleHandleExW;                  
int WideCharToMultiByte(){return 0;}
int* __imp_WideCharToMultiByte=(int *)&WideCharToMultiByte;                  
int FindNextFileW(){return 0;}
int* __imp_FindNextFileW=(int *)&FindNextFileW;                        
int CreateFiber(){return 0;}
int* __imp_CreateFiber=(int *)&CreateFiber;                           
int SwitchToFiber(){return 0;}
int* __imp_SwitchToFiber=(int *)&SwitchToFiber;                        
int DeleteFiber(){return 0;}
int* __imp_DeleteFiber=(int *)&DeleteFiber;                           
int ConvertThreadToFiber(){return 0;}
int* __imp_ConvertThreadToFiber=(int *)&ConvertThreadToFiber;                  
int ConvertFiberToThread(){return 0;}
int* __imp_ConvertFiberToThread=(int *)&ConvertFiberToThread;                  
int WriteFile(){return 0;}
int* __imp_WriteFile=(int *)&WriteFile;                           
int FindFirstFileW(){return 0;}
int* __imp_FindFirstFileW=(int *)&FindFirstFileW;                        
int FindClose(){return 0;}
int* __imp_FindClose=(int *)&FindClose;                           
int GetSystemTimeAsFileTime(){return 0;}
int* __imp_GetSystemTimeAsFileTime=(int *)&GetSystemTimeAsFileTime;               
int GetTickCount(){return 0;}
int* __imp_GetTickCount=(int *)&GetTickCount;                        
int Sleep(){return 0;}
int* __imp_Sleep=(int *)&Sleep;                                 
int RtlCaptureContext(){return 0;}
int* __imp_RtlCaptureContext=(int *)&RtlCaptureContext;                     
int RtlLookupFunctionEntry(){return 0;}
int* __imp_RtlLookupFunctionEntry=(int *)&RtlLookupFunctionEntry;               
int SetUnhandledExceptionFilter(){return 0;}
int* __imp_SetUnhandledExceptionFilter=(int *)&SetUnhandledExceptionFilter;         
int UnhandledExceptionFilter(){return 0;}
int* __imp_UnhandledExceptionFilter=(int *)&UnhandledExceptionFilter;            
int GetCurrentProcess(){return 0;}
int* __imp_GetCurrentProcess=(int *)&GetCurrentProcess;                     
int TerminateProcess(){return 0;}
int* __imp_TerminateProcess=(int *)&TerminateProcess;                     
int ExitProcess(){return 0;}
int* __imp_ExitProcess=(int *)&ExitProcess;                           
int ReadConsoleW(){return 0;}
int* __imp_ReadConsoleW=(int *)&ReadConsoleW;                        
int ReadConsoleA(){return 0;}
int* __imp_ReadConsoleA=(int *)&ReadConsoleA;                        
int SetConsoleMode(){return 0;}
int* __imp_SetConsoleMode=(int *)&SetConsoleMode;                        
int GetConsoleMode(){return 0;}
int* __imp_GetConsoleMode=(int *)&GetConsoleMode;                        
int GetCurrentThreadId(){return 0;}
int* __imp_GetCurrentThreadId=(int *)&GetCurrentThreadId;                  
int TlsFree(){return 0;}
int* __imp_TlsFree=(int *)&TlsFree;                              
int TlsSetValue(){return 0;}
int* __imp_TlsSetValue=(int *)&TlsSetValue;                           
int TlsGetValue(){return 0;}
int* __imp_TlsGetValue=(int *)&TlsGetValue;                           
int TlsAlloc(){return 0;}
int* __imp_TlsAlloc=(int *)&TlsAlloc;                              
int DeleteCriticalSection(){return 0;}
int* __imp_DeleteCriticalSection=(int *)&DeleteCriticalSection;               
int LeaveCriticalSection(){return 0;}
int* __imp_LeaveCriticalSection=(int *)&LeaveCriticalSection;                  
int EnterCriticalSection(){return 0;}
int* __imp_EnterCriticalSection=(int *)&EnterCriticalSection;                  
int InitializeCriticalSectionAndSpinCount(){return 0;}
int* __imp_InitializeCriticalSectionAndSpinCount=(int *)&InitializeCriticalSectionAndSpinCount;
int GetEnvironmentVariableW(){return 0;}
int* __imp_GetEnvironmentVariableW=(int *)&GetEnvironmentVariableW;               
int GlobalMemoryStatus(){return 0;}
int* __imp_GlobalMemoryStatus=(int *)&GlobalMemoryStatus;                  
int GetCurrentProcessId(){return 0;}
int* __imp_GetCurrentProcessId=(int *)&GetCurrentProcessId;                  
int QueryPerformanceCounter(){return 0;}
int* __imp_QueryPerformanceCounter=(int *)&QueryPerformanceCounter;               
int RtlVirtualUnwind(){return 0;}
int* __imp_RtlVirtualUnwind=(int *)&RtlVirtualUnwind;                     
int MessageBoxW(){return 0;}
int* __imp_MessageBoxW=(int *)&MessageBoxW;                           
int GetProcessWindowStation(){return 0;}
int* __imp_GetProcessWindowStation=(int *)&GetProcessWindowStation;               
int GetUserObjectInformationW(){return 0;}
int* __imp_GetUserObjectInformationW=(int *)&GetUserObjectInformationW;         


int FlushFileBuffers(){return 0;}
int* __imp_FlushFileBuffers=(int*)&FlushFileBuffers;      
int RtlUnwindEx(){return 0;}
int* __imp_RtlUnwindEx=(int*)&RtlUnwindEx;            
int HeapAlloc(){return 0;}
int* __imp_HeapAlloc=(int*)&HeapAlloc;               
int HeapFree(){return 0;}
int* __imp_HeapFree=(int*)&HeapFree;               
int OutputDebugStringA(){return 0;}
int* __imp_OutputDebugStringA=(int*)&OutputDebugStringA;      
int GetCPInfo(){return 0;}
int* __imp_GetCPInfo=(int*)&GetCPInfo;               
int GetACP(){return 0;}
int* __imp_GetACP=(int*)&GetACP;                  
int GetOEMCP(){return 0;}
int* __imp_GetOEMCP=(int*)&GetOEMCP;               
int EncodePointer(){return 0;}
int* __imp_EncodePointer=(int*)&EncodePointer;         
int DecodePointer(){return 0;}
int* __imp_DecodePointer=(int*)&DecodePointer;         
int FlsSetValue(){return 0;}
int* __imp_FlsSetValue=(int*)&FlsSetValue;            
int FlsGetValue(){return 0;}
int* __imp_FlsGetValue=(int*)&FlsGetValue;            
int GetModuleFileNameA(){return 0;}
int* __imp_GetModuleFileNameA=(int*)&GetModuleFileNameA;      
int LCMapStringW(){return 0;}
int* __imp_LCMapStringW=(int*)&LCMapStringW;            
int GetStringTypeW(){return 0;}
int* __imp_GetStringTypeW=(int*)&GetStringTypeW;         
int LoadLibraryExW(){return 0;}
int* __imp_LoadLibraryExW=(int*)&LoadLibraryExW;         
int VirtualProtect(){return 0;}
int* __imp_VirtualProtect=(int*)&VirtualProtect;         
int VirtualAlloc(){return 0;}
int* __imp_VirtualAlloc=(int*)&VirtualAlloc;            
int SetThreadStackGuarantee(){return 0;}
int* __imp_SetThreadStackGuarantee=(int*)&SetThreadStackGuarantee;
int GetSystemInfo(){return 0;}
int* __imp_GetSystemInfo=(int*)&GetSystemInfo;         
int VirtualQuery(){return 0;}
int* __imp_VirtualQuery=(int*)&VirtualQuery;            
int GetTimeZoneInformation(){return 0;}
int* __imp_GetTimeZoneInformation=(int*)&GetTimeZoneInformation;
int HeapReAlloc(){return 0;}
int* __imp_HeapReAlloc=(int*)&HeapReAlloc;            
int CompareStringW(){return 0;}
int* __imp_CompareStringW=(int*)&CompareStringW;         
int SetEnvironmentVariableA(){return 0;}
int* __imp_SetEnvironmentVariableA=(int*)&SetEnvironmentVariableA;
int FileTimeToSystemTime(){return 0;}
int* __imp_FileTimeToSystemTime=(int*)&FileTimeToSystemTime;   
int FileTimeToLocalFileTime(){return 0;}
int* __imp_FileTimeToLocalFileTime=(int*)&FileTimeToLocalFileTime;
int GetDriveTypeA(){return 0;}
int* __imp_GetDriveTypeA=(int*)&GetDriveTypeA;         
int FindFirstFileA(){return 0;}
int* __imp_FindFirstFileA=(int*)&FindFirstFileA;         
int HeapSize(){return 0;}
int* __imp_HeapSize=(int*)&HeapSize;               
int GetFullPathNameA(){return 0;}
int* __imp_GetFullPathNameA=(int*)&GetFullPathNameA;      
int GetCurrentDirectoryA(){return 0;}
int* __imp_GetCurrentDirectoryA=(int*)&GetCurrentDirectoryA;   
int SetFilePointer(){return 0;}
int* __imp_SetFilePointer=(int*)&SetFilePointer;         






#ifndef _OPENSSL_INC_H_
#define _OPENSSL_INC_H_
/*openssl_inc.c */

#include "openssl\aes.h"
#include "openssl\applink.c"
#include "openssl\asn1.h"
#include "openssl\asn1t.h"
//#include "openssl\asn1_mac.h"
#include "openssl\async.h"
#include "openssl\bio.h"
#include "openssl\blowfish.h"
#include "openssl\bn.h"
#include "openssl\buffer.h"
#include "openssl\camellia.h"
#include "openssl\cast.h"
#include "openssl\cmac.h"
#include "openssl\cms.h"
#include "openssl\comp.h"
#include "openssl\conf.h"
#include "openssl\conf_api.h"
#include "openssl\crypto.h"
#include "openssl\ct.h"
#include "openssl\des.h"
#include "openssl\dh.h"
#include "openssl\dsa.h"
#include "openssl\dtls1.h"
#include "openssl\ebcdic.h"
#include "openssl\ec.h"
#include "openssl\ecdh.h"
#include "openssl\ecdsa.h"
#include "openssl\engine.h"
#include "openssl\err.h"
#include "openssl\evp.h"
#include "openssl\e_os2.h"
#include "openssl\hmac.h"
#include "openssl\idea.h"
#include "openssl\kdf.h"
#include "openssl\lhash.h"
#include "openssl\md2.h"
#include "openssl\md4.h"
#include "openssl\md5.h"
#include "openssl\mdc2.h"
#include "openssl\modes.h"
#include "openssl\objects.h"
#include "openssl\obj_mac.h"
#include "openssl\ocsp.h"
#include "openssl\opensslconf.h"
#include "openssl\opensslv.h"
#include "openssl\ossl_typ.h"
#include "openssl\pem.h"
#include "openssl\pem2.h"
#include "openssl\pkcs12.h"
#include "openssl\pkcs7.h"
#include "openssl\rand.h"
#include "openssl\rc2.h"
#include "openssl\rc4.h"
#include "openssl\rc5.h"
#include "openssl\ripemd.h"
#include "openssl\rsa.h"
#include "openssl\safestack.h"
#include "openssl\seed.h"
#include "openssl\sha.h"
#include "openssl\srp.h"
#include "openssl\srtp.h"
#include "openssl\ssl.h"
#include "openssl\ssl2.h"
#include "openssl\ssl3.h"
#include "openssl\stack.h"
#include "openssl\symhacks.h"
#include "openssl\tls1.h"
#include "openssl\ts.h"
#include "openssl\txt_db.h"
#include "openssl\ui.h"
#include "openssl\whrlpool.h"
#include "openssl\x509.h"
#include "openssl\x509v3.h"
#include "openssl\x509_vfy.h"
//#include "openssl\__DECC_INCLUDE_EPILOGUE.H"
//#include "openssl\__DECC_INCLUDE_PROLOGUE.H"

#endif

简单的驱动

#include <ntdef.h>
#include <ntddk.h>

#include "openssl_inc.c"

#pragma comment(lib,"libcrypto.lib")
#pragma comment(lib,"libssl.lib")

int foo()
{
        SSL_CTX *ctx;
        SSL_library_init();
        OpenSSL_add_all_algorithms();
        SSL_load_error_strings();
        ctx=SSL_CTX_new(SSLv23_server_method());
        SSL_CTX_free(ctx);
}



NTSTATUS
DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegistryPath)
{
      return 0;
}



构建命令行

@echo off
:re
cls
echo /*********************************************/
echo /                                             /
echo /*********************************************/
set path=F:\WinDDK\7600.16385.1

set ddk=%path%\inc\ddk
set crt=%path%\inc\crt
set api=%path%\inc\api
set ssl=F:\openssl\amd64
set obj=.\obj\amd64\openssl_lib.obj

%path%\bin\x86\AMD64\cl.exe .\src\openssl_lib.c /Fp:strict /D"_AMD64_" /Fo"%obj%" /c



%path%\bin\x86\AMD64\cl.exe .\src\hello_world.c /Fp:strict /D"_AMD64_" /I"%crt%" /I"%api%" /I"%ddk%" /I "%ssl%\include" /Fo"Debug\hello_world.obj" /c

echo /*********************************************/
echo /                                             /
echo /*********************************************/


%path%\bin\x86\AMD64\link.exe %obj% .\Debug\hello_world.obj /LIBPATH:"%path%\lib\win7\amd64" /LIBPATH:"%path%\lib\Crt\amd64" /LIBPATH:"F:\openssl\amd64\lib" /OUT:"Debug\hello_world_amd64_win7.sys" /NOLOGO /SUBSYSTEM:native /driver /MACHINE:AMD64 ntoskrnl.lib libcrypto.lib libssl.lib /ENTRY:DriverEntry                                 
echo /*********************************************/
echo /                                             /
echo /*********************************************/


pause
goto re
;/driver /base:0x10000 /align:32 /subsystem:native

0xAA55 发表于 2018-1-12 12:16:59

一堆傀儡函数!

music 发表于 2020-7-22 20:59:34

这个是传说中的winssl吗?
页: [1]
查看完整版本: 【openssl】尝试将openssl转移到windows内核