LB Booster
Programming >> BASIC code examples >> PEEK() function
http://lbb.conforums.com/index.cgi?board=code&action=display&num=1425118964

PEEK() function
Post by Richard Russell on Feb 28th, 2015, 09:22am

Over on the LB Community Forum somebody is asking about the PEEK() function, which allows you to read the contents of memory directly. This was commonly available in traditional BASICs (along with POKE, which is much more dangerous!), but tends not to be supported in modern dialects.

Although not provided as a native function in LB4 or LBB, it's very easy to use a little bit of embedded BBC BASIC code to implement it as a user-defined function in LBB:

Code:
    STRUCT test, byte as char[1]
    test.byte.struct = 123
    pointer = test.struct
    PRINT PEEK(pointer)
    END

    FUNCTION PEEK(address)
    !PEEK = ?address
    END FUNCTION 

It should be used with care, since PEEKing an inappropriate address may crash the process (although that shouldn't do any damage, it's not desirable!).

Richard.
Re: PEEK() function
Post by carninesix on Apr 1st, 2015, 05:44am

Hello,

I am struggling here, it is my lack of knowledge more than any thing else. I am going to explain it in a slightly different way and hope that it makes sense.

This is the code I used many years ago on a Commodore Pet 2001, and was hoping to use similar, thus bypassing the operating system restrictions.

10 FOR A = 0 TO 65531
20 B = PEEK (A)
30 PRINT B;
40 NEXT A
50 PRINT
60 END

Instead of the PRINT statement, I would be looking for strings by using PEEK (a+1 . . . 4) to find things like "Intel" or similar.

Hope this helps, I know it can be done as I had software in the past that could do it but not written by me.

Steve

Re: PEEK() function
Post by Richard Russell on Apr 1st, 2015, 08:45am

on Apr 1st, 2015, 05:44am, carninesix wrote:
Instead of the PRINT statement, I would be looking for strings by using PEEK (a+1 . . . 4) to find things like "Intel" or similar.

The function I listed in my earlier message does that. Here for example is a program which scans memory looking for the string 'BASIC':

Code:
   FOR A = 4200000 TO 4280000
     IF PEEK(A)=66 AND PEEK(A+1)=65 AND PEEK(A+2)=83 AND _
     PEEK(A+3)=73 AND PEEK(A+4)=67 THEN PRINT "'BASIC' found at ";A
   NEXT A
   PRINT "Finished scanning memory"
   END

   FUNCTION PEEK(address)
   !PEEK = ?address
   END FUNCTION 

But it only allows you to look at 'user' memory (i.e. the memory which has been allocated to your process and is therefore straightforwardly accessible), which is why I have chosen the address range above rather carefully!

If you are wanting to read memory allocated to other user processes, that is (usually) possible but requires a different technique, and you would need to know exactly which process(es) and memory ranges you were interested in.

If you were hoping to read 'operating system' (kernel) memory that is to all intents and purposes impossible because of the security barriers in Windows.

Richard.
Re: PEEK() function
Post by carninesix on Apr 1st, 2015, 9:31pm

Hello,

Thank you for the reply.

I want to read the hardwares memory, so I can actually read the ROM's directly, so I can pick specific information to hard code in to a program to prevent it being run on another machine.

I do not want to use API's or DLL's as these could be spoofed, I want to try to make my software very secure.

Types of data I would be looking for are chip id's and such.

Steve

Re: PEEK() function
Post by carninesix on Apr 30th, 2015, 12:10am

Hello,

I have not given up on my idea, but I am now looking at other methods. These include but not limited too other O/S's and languages. Some I have tried and have failed at this task, so I started digging a bit more.

I have now read many articles ( some by Richard ) on how Microsoft blocks what I am trying to do. Seems I am for too old school for Microsoft and anything past Win98SE.

Luckily for me this is a hobby project and I have all the time to research and look for alternatives.

Steve

Re: PEEK() function
Post by Dacite on Apr 30th, 2015, 3:46pm

Don't know how many Win versions this will work (it works on WinXP), but you can
Code:
RUN "ipconfig /all > D:\Path\ThisApp_ip.txt" 
read the file, and if they have an ethernet adapter, on the "Physical Address" line it prints the 6 hex bytes adapter address, which won't change unless they put in a new network card. The "Description" line could be useful too. Prefix the data with a string of your choosing and save it as a hash value.

