Infralution Support Forum Index Infralution Support
Support groups for Infralution products
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

WindowsHooks - Attached Code works standalone, not Encrypted

 
Post new topic   Reply to topic    Infralution Support Forum Index -> .NET Encryptor Support
View previous topic :: View next topic  
Author Message
tb-net



Joined: 10 May 2012
Posts: 3

PostPosted: Thu May 10, 2012 4:53 am    Post subject: WindowsHooks - Attached Code works standalone, not Encrypted Reply with quote

The following Code works perfectly, as long it is not packed with .NetEcryptor.
Please let me know what to do to get it working and encrypted:

Code:

Option Strict On
Option Explicit On
Option Infer Off

Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Windows.Forms
Imports System.ComponentModel

Public Class MouseTracker
    Private Delegate Function MouseHookCallback(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer

    Private MouseHookCallbackDelegate As MouseHookCallback
    Private MouseHookID As Integer

    Public Event MouseButtonStateChanged(ByVal sender As Object, ByVal args As Mouse.MouseTrackingEventArgs)
    Public Event MousePositionChanged(ByVal sender As Object, ByVal args As Mouse.MouseTrackingEventArgs)

#Region "WINAPI"

    <DllImport> _
    Private Shared Function CallNextHookEx( _
         ByVal idHook As Integer, _
         ByVal nCode As Integer, _
         ByVal wParam As IntPtr, _
         ByVal lParam As IntPtr) As Integer

    End Function

    <DllImport> _
    Private Shared Function SetWindowsHookEx _
          (ByVal idHook As Integer, ByVal HookProc As MouseHookCallback, _
           ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
    End Function

    <DllImport> _
    Private Shared Function UnhookWindowsHookEx(ByVal idHook As Integer) As Integer
    End Function

    <StructLayout> _
    Private Structure Point
        Public x As Integer
        Public y As Integer
    End Structure

    <StructLayout> _
    Private Structure MouseHookStruct
        Public pt As Point
        Public hwnd As Integer
        Public wHitTestCode As Integer
        Public dwExtraInfo As Integer
    End Structure

    Private Enum MouseMessages
        WM_LeftButtonDown = &H201
        WM_LeftButtonUp = &H202
        WM_LeftDblClick = &H203
        WM_RightButtonDown = &H204
        WM_RightButtonUp = &H205
        WM_RightDblClick = &H206
        WM_MiddleButtonDown = &H207
        WM_MiddleButtonUp = &H208
        WM_MiddleDblClick = &H209
        WM_MouseMove = &H200
        WM_MouseWheel = &H20E
    End Enum

#End Region

    Public Sub New()
        If Me.MouseHookID = 0 Then
            Me.MouseHookCallbackDelegate = AddressOf Me.MouseHookProc
           
          [b]  'This was the original line, however, not knowing too much about the encrypted module structure, I changed it to the following passage
                 Both is not doing, what it does unencrypted.
            'Me.MouseHookID = SetWindowsHookEx(CInt(14), Me.MouseHookCallbackDelegate, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)), 0)[/b]

            [i]Dim assemblyName As String = My.Application.Info.AssemblyName & ".exe"
            Dim modules As System.Reflection.Module() = Assembly.GetExecutingAssembly.GetModules()
            Dim asssembly As System.Reflection.Module
            asssembly = Array.Find(modules, Function(m) m.Name = assemblyName)
            Me.MouseHookID = SetWindowsHookEx(CInt(14), Me.MouseHookCallbackDelegate, Marshal.GetHINSTANCE(asssembly), 0)[/i]

            If Me.MouseHookID = 0 Then                                          'Not hooked, throw Error
                Dim traceEx As New WarningException(Environment.StackTrace)
                Dim warningEx As New WarningException("Could not hook into Windows!", traceEx)
                Throw New TypeInitializationException("MouseTracker", warningEx)
               
                'Here is fun, too. Application will NOT exit, if encrypted, and hooking fails :-)
                'StandAlone, the App exits.
                [b]Application.Exit()[/b]

            End If
        End If
    End Sub

    Public Sub Dispose()
        If Not Me.MouseHookID = -1 Then
            Call MouseTracker.UnhookWindowsHookEx(Me.MouseHookID)
            Me.MouseHookCallbackDelegate = Nothing
        End If
        Me.MouseHookID = -1
    End Sub

    Private Function MouseHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
        If nCode < 0 Then
            Return MouseTracker.CallNextHookEx(Me.MouseHookID, nCode, CType(wParam, IntPtr), lParam)
        End If

        Dim mouseData As MouseHookStruct = CType(Marshal.PtrToStructure(lParam, GetType(MouseTracker.MouseHookStruct)), MouseTracker.MouseHookStruct)

        Select Case wParam
            Case MouseMessages.WM_MouseMove
                Dim args As New Mouse.MouseTrackingEventArgs(Mouse.MouseTrackingActionEnum.Move, mouseData.pt.x, mouseData.pt.y)
                RaiseEvent MousePositionChanged(Nothing, args)

            Case MouseMessages.WM_LeftButtonDown
                Dim args As New Mouse.MouseTrackingEventArgs(Mouse.MouseTrackingActionEnum.LeftDown, mouseData.pt.x, mouseData.pt.y)
                RaiseEvent MouseButtonStateChanged(Nothing, args)

            Case MouseMessages.WM_LeftButtonUp
                Dim args As New Mouse.MouseTrackingEventArgs(Mouse.MouseTrackingActionEnum.LeftUp, mouseData.pt.x, mouseData.pt.y)
                RaiseEvent MouseButtonStateChanged(Nothing, args)

            Case MouseMessages.WM_RightButtonDown
                Dim args As New Mouse.MouseTrackingEventArgs(Mouse.MouseTrackingActionEnum.RightDown, mouseData.pt.x, mouseData.pt.y)
                RaiseEvent MouseButtonStateChanged(Nothing, args)

            Case MouseMessages.WM_RightButtonUp
                Dim args As New Mouse.MouseTrackingEventArgs(Mouse.MouseTrackingActionEnum.RightUp, mouseData.pt.x, mouseData.pt.y)
                RaiseEvent MouseButtonStateChanged(Nothing, args)

            Case MouseMessages.WM_MiddleButtonDown
                Dim args As New Mouse.MouseTrackingEventArgs(Mouse.MouseTrackingActionEnum.MiddleDown, mouseData.pt.x, mouseData.pt.y)
                RaiseEvent MouseButtonStateChanged(Nothing, args)

            Case MouseMessages.WM_MiddleButtonUp
                Dim args As New Mouse.MouseTrackingEventArgs(Mouse.MouseTrackingActionEnum.MiddleUp, mouseData.pt.x, mouseData.pt.y)
                RaiseEvent MouseButtonStateChanged(Nothing, args)

        End Select

        Return MouseTracker.CallNextHookEx(Me.MouseHookID, nCode, CType(wParam, IntPtr), lParam)
    End Function

End Class
Back to top
View user's profile Send private message
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Thu May 10, 2012 12:40 pm    Post subject: Reply with quote

The hooking code may be being blocked by the anti-hooking and dumping algorithms that .NET Encryptor implements. You can try turning this down by reducing the level parameter passed to the CheckProcessIntegrity call. If the code still doesn't work with the level set to 0 then if you can put together a small sample project and email it to support@infralution.com we will take a look.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
tb-net



Joined: 10 May 2012
Posts: 3

PostPosted: Fri May 25, 2012 12:17 am    Post subject: Reply with quote

Infralution wrote:
The hooking code may be being blocked by the anti-hooking and dumping algorithms that .NET Encryptor implements. You can try turning this down by reducing the level parameter passed to the CheckProcessIntegrity call. If the code still doesn't work with the level set to 0 then if you can put together a small sample project and email it to support@infralution.com we will take a look.


Well, I marked two passages in my text.
You did not read it, did you?

Anyway, those marks should indicate the suspected code area.
You did not, I did find a solution:

Code:

            'works
            Dim modul As Int32 = System.Diagnostics.Process.GetCurrentProcess().MainModule.BaseAddress.ToInt32()
            Me.MouseHookID = SetWindowsHookEx(CInt(14), Me.MouseHookCallbackDelegate, CType(modul, IntPtr), 0)

            'works not
            Dim modul As Int32 = Assembly.GetExecutingAssembly.GetModules()(0)
            Me.MouseHookID = SetWindowsHookEx(CInt(14), Me.MouseHookCallbackDelegate, Marshal.GetHINSTANCE(modul), 0)


Important to mention, that

Code:

Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0))


