Hosting CLR...

Just starting out? Need help? Post your questions and find answers here.
User avatar
CELTIC88
Enthusiast
Enthusiast
Posts: 154
Joined: Thu Sep 17, 2015 3:39 pm

Hosting CLR...

Post by CELTIC88 »

hi all,
i am a "c#" programmer and i want protect my application with pb using Hosting CLR .
i have tested with this code but without success, please tell me the right way?

thank you.

Code: Select all

Interface ICLRMetaHost Extends IUnknown
  GetRuntime.l(wstr,struct,ptr)
  GetVersionFromFile.l(ptr,ptr,ptr)
  EnumerateInstalledRuntimes.l(ptr)
  EnumerateLoadedRuntimes.l(ptr,ptr)
  RequestRuntimeLoadedNotification.l(ptr,ptr,ptr)
  QueryLegacyV2RuntimeBinding.l(ptr,ptr)
  ExitProcess.l(INT.l)
EndInterface

Interface ICLRRuntimeInfo Extends IUnknown
  GetVersionString.l(ptr,ptr)
  GetRuntimeDirectory.l(ptr,ptr)
  IsLoaded.l(ptr,ptr)
  LoadErrorString.l(ptr,ptr,ptr,ptr)
  LoadLibrary.l(ptr,ptr)
  GetProcAddress.l(ptr,ptr)
  GetInterface.l(ptr,ptr,ptr)
  IsLoadable.l(*Bool)
  SetDefaultStartupFlags.l(ptr,ptr)
  GetDefaultStartupFlags.l(ptr,ptr,ptr)
  BindAsLegacyV2Runtime.l()
  IsStarted.l(ptr,ptr)
EndInterface

Interface ICLRRuntimeHost Extends IUnknown
  Start.l()
  Stop.l()
  SetHostControl.l(ptr)
  GetCLRControl.l(*ptr)
  UnloadAppDomain.l(ptr,ptr)
  ExecuteInAppDomain.l(ptr,ptr,ptr)
  GetCurrentAppDomainId.l(ptr)
  ExecuteApplication.l(ptr,ptr,ptr,ptr,ptr,ptr)
  ExecuteInDefaultAppDomain.l(wstr,wstr,wstr,wstr,*ptr)
EndInterface

Interface ICorRuntimeHost Extends IUnknown
  CreateLogicalThreadState.l()
  DeleteLogicalThreadState.l()
  SwitchInLogicalThreadState.l()
  SwitchOutLogicalThreadState.l()
  LocksHeldByLogicalThread.l()
  MapFile.l()
  GetConfiguration.l()
  Start.l()
  Stop.l()
  CreateDomain.l()
  GetDefaultDomain.l(*ptr)
  EnumDomains.l()
  NextDomain.l()
  CloseEnum.l()
  CreateDomainEx.l()
  CreateDomainSetup.l()
  CreateEvidence.l()
  UnloadDomain.l()
  CurrentDomain.l()
EndInterface

Interface IAssembly Extends IDispatch
  get_ToString.l(*bstr)
  Equals.l()
  GetHashCode.l()
  GetType.l(*ptr)
  get_CodeBase.l()
  get_EscapedCodeBase.l()
  GetName.l()
  GetName_2.l()
  get_FullName.l(*bstr)
  get_EntryPoint.l(*ptr)
  GetType_2.i(bstr)
  GetType_3.l()
  GetExportedTypes.l()
  GetTypes.l(*ptr)
  GetManifestResourceStream.l()
  GetManifestResourceStream_2.l()
  GetFile.l()
  GetFiles.l()
  GetFiles_2.l()
  GetManifestResourceNames.l()
  GetManifestResourceInfo.l()
  get_Location.l()
  get_Evidence.l()
  GetCustomAttributes.l()
  GetCustomAttributes_2.l()
  IsDefined.l()
  GetObjectData.l()
  add_ModuleResolve.l()
  remove_ModuleResolve.l()
  GetType_4.l()
  GetSatelliteAssembly.l()
  GetSatelliteAssembly_2.l()
  LoadModule.l()
  LoadModule_2.l()
  CreateInstance.l(bstr,*variant)
  CreateInstance_2.l(bstr,bool.l,*variant)
  CreateInstance_3.l(bstr,bool.l,INT.l,ptr,ptr,ptr,ptr,*variant)
  GetLoadedModules.l()
  GetLoadedModules_2.l()
  GetModules.l()
  GetModules_2.l()
  GetModule.l()
  GetReferencedAssemblies.l()
  get_GlobalAssemblyCache.l()
