flat assembler
Message board for the users of flat assembler.

Index > Main > How to automatically detect the OS at compile time

Author
Thread Post new topic Reply to topic
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20220
Location: In your JS exploiting you and your system
revolution 18 May 2024, 16:56
Code:
virtual
        file "%ComSpec%%SHELL%"
        load OS_byte byte from $$
end virtual
if OS_byte = 'M'
        display 'Windows OS'
        format pe ...
else if OS_byte = 0x7f
        display 'Linux OS'
        format elf ...
else
        display 'Unknown OS'
        err
end if    
The above code can automatically show the running OS without needing to set any values, or make a special included file that defines the OS.

It works because fasm will replace any unset environment variables with null.

When run in Windows, only "ComSpec" is expanded and cmd.exe is loaded.

When run in Linus, only "SHELL" is expanded and /bin/bash (or whatever your shell is) is loaded.

Then the first byte is examined to determine the file type. "M" is the DOS stub for Windows, and 0x7f is the ELF header for Linux.

Then you can have OS dependant code paths be assembled as needed.
Post 18 May 2024, 16:56
View user's profile Send private message Visit poster's website Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 922
Location: Russia
macomics 18 May 2024, 19:25
Then it's worth checking all 'magic' in header

It is also worth considering the case of missing values in both ComSpec and SHELL, or an incorrect value in these variables
Code:
virtual
        file "%ComSpec%%SHELL%"
    if $ > 16
        load OS_word word from $$
        load OS_dword dword from $$
    end if
end virtual
if OS_word = 'MZ'
        display 'Windows OS'
        format pe ...
else if OS_dword = 0x7f or ( 'ELF' shl 8 )
        display 'Linux OS'
        format elf ...
else
        display 'Unknown OS'
        err
end if    

Code:
$ touch ./zero.txt; env SHELL="./zero.txt" fasm ostype.asm
flat assembler  version 1.73.32  (16384 kilobytes memory, x64)
Unknown OS
ostype.asm [16]:
        err
processed: err
error: error directive encountered in source file.

$ env ComSpec="./x96dbg.exe" env SHELL="" fasm ostype.asm
flat assembler  version 1.73.32  (16384 kilobytes memory, x64)
Windows OS
1 passes, 512 bytes.

$ fasm ostype.asm
flat assembler  version 1.73.32  (16384 kilobytes memory, x64)
Linux OS
1 passes, 268 bytes.

$ echo M>./test.txt; env SHELL="./test.txt" fasm ostype.asm
flat assembler  version 1.73.32  (16384 kilobytes memory, x64)
Unknown OS
ostype.asm [16]:
        err
processed: err
error: error directive encountered in source file.    
Post 18 May 2024, 19:25
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20220
Location: In your JS exploiting you and your system
revolution 19 May 2024, 04:01
Under WINE it fails because WINE imports the SHELL value, and also sets the ComSpec value for itself. So the end result is both values run together and it gives this mess:
Code:
C:\windows\system32\cmd.exe/bin/bash    
Post 19 May 2024, 04:01
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2471
Furs 19 May 2024, 19:55
Well tbh the solution is too much of a hack since you can just export those envs vars yourself, or change them even.
Post 19 May 2024, 19:55
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20220
Location: In your JS exploiting you and your system
revolution 20 May 2024, 01:57
I think it is also a hack to have to create, maintain and update a separate file for each system.
Code:
; filename OS.inc

OS_Unknown      = -1
OS_Linux        = 0
OS_Windows      = 1
OS_Mac          = 2
OS_Unix         = 3
OS_DOS          = 4
OS_QNX          = 5
OS_CPM          = 6
OS_OS2          = 7
OS_Menuet       = 8

; ========================================================================
; IMPORTANT !!!!! change the line below to match the system you have !!!!!
; ========================================================================

OS = OS_        ; <--- complete this line before using !!!!!    
If you are deploying from a central system then updating and distributing become awkward and annoying.
Post 20 May 2024, 01:57
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2471
Furs 20 May 2024, 15:34
Usually you pass it as a command line arg or something along those lines to a compilation script. Or an env var, but one explicitly set for this purpose, not having other side-effects.

Note that there's no guarantee you're even assembling for your own platform in the first place.
Post 20 May 2024, 15:34
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20220
Location: In your JS exploiting you and your system
revolution 20 May 2024, 15:57
If that works for you then great.

I find it extremely convenient to just go to a customer's machine (with whatever OS they might have) and type "fasm source.asm" and done.

Then there is no need to fiddle with command line options, or have to select the correct script, or make a custom OS dependant file, etc. All those things are easy to make mistakes, or forget to do.
Post 20 May 2024, 15:57
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.