is considered the best practise and published all over the Web?

Please make this public.
_________________
It's dead, Jim...
Back to top
View user's profile Send private message
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Fri May 25, 2012 12:39 am    Post subject: Reply with quote

Quote:
Well, I marked two passages in my text.
You did not read it, did you?

Anyway, those marks should indicate the suspected code area.
You did not, I did find a solution:


I'm not quite sure what you are getting at. Yes we did read your code and since it is using windows hooking we suggested that it might be an interaction with the code in .NET Encryptor that prevents other applications hooking your application and suggested a workaround. However since we are not intimately familiar with your code it makes our job tracking down an issue much easier if you can send us a small working project that demonstrates the problem.

If you are actually trying to hook the user interface thread within your own process then you can just pass SetWindowsHookEx null for the Module parameter like:

Code:
Me.MouseHookID = SetWindowsHookEx(CInt(14), Me.MouseHookCallbackDelegate, 0, AppDomain.GetCurrentThreadId());


We use this technique for windows hooking within our own projects and I know it is compatible with .NET Encryptor.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
tb-net



Joined: 10 May 2012
Posts: 3

PostPosted: Fri May 25, 2012 3:00 pm    Post subject: Reply with quote

Quote:

I'm not quite sure what you are getting at.


I did not want to annoy You; sorry for this.
I just was 'enthusiastic' Wink

However, setting down security was not my favorite workaround; I'd rather get knowledge about what leads to the problem.

Quote:

If you are actually trying to hook the user interface thread within your own process then you can just pass SetWindowsHookEx null for the Module parameter like:

Code:
Me.MouseHookID = SetWindowsHookEx(CInt(14), Me.MouseHookCallbackDelegate, 0, AppDomain.GetCurrentThreadId());


We use this technique for windows hooking within our own projects and I know it is compatible with .NET Encryptor.


Thank You for this, the last day (and night) I was busy doing MY homework (I didn't know wot I's doing, really Smile)

I must admit, the reason, why
Code:

System.Diagnostics.Process.GetCurrentProcess().MainModule.BaseAddress.ToInt32()

works with .NetEncryptor, but
Code:

Assembly.GetExecutingAssembly.GetModules()(0)

not, still is not really obvious to me...
_________________
It's dead, Jim...
Back to top
View user's profile Send private message
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Sat May 26, 2012 1:47 am    Post subject: Reply with quote

I think the reason is the related to the technique that .NET Encryptor uses to determine whether the hook is from the process itself (which is allowed) or from another process which .NET Encryptor blocks.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Infralution Support Forum Index -> .NET Encryptor Support All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group