EndInterface

Interface AppDomain Extends IUnknown
  GetTypeInfoCount.l()
  GetTypeInfo.l()
  GetIDsOfNames.l()
  Invoke.l()
  get_ToString.l()
  Equals.l()
  GetHashCode.l()
  GetType.l()
  InitializeLifetimeService.l()
  GetLifetimeService.l()
  get_Evidence.l()
  add_DomainUnload.l()
  remove_DomainUnload.l()
  add_AssemblyLoad.l()
  remove_AssemblyLoad.l()
  add_ProcessExit.l()
  remove_ProcessExit.l()
  add_TypeResolve.l()
  remove_TypeResolve.l()
  add_ResourceResolve.l()
  remove_ResourceResolve.l()
  add_AssemblyResolve.l()
  remove_AssemblyResolve.l()
  add_UnhandledException.l()
  remove_UnhandledException.l()
  DefineDynamicAssembly.l()
  DefineDynamicAssembly_2.l()
  DefineDynamicAssembly_3.l()
  DefineDynamicAssembly_4.l()
  DefineDynamicAssembly_5.l()
  DefineDynamicAssembly_6.l()
  DefineDynamicAssembly_7.l()
  DefineDynamicAssembly_8.l()
  DefineDynamicAssembly_9.l()
  CreateInstance.l()
  CreateInstanceFrom.l()
  CreateInstance_2.l()
  CreateInstanceFrom_2.l()
  CreateInstance_3.l()
  CreateInstanceFrom_3.l()
  Load.l()
  Load_2.l(bstr,*ptr)
  Load_3.l(ptr,*ptr)
  Load_4.l()
  Load_5.l()
  Load_6.l()
  Load_7.l()
  ExecuteAssembly.l()
  ExecuteAssembly_2.l()
  ExecuteAssembly_3.l()
  get_FriendlyName.l()
  get_BaseDirectory.l()
  get_RelativeSearchPath.l()
  get_ShadowCopyFiles.l()
  GetAssemblies.l()
  AppendPrivatePath.l()
  ClearPrivatePath.l()
  SetShadowCopyPath.l()
  ClearShadowCopyPath.l()
  SetCachePath.l()
  SetData.l()
  GetData.l()
  SetAppDomainPolicy.l()
  SetThreadPrincipal.l()
  SetPrincipalPolicy.l()
  DoCallBack.l()
  get_DynamicDirectory.l()
EndInterface

Interface IMethodInfo Extends IUnknown
  GetTypeInfoCount.l()
  GetTypeInfo.l()
  GetIDsOfNames.l()
  Invoke.l()
  ToString.l()
  Equals.l()
  GetHashCode.l()
  GetType.l()
  MemberType.l()
  name.l(*bstr)
  DeclaringType.l()
  ReflectedType.l()
  GetCustomAttributes.l()
  GetCustomAttributes_2.l()
  IsDefined.l()
  GetParameters.l()
  GetMethodImplementationFlags.l()
  MethodHandle.l()
  Attributes.l()
  CallingConvention.l()
  Invoke_2.l()
  IsPublic.l()
  IsPrivate.l()
  IsFamily.l()
  IsAssembly.l()
  IsFamilyAndAssembly.l()
  IsFamilyOrAssembly.l()
  IsStatic.l()
  IsFinal.l()
  IsVirtual.l()
  IsHideBySig.l()
  IsAbstract.l()
  IsSpecialName.l()
  IsConstructor.l()
  Invoke_3.l(variant.variant,ptr,*variant.variant)
  returnType.l()
  ReturnTypeCustomAttributes.l()
  GetBaseDefinition.l()
EndInterface

Interface IType Extends IDispatch
  get_ToString.l(*bstr)
  Equals.l(variant,*short)
  GetHashCode.l(*int)
  GetType.l(ptr)
  get_MemberType.l(ptr)
  get_name.l(*bstr)
  get_DeclaringType.l(ptr)
  get_ReflectedType.l(ptr)
  GetCustomAttributes.l(ptr,short.w,ptr)
  GetCustomAttributes_2.l(short.w,ptr)
  IsDefined.l(ptr,short.w,*short)
  get_Guid.l(ptr)
  get_Module.l(ptr)
  get_Assembly.l(*ptr)
  get_TypeHandle.l(ptr)
  get_FullName.l(*bstr)
  get_Namespace.l(*bstr)
  get_AssemblyQualifiedName.l(*bstr)
  GetArrayRank.l(*int)
  get_BaseType.l(ptr)
  GetConstructors.l(ptr,ptr)
  GetInterface.l(bstr,short.w,ptr)
  GetInterfaces.l(ptr)
  FindInterfaces.l(ptr,variant,ptr)
  GetEvent.l(bstr,ptr,ptr)
  GetEvents.l(ptr)
  GetEvents_2.l(INT.l,ptr)
  GetNestedTypes.l(INT.l,ptr)
  GetNestedType.l(bstr,ptr,ptr)
  GetMember.l(bstr,ptr,ptr,ptr)
  GetDefaultMembers.l(ptr)
  FindMembers.l(ptr,ptr,ptr,variant,ptr)
  GetElementType.l(ptr)
  IsSubclassOf.l(ptr,*short)
  IsInstanceOfType.l(variant,*short)
  IsAssignableFrom.l(ptr,*short)
  GetInterfaceMap.l(ptr,ptr)
  GetMethod.l(bstr,ptr,ptr,ptr,ptr,ptr)
  GetMethod_2.l(bstr,ptr,ptr)
  GetMethods.l(INT.l,ptr)
  GetField.l(bstr,ptr,ptr)
  GetFields.l(INT.l,ptr)
  GetProperty.l(bstr,ptr,ptr)
  GetProperty_2.l(bstr,ptr,ptr,ptr,ptr,ptr,ptr)
  GetProperties.l(ptr,ptr)
  GetMember_2.l(bstr,ptr,ptr)
  GetMembers.l(INT.l,*ptr)
  InvokeMember.l(bstr,ptr,ptr,variant,ptr,ptr,ptr,ptr,*variant)
  get_UnderlyingSystemType.l(ptr)
  InvokeMember_2.l(bstr,INT.l,ptr,variant,ptr,ptr,*variant)
  InvokeMember_3.l(bstr,INT.l,ptr,variant,ptr,*variant)
  GetConstructor.l(ptr,ptr,ptr,ptr,ptr,ptr)
  GetConstructor_2.l(ptr,ptr,ptr,ptr,ptr)
  GetConstructor_3.l(ptr,ptr)
  GetConstructors_2.l(ptr)
  get_TypeInitializer.l(ptr)
  GetMethod_3.l(bstr,ptr,ptr,ptr,ptr,ptr,ptr)
  GetMethod_4.l(bstr,ptr,ptr,ptr)
  GetMethod_5.l(bstr,ptr,ptr)
  GetMethod_6.l(bstr,ptr)
  GetMethods_2.l(ptr)
  GetField_2.l(bstr,ptr)
  GetFields_2.l(ptr)
  GetInterface_2.l(bstr,ptr)
  GetEvent_2.l(bstr,ptr)
  GetProperty_3.l(bstr,ptr,ptr,ptr,ptr)
  GetProperty_4.l(bstr,ptr,ptr,ptr)
  GetProperty_5.l(bstr,ptr,ptr)
  GetProperty_6.l(bstr,ptr,ptr)
  GetProperty_7.l(bstr,ptr)
  GetProperties_2.l(ptr)
  GetNestedTypes_2.l(ptr)
  GetNestedType_2.l(bstr,ptr)
  GetMember_3.l(bstr,ptr)
  GetMembers_2.l(ptr)
  get_Attributes.l(ptr)
  get_IsNotPublic.l(*short)
  get_IsPublic.l(*short)
  get_IsNestedPublic.l(*short)
  get_IsNestedPrivate.l(*short)
  get_IsNestedFamily.l(*short)
  get_IsNestedAssembly.l(*short)
  get_IsNestedFamANDAssem.l(*short)
  get_IsNestedFamORAssem.l(*short)
  get_IsAutoLayout.l(*short)
  get_IsLayoutSequential.l(*short)
  get_IsExplicitLayout.l(*short)
  get_IsClass.l(*short)
  get_IsInterface.l(*short)
  get_IsValueType.l(*short)
  get_IsAbstract.l(*short)
  get_IsSealed.l(*short)
  get_IsEnum.l(*short)
  get_IsSpecialName.l(*short)
  get_IsImport.l(*short)
  get_IsSerializable.l(*short)
  get_IsAnsiClass.l(*short)
  get_IsUnicodeClass.l(*short)
  get_IsAutoClass.l(*short)
  get_IsArray.l(*short)
  get_IsByRef.l(*short)
  get_IsPointer.l(*short)
  get_IsPrimitive.l(*short)
  get_IsCOMObject.l(*short)
  get_HasElementType.l(*short)
  get_IsContextful.l(*short)
  get_IsMarshalByRef.l(*short)
  Equals_2.l(ptr,*short)
