Antivirus hurt by functions pasting text on clipboard - sometimes

General FreeBASIC programming questions.
Post Reply
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Antivirus hurt by functions pasting text on clipboard - sometimes

Post by Tourist Trap »

Hello,

Does someone see why Kaspersky on Win10 gets angered with this (dodi's one) version :

Code: Select all

#include once "windows.bi"

Sub SaveToClipBoard(Byref Text As String)
'https://www.freebasic.net/forum/viewtopic.php?p=210732#p210732
    Var Num=GlobalAlloc(0,Len(Text)+1)
    Var Chars=GlobalLock(Num)
    Chars= @text[0]
    GlobalUnlock(Num)
    OpenClipboard(0)
    EmptyClipboard()
    SetClipboardData(CF_TEXT,Chars)
    CloseClipboard()
End Sub

SaveToClipBoard("hello")
While being happy with this (KristopherWindsor's one and rather long...) :

Code: Select all

#include once "windows.bi"

Sub set_clipboard(Byref txt As Const String)
'https://www.freebasic.net/forum/viewtopic.php?f=7&t=14933&p=128787&hilit=clipboard+empty#p128787
    Dim As HANDLE hText = NULL
    Dim As Ubyte Ptr clipmem = NULL
    Dim As Integer n = Len(txt)
    If n > 0 Then
        hText = GlobalAlloc(GMEM_MOVEABLE Or GMEM_DDESHARE, n + 1)
        Sleep 15
        If (hText) Then
            clipmem = GlobalLock(hText)
            If clipmem Then
                CopyMemory(clipmem, Strptr(txt), n)
            Else
                hText = NULL
            End If
            If GlobalUnlock(hText) Then
                hText = NULL
            End If
        End If
        If (hText) Then
            If OpenClipboard(NULL) Then
                Sleep 15
                If EmptyClipboard() Then
                    Sleep 15
                    If SetClipboardData(CF_TEXT, hText) Then
                        Sleep 15
                    End If
                End If
            CloseClipboard()
            End If
        End If
    End If
End Sub

set_clipboard("hello")
I also tried another one from around here, and got the antivirus hit very badly. Does anyone understand what fundamentally makes here any difference?

Image
Last edited by Tourist Trap on Nov 19, 2020 19:35, edited 1 time in total.
dodicat
Posts: 7987
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Antivirus hurt by functions pasting text on clipboard - sometimes

Post by dodicat »

Hi TT
This one is better for Win 10.
I think the last code was for XP, it was a while back.

Code: Select all


#include "windows.bi"
#include "crt.bi"
Function get_clipboard() As String
        If IsClipboardFormatAvailable(CF_TEXT) = 0 Then Return "Error"
        If OpenClipboard(0) = 0 Then Return "Error"
        Function = *Cast(zstring Ptr,GetClipboardData(CF_TEXT))
        CloseClipboard()
End Function

Sub SaveToClipBoard(Text As String)       'write Text to the clipboard
   Var Num=GlobalAlloc(GMEM_MOVEABLE,Len(Text)+1)
   memcpy(GlobalLock(num), @text[0], Len(Text)+1)
   Var Chars=GlobalLock(Num)
    GlobalUnlock(Num)
    OpenClipboard(0)
    EmptyClipboard()
    SetClipboardData(CF_TEXT,Chars)
    CloseClipboard()
End Sub

SaveToClipboard("Hello")

print get_clipboard()
sleep
 
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Antivirus hurt by functions pasting text on clipboard - sometimes

Post by Tourist Trap »

dodicat wrote:Hi TT
This one is better for Win 10.
I think the last code was for XP, it was a while back.
That works perfectly. I can even omit CRT.BI by the way here.

You are probably right that there must be something related to XP, for which the old code was designed. But KristopherWindsor's version looks almost identical, left besides the waiters, and the nested conditions. So I still wonder. Are the 2 old codes so different?

Anyway thanks for your fix, I'll use it as soon as now if you don't care ;)
dodicat
Posts: 7987
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Antivirus hurt by functions pasting text on clipboard - sometimes

Post by dodicat »

Windows.bi is fine without crt.bi to get memcpy, thanks for that.
Good luck with your project.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Antivirus hurt by functions pasting text on clipboard - sometimes

Post by jj2007 »

Does Kaspersky complain about the exe as such, or only when it's running?
Btw I use OpenClipboard(GetDesktopWindow()) but have forgotten why that is better than OpenClipboard(0).
Caution with GlobalAlloc(GMEM_MOVEABLE,Len(Text)+1): the function expects bytes, but FB Len() provides chars for a unicode text:

Code: Select all

#define UNICODE ' **** DANGER ****
#include once "windows.bi"

Sub SaveToClipBoard(Byref Text As String)
    Var Buffer=GlobalAlloc(GMEM_MOVEABLE, Len(Text)+1)	' Len refers to the Ansi version of the text
    lstrcpy(GlobalLock(Buffer), Text)			' but the Unicode version gets copied!
    GlobalUnlock(Buffer)
    OpenClipboard(GetDesktopWindow())
    EmptyClipboard()
    SetClipboardData(CF_UNICODETEXT, Buffer)
    CloseClipboard()
End Sub

SaveToClipBoard("Hello world, how are you?")

print "text set to clipboard"
Sleep
That runs without apparent problems, but under the hood you can see that GlobalAlloc uses too few bytes, and only half of the text is on the clipboard - a recipe for disaster. Here are VirusTotal results.
fxm
Moderator
Posts: 12145
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Antivirus hurt by functions pasting text on clipboard - sometimes

Post by fxm »

Tourist Trap wrote:Image
'Un programme malveillant actif a été détecté':
This therefore leaves us to suppose that the detection of a potential virus took place during the execution of the program, perhaps at the time of the execution of the crashing part of code ('SetClipboardData(CF_TEXT,Chars)').
Last edited by fxm on Nov 20, 2020 12:51, edited 1 time in total.
dodicat
Posts: 7987
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Antivirus hurt by functions pasting text on clipboard - sometimes

Post by dodicat »

The code was for text (ansi) - jj2007
For unicode this is better.

Code: Select all

#include "windows.bi"
#include "crt.bi"

Function get_clipboardW() As wString ptr
    dim as wstring * 10 e="ERROR"
        If IsClipboardFormatAvailable(CF_UNICODETEXT) = 0 Then Return @e
        If OpenClipboard(0) = 0 Then Return @e
        Function = Cast(wstring Ptr,GetClipboardData(CF_UNICODETEXT))
        CloseClipboard()
End Function

Sub SaveToClipBoardW(Text As wString)       'write uText to the clipboard
   Var Num=GlobalAlloc(GMEM_MOVEABLE,(wcslen(@Text)+1)*sizeof(wstring))
   memcpy(GlobalLock(num), @text, (wcslen(@Text)+1)*sizeof(wstring))
   var Chars=GlobalLock(Num)
    GlobalUnlock(Num)
    OpenClipboard(0)
    EmptyClipboard()
    SetClipboardData(CF_UNICODETEXT,Chars)
    CloseClipboard()
End Sub

dim as wstring * 1000 us

us="По оживлённым берегам" +wchr(10)
us+="Громады стройные теснятся" +wchr(10)
us+="Дворцов и башен; корабли" +wchr(10)
us+="Толпой со всех концов земли"+wchr(10)
us+="К богатым пристаням стремятся;"





SaveToClipBoardW (us)
print *get_clipboardW()
sleep 
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Antivirus hurt by functions pasting text on clipboard - sometimes

Post by jj2007 »

dodicat wrote:The code was for text (ansi) - jj2007
For unicode this is better.
Your code works fine, but too much acrobatics for my basic taste. My snippet above works fine, too, if you put 2*Len+2.

The point is another one: You tell the compiler "it's unicode" with #Define UNICODE. Fine, so the compiler knows that it has to use lstrcpyW instead of lstrcpyA. Right? But it uses ANSI Len() for a string passed as "Hello" (i.e. immediate), and afterwards it creates the Unicode string that gets passed to lstrcpyW (I've checked that in the debugger, at assembly level). IMHO that's a fat bug.

Btw the snippet above works apparently fine. It's only when you run it with a heap debugger that you realise that lstrcpyW does corrupt the heap. I've always thought that defining a whole application as "Unicode" must end up in disaster.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Antivirus hurt by functions pasting text on clipboard - sometimes

Post by Tourist Trap »

fxm wrote: perhaps at the time of the execution of the crashing part of code ('SetClipboardData(CF_TEXT,Chars)').
Why would it be a crashing part? Do you mean the code is broken? It's possible but then why not the KristoherWindsor's one? Does it handle the process more properly?
jj2007 wrote:Does Kaspersky complain about the exe as such, or only when it's running?
While running. It's caught on fly.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Antivirus hurt by functions pasting text on clipboard - sometimes

Post by jj2007 »

Tourist Trap wrote:Why would it be a crashing part? Do you mean the code is broken?

Code: Select all

## HEAP[TmpFb.exe]:
## Invalid address specified to RtlGetUserInfoHeap( 00760000, 009D2938 )
fxm
Moderator
Posts: 12145
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Antivirus hurt by functions pasting text on clipboard - sometimes

Post by fxm »

Tourist Trap wrote:
fxm wrote: perhaps at the time of the execution of the crashing part of code ('SetClipboardData(CF_TEXT,Chars)').
Why would it be a crashing part? Do you mean the code is broken? It's possible but then why not the KristoherWindsor's one? Does it handle the process more properly?
As 'Chars=@text[0]', this argument passed to 'SetClipboardData()' is not a handle to a string character data (but only a pointer to the passed string character data).

Working code:

Code: Select all

#include once "windows.bi"
#include "crt/string.bi"

Sub SaveToClipBoard(Byref Text As String)
    Var Num=GlobalAlloc(0,Len(Text)+1)
    Var Chars=GlobalLock(Num)
'    Chars=@text[0]
    memcpy(chars,@Text[0],Len(Text)+1)
    GlobalUnlock(Num)
    OpenClipboard(0)
    EmptyClipboard()
'    SetClipboardData(CF_TEXT,Chars)
    SetClipboardData(CF_TEXT,Num)
    CloseClipboard()
End Sub

SaveToClipBoard("hello")
dodicat
Posts: 7987
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Antivirus hurt by functions pasting text on clipboard - sometimes

Post by dodicat »

You can use the unicode version for ansi text also.

Code: Select all

#include "windows.bi"
#include "crt.bi"

Function get_clipboard() As wString ptr
    dim as wstring * 10 e="ERROR"
        If IsClipboardFormatAvailable(CF_UNICODETEXT) = 0 Then Return @e
        If OpenClipboard(0) = 0 Then Return @e
        Function = Cast(wstring Ptr,GetClipboardData(CF_UNICODETEXT))
        CloseClipboard()
End Function


Sub SaveToClipBoard(Text As wString)       'write uText to the clipboard
   Var Num=GlobalAlloc(GMEM_MOVEABLE,(wcslen(@Text)+1)*sizeof(wstring))
   memcpy(GlobalLock(num), @text, (wcslen(@Text)+1)*sizeof(wstring))
   var Chars=GlobalLock(Num)
    GlobalUnlock(Num)
    OpenClipboard(0)
    EmptyClipboard()
    SetClipboardData(CF_UNICODETEXT,Chars)
    CloseClipboard()
End Sub


SaveToClipBoard("Hello world, how are you?")
print *get_clipboard()

dim as string s="Not great."

SaveToClipBoard(s)
print *get_clipboard()


sleep
 
I would say kill two birds with the one stone, but that is not appropriate these days, perhaps have your cake and eat it.
Post Reply