It is currently Wed Sep 26, 2018 1:52 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 41 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject:
PostPosted: Thu Aug 11, 2005 6:07 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Fri Apr 25, 2003 6:03 pm
Posts: 3000
Location: Lincoln, UK
thefool wrote:
probably. but is that really a good thing here? i mean, sometimes it is used wisely, sometimes not :shock:

Its never wise to overflow a buffer! You could be writing over data that could be important, e.g. OS stuff.

_________________
--Kale

Image


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Aug 11, 2005 6:15 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Fri Apr 25, 2003 4:34 pm
Posts: 845
Location: Canada
I'm not sure what you are finding so confusing?

We defined the max buffer size with "size.l=512"
The memory address of the buffer is returned in our other variable.

The API command we are calling does the rest of the work. ;)


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Aug 11, 2005 6:28 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Fri Apr 25, 2003 6:03 pm
Posts: 3000
Location: Lincoln, UK
Paul wrote:
I'm not sure what you are finding so confusing?

We defined the max buffer size with "size.l=512"
The memory address of the buffer is returned in our other variable.

The API command we are calling does the rest of the work. ;)

Yes but where is the buffer, what points to it?
CurDir.l is a long with a value of 0
ptrDir.l is a long pointing to CurDir
i still don't see where the string is going?
Code:
  GetCurrentDirectory=GetProcAddress_(module,"GetCurrentDirectoryA")
  size.l=512
  CurDir.l= 0
  ptrDir.l = @CurDir
 
  !PUSH [v_ptrDir]
  !PUSH [v_size]
  !CALL [v_GetCurrentDirectory]

  Debug PeekS(@CurDir)

in this code you are peeking a string at the address of CurDir.l, which is a long not a string

Code:
CurDir.l = 606915
peekL(@CurDir)

will debug the value 606915
Code:
CurDir.l = 606915
peekS(@CurDir)

will debug garbage

_________________
--Kale

Image


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Aug 11, 2005 6:32 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Fri Apr 25, 2003 6:03 pm
Posts: 3000
Location: Lincoln, UK
Now i am home i can run these examples:

This produces a buffer overflow and crashes the process will have to be killed via the IDE. Because a string is being stuffed into a bufer only 4 bytes long.
Code:
module=LoadLibrary_("kernel32.dll")
If module
  GetCurrentDirectory=GetProcAddress_(module,"GetCurrentDirectoryA")
  size.l=512
  CurDir.l= 0
  ptrDir.l = @CurDir
 
  !PUSH [v_ptrDir]
  !PUSH [v_size]
  !CALL [v_GetCurrentDirectory]

  Debug PeekS(@CurDir)
 
  FreeLibrary_(module)
EndIf



this is the correct way:
Code:
module=LoadLibrary_("kernel32.dll")
If module
  GetCurrentDirectory=GetProcAddress_(module,"GetCurrentDirectoryA")
  size.l=512
  CurDir.s= ""
  ptrDir.l = @CurDir
  !PUSH [v_ptrDir]
  !PUSH [v_size]
  !CALL [v_GetCurrentDirectory]
  Debug PeekS(ptrDir)
  FreeLibrary_(module)
EndIf

_________________
--Kale

Image


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Aug 11, 2005 6:53 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Fri Apr 25, 2003 4:34 pm
Posts: 845
Location: Canada
I posted 2 different versions.

Do what you want with it, doesn't really matter to me. ;)


Last edited by Paul on Thu Aug 11, 2005 7:47 pm, edited 2 times in total.

Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Aug 11, 2005 7:14 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Apr 25, 2003 8:39 pm
Posts: 225
Paul wrote:
I posted 2 different versions, both of which work fine here.

You say it crashes with a buffer overflow but I cannot reproduce your problem. Both code do the same thing.

If you do not understand how it works, I'm sorry but there is nothing more I can help you with. I cannot explain it any better since I am a programmer, not a teacher. ;)