EndInterface

EnableExplicit

Prototype.i CLRCreateInstance(*CLSID, *IID, *Inter)
Global mscoree_dll = OpenLibrary(#PB_Any, "mscoree.dll")
Global CLRCreateInstance.CLRCreateInstance = GetFunction(mscoree_dll, "CLRCreateInstance")

Procedure Load_Cs_PE(pDataFile, FileSize)
  Protected pClrHost.ICLRMetaHost
  Protected pRunInfo.ICLRRuntimeInfo
  Protected pRuntimeHost.ICLRRuntimeHost
  Protected pCorRuntimeHost.ICorRuntimeHost
  Protected pDomain.IUnknown
  Protected myAppDomain.AppDomain
  Protected loadedAssembly.IAssembly
  Protected pMethodInfo.IMethodInfo
  Protected fLoadable.l  
  
  CLRCreateInstance(?CLSID_CLRMetaHost,?IID_ICLRMetaHost,@pClrHost)
  pClrHost\GetRuntime(@"v4.0.30319",?IID_ICLRRuntimeInfo,@pRunInfo)
  pRunInfo\IsLoadable(@fLoadable)
  pRunInfo\GetInterface(?CLSID_CLRRuntimeHost,?IID_ICLRRuntimeHost,@pRuntimeHost)
  pRuntimeHost\Start()
  pRunInfo\GetInterface(?CLSID_CorRuntimeHost,?IID_ICorRuntimeHost,@pCorRuntimeHost)
  pCorRuntimeHost\Start()
  pCorRuntimeHost\GetDefaultDomain(@pDomain)
  pDomain\QueryInterface(?IID_AppDomain,@myAppDomain)                   
  
  Protected rgsabound.SAFEARRAYBOUND
  rgsabound\lLBound = 0
  rgsabound\cElements = FileSize
  Protected psa = SafeArrayCreate_(#VT_UI1, 1, @rgsabound)
  Protected ppData.i
  SafeArrayAccessData_(psa, @ppData)
  CopyMemory(pDataFile, ppdata, FileSize)
  SafeArrayUnaccessData_(psa)
  
  Debug myAppDomain\Load_3(psa,@loadedAssembly)
  
  Protected pBSR.string
  loadedAssembly\get_FullName(pBSR)
  Debug pBSR\s                  
  
  
  loadedAssembly\get_EntryPoint(@pMethodInfo) 
  pMethodInfo\name(pBSR)
  Debug pBSR\s
  
  Protected retVal.VARIANT
  Protected obj.VARIANT\vt = #VT_NULL

  Debug pMethodInfo\Invoke_3(obj,SafeArrayCreateVector_(#VT_VARIANT, 0, 0),@retVal)
  MessageRequester("","")
  
EndProcedure

DisableExplicit
NetFile.s = "n.exe"
FileSize =FileSize(NetFile)
pDataFile =AllocateMemory(FileSize*100)
ReadFile(0,NetFile)
ReadData(0,pDataFile,FileSize)
CloseFile(0)

Load_Cs_PE(pDataFile, FileSize)

DataSection
  IID_ICLRMetaHost:
  Data.l $D332DB9E
  Data.w $B9B3, $4125
  Data.b $82, $07, $A1, $48, $84, $F5, $32, $16 
  
  CLSID_CLRMetaHost: 
  Data.l $9280188d
  Data.w $e8e, $4867
  Data.b $b3, $c, $7f, $a8, $38, $84, $e8, $de
  
  IID_ICLRRuntimeInfo:
  Data.l $BD39D1D2
  Data.w $BA2F, $486a
  Data.b $89, $B0, $B4, $B0, $CB, $46, $68, $91
  
  CLSID_CLRRuntimeHost:
  Data.l $90F1A06E
  Data.w $7712,$4762
  Data.b $86,$B5,$7A,$5E,$BA,$6B,$DB,$02
  
  IID_ICLRRuntimeHost:
  Data.l $90F1A06C
  Data.w $7712,$4762
  Data.b $86,$B5,$7A,$5E,$BA,$6B,$DB,$02
  
  CLSID_CorRuntimeHost:
  Data.l $cb2f6723
  Data.w $ab3a, $11d2
  Data.b $9c, $40, $00, $c0, $4f, $a3, $0a, $3e 
  
  IID_ICorRuntimeHost:
  Data.l $CB2F6722
  Data.w $AB3A,$11D2
  Data.b $9C,$40,$0,$C0,$4F,$A3,$A,$3E
  
  IID_AppDomain:
  Data.l $05F696DC
  Data.w $2B29,$3663
  Data.b $AD,$8B,$C4,$38,$9C,$F2,$A7,$13
  
  IID_MethodInfo:
  Data.l $FFCC1B5D
  Data.w $ECB8,$38DD
  Data.b $9B,$01,$3D,$C8,$AB,$C2,$AA,$5F
  
  IID_IAssembly:
  Data.l $17156360
  Data.w $2F1A
  Data.w $384A
  Data.b $BC,$52,$FD,$E9,$3C,$21,$5C,$5B
EndDataSection
interested in Cybersecurity..
User avatar
Rings
Moderator
Moderator
Posts: 1427
Joined: Sat Apr 26, 2003 1:11 am

Re: Hosting CLR...

Post by Rings »

please use better names for variables, not the same as
native PB commands.
And check if the file is already there and can be loaded.

Code: Select all

NetFile.s = "n.exe"
FileSize1 =FileSize(NetFile)
If FileSize1<1
  Debug "file not found !"
  End
EndIf

pDataFile =AllocateMemory(FileSize1*100)
ReadFile(0,NetFile)
ReadData(0,pDataFile,FileSize1)
CloseFile(0)

Load_Cs_PE(pDataFile, FileSize1)
SPAMINATOR NR.1
0x90
New User
New User
Posts: 2
Joined: Thu Mar 15, 2018 6:18 pm

Re: Hosting CLR...

Post by 0x90 »

Rings wrote:please use better names for variables, not the same as
native PB commands.
And check if the file is already there and can be loaded.

Code: Select all

NetFile.s = "n.exe"
FileSize1 =FileSize(NetFile)
If FileSize1<1
  Debug "file not found !"
  End
EndIf

pDataFile =AllocateMemory(FileSize1*100)
ReadFile(0,NetFile)
ReadData(0,pDataFile,FileSize1)
CloseFile(0)

Load_Cs_PE(pDataFile, FileSize1)
Has anyone managed to get this to work?
Post Reply