Microsoft collects this information about your computer, so if you replace your network card you have to re-validate your Windows copy with them...

Good luck!

- Dacite
Re: PEEK() function
Post by Richard Russell on Apr 30th, 2015, 5:56pm

on Apr 30th, 2015, 3:46pm, Dacite wrote:
it prints the 6 hex bytes adapter address

There are API ways of getting the MAC address as well. If you don't mind translating from BBC BASIC to Liberty BASIC (or using the code directly by means of the 'BBC BASIC escape' feature of LBB) there are five different methods here:

http://bb4w.wikispaces.com/Finding+the+MAC+address

Richard.

Re: PEEK() function
Post by carninesix on May 2nd, 2015, 09:37am

Hello,

Thank you for the replies, but I am aware of various methods of getting MAC addresses and am also aware of how to spoof them too. This comes from my anti-cheat background.

This is why I actually may ditch Windows ( after 98SE ) for this project, am actually thinking about DOS if I can't find a version of Linux that suits me.

Luckily I am not OS dependent for this project, I can use what ever I need.

Steve

Re: PEEK() function
Post by Richard Russell on May 2nd, 2015, 10:20am

on May 2nd, 2015, 09:37am, carninesix wrote:
Luckily I am not OS dependent for this project, I can use what ever I need.

Isn't your problem less the OS and more the CPU? Even if you were to use MS-DOS, on a modern CPU it would be easy for somebody to hijack the boot process, run a DOS VM that looks indistinguishable from the 'real thing', and then execute your code. How would your program know that the BIOS memory it thinks it is accessing hasn't been spoofed?

Richard.

Re: PEEK() function
Post by Dacite on May 2nd, 2015, 4:17pm

I was thinking you were going to be selling a program, but this must be something you want to ONLY run on YOUR computer. Sorry, the NSA or a determined hacker can disassemble your program and figure out your copy protection, and make it run wherever they want - i.e., insert NOPs over your copy protection code.

- Dacite
Re: PEEK() function
Post by carninesix on May 3rd, 2015, 10:26am

Hello,

This is not copy protection, this is for a security measure.

The reason I want to use PEEK() as a command is to look at specific memory, the ROM on the mother board.

PEEK() is the direct command I know and it is not reliant on any drivers, API's, DLL's or OS's.

Most modern OS's run in protected memory or just don't allow you to look directly at motherboard ROM's.

I want to write code that reads the ROM memory directly and use some values as part of a password. Some of the password will come from the user, the rest from ROM. Each chip has a serial number, this is one of the things I want to use.

Two reasons for this. Most people use or pick easy passwords, including 40-50 unique characters will stop this, but do it invisibly as far as the user is concerned. Second, will mean that the encryption/decryption will only work on one machine.

I know that any cypher can be broken, it is only a matter of time. I will be using multiple layer of various types.

Steve

Re: PEEK() function
Post by Richard Russell on May 3rd, 2015, 11:07am

on May 3rd, 2015, 10:26am, carninesix wrote:
I want to write code that reads the ROM memory directly and use some values as part of a password.

As I said that can be spoofed so that when you think you are looking at the ROM memory "directly" actually you aren't because your code is running in a VM. Don't you care about that?

Richard.

Re: PEEK() function
Post by carninesix on May 3rd, 2015, 10:25pm

Hello,

VM's are not perfect, and I shall be exploiting the flaws in them. Also I will not just be relying on this as a sole check.

This is both an exercise and a problem to be solved for me, I am doing this to push myself.

I have been around computer since '77, whilst everything appears to have got easier on the surface when looking under the hood it is much more difficult.

I have worked in binary, machine code, Cobol, Fortran, Basic ( my preferred language ) and dabbled with a couple of others. My problem today is I am way too rusty, not been actively programming hard stuff for a long time. Most of my recent work ( last 15 years ) has been solving communication/translation issues.

I may never get this to work the way I want, but it will be fun trying.

I am also pleased with the help/attitude here, I was on another forum and was just shot down there. Here I am being given the help and also the reasons behind the help.

Steve