I think I know what confuses Kale and yep, I think he is right.
You pass a pointer to a long instead of a pointer of a string, which can work but also can't.

Code:
module=LoadLibrary_("kernel32.dll")
If module
  GetCurrentDirectory=GetProcAddress_(module,"GetCurrentDirectoryA")
  size.l=65000
  CurDir.l= 0

  ptrDir.l = @CurDir

  a = 1
  b = 2
  c = 3
  d = 4
  e = 5
  f = 6
 
  !PUSH [v_ptrDir]
  !PUSH [v_size]
  !CALL [v_GetCurrentDirectory]

  Debug PeekS(@CurDir)
 
  Debug a
  Debug b
  Debug c
  Debug d
  Debug e
  Debug f
 
  FreeLibrary_(module)
EndIf

_________________
Athlon64 3800+ · 1 GB RAM · Radeon X800 XL · Win XP Prof/SP1+IE6.0/Firefox · PB 3.94/4.0
Intel Centrino 1.4 MHz · 1.5 GB RAM · Radeon 9000 Mobility · Win XP Prof/SP2+IE6.0/Firefox · PB 3.94/4.0


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Aug 11, 2005 7:39 pm 
Offline
Always Here
Always Here
User avatar

Joined: Sat Aug 30, 2003 5:58 pm
Posts: 5883
Location: Denmark
as on kales machine it crashes here too. however it DOES show the place where it is then crashes :S


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Aug 11, 2005 10:27 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Fri Apr 25, 2003 6:03 pm
Posts: 3000
Location: Lincoln, UK
Sorry i didn't mean to sound like an arse, but i though you had shown me a undocumented feature of PB on dynamic types but once i ran the examples it was clear the first example was wrong. No big deal though it just did my head in for a bit not knowing if i was wrong or going mad or summits. :twisted:

_________________
--Kale

Image


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Aug 12, 2005 3:26 pm 
Offline
Always Here
Always Here
User avatar

Joined: Sat Aug 30, 2003 5:58 pm
Posts: 5883
Location: Denmark
it was going MAD i tell you!


Top
 Profile  
Reply with quote  
 Post subject: Re: Using DLLs with WinAPI
PostPosted: Sun May 06, 2018 3:32 am 
Offline
User
User

Joined: Sun Apr 30, 2017 12:27 pm
Posts: 16
Code updated For 5.62

Code:
hMod = GetModuleHandle_("kernel32.dll")
If hMod
  GetCurrentDirectory = GetProcAddress_(hMod, UTF8("GetCurrentDirectoryW"))
  size.l=512
  CurDir.s= Space(#MAX_PATH)
  ptrDir.l = @CurDir
  !PUSH [v_ptrDir]
  !PUSH [v_size]
  !CALL [v_GetCurrentDirectory]
  Debug PeekS(ptrDir)
  CloseHandle_(hMod)
EndIf


Top
 Profile  
Reply with quote  
 Post subject: Re: Using DLLs with WinAPI
PostPosted: Sun May 06, 2018 8:36 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat May 05, 2007 5:31 pm
Posts: 481
Location: Linz, Austria
If you use UTF8(), you also need to free the memory yourself (like Fred mentioned in another thread)!
Also, GetModuleHandle_() is freed with FreeLibrary_() and not with CloseHandle_()...
Code:
hMod = GetModuleHandle_("kernel32.dll")
If hMod
  *utf8 = UTF8("GetCurrentDirectoryW")
  GetCurrentDirectory = GetProcAddress_(hMod, *utf8)
  path${#MAX_PATH}
  !PUSH v_path$
  !PUSH 260 ;#MAX_PATH
  !CALL [v_GetCurrentDirectory]
  Debug path$
  FreeMemory(*utf8)
  FreeLibrary_(hMod)
EndIf


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 41 posts ]  Go to page Previous  1, 2, 3

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 2 guests


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

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye