flat assembler
Message board for the users of flat assembler.

Index > Windows > clear screen - win32 console

Author
Thread Post new topic Reply to topic
int0x50



Joined: 19 Jul 2019
Posts: 54
int0x50 16 Jun 2023, 07:54
i am writing a PE64 console application... i am struggling to clear the screen ..

the escape characters through printf is not working out ...
Post 16 Jun 2023, 07:54
View user's profile Send private message Reply with quote
Flier-Mate



Joined: 26 May 2023
Posts: 88
Flier-Mate 16 Jun 2023, 08:56
Maybe you can use ANSI escape code (have to enable it first), but below is what I found through web search.

Quote:
Clearing the console screen done through four Win32 API functions, GetStdHandle(), GetConsoleScreenBufferInfo(), FillConsoleOutputCharacter() and SetConsoleCursorPosition(). It is worth noting that all these functions located in Kernel32.dll library.
Post 16 Jun 2023, 08:56
View user's profile Send private message Reply with quote
int0x50



Joined: 19 Jul 2019
Posts: 54
int0x50 16 Jun 2023, 09:12
thanks Flier-Mate
Post 16 Jun 2023, 09:12
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1391
Location: Piraeus, Greece
Picnic 16 Jun 2023, 11:05
Hi int0x50,

Here is a CLS routine to get you started (32-bit code).
Post 16 Jun 2023, 11:05
View user's profile Send private message Visit poster's website Reply with quote
int0x50



Joined: 19 Jul 2019
Posts: 54
int0x50 16 Jun 2023, 11:47
edit: it worked after including API\KERNEL32.INC into import table. the FillConsoleOutputCharacter should be FillConsoleOutputCharacterA.

I have tried the following program .. the compilation is happening ... but it's not working ... when I tried debugging it I get STATUS_ENTRYPOINT_NOT_FOUND...



section '.data' data readable writeable

handle_getstd dw 1
csbi rb 18
count dw 0
dwLen dd 0
dwCoord dd 0
lpWritten dd 0

section '.text' code readable executable

main:
push rax
push rbx
push rcx
push rdx
xor eax, eax
mov eax, -11
invoke GetStdHandle, -11
mov dword [handle_getstd], eax

xor rdx, rdx
xor rcx, rcx

invoke GetConsoleScreenBufferInfo, dword [handle_getstd], addr csbi

xor ecx, ecx
xor edx, edx

mov byte cl, byte [csbi]
mov byte dl, byte [csbi + 2]
imul ecx, edx
mov dword [dwLen], ecx

invoke FillConsoleOutputCharacter, dword [handle_getstd], 0x20, dword [dwLen], dword [dwCoord], addr lpWritten

pop rdx
pop rcx
pop rbx
pop rax
ret

section '.idata' import data readable
library msvcrt, 'msvcrt.dll',\
kernel32, 'kernel32.dll'
import msvcrt,\
printf, 'printf'

import kernel32,\
GetStdHandle, 'GetStdHandle',\
GetConsoleScreenBufferInfo, 'GetConsoleScreenBufferInfo', \
FillConsoleOutputCharacter, 'FillConsoleOutputCharacter'
Post 16 Jun 2023, 11:47
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4046
Location: vpcmpistri
bitRAKE 16 Jun 2023, 15:59
I'd recommend using fasmg.

Assuming you are using a terminal that supports ANSI codes, this should work:
Code:
format PE64 CONSOLE 6.2 at 0x1_4000_0000
include 'format\format.inc'
include 'macro\import64.inc'
include '..\..\utility\align.inc'

section '.data' data readable writeable

_clear_screen db 27,'[H',27,'[J'
_clear_screen.characters := $ - _clear_screen

section '.text' code readable executable

main:
        entry $
        virtual at rsp
                        dq ?    ; shadow space
                        dq ?
                        dq ?
                        dq ?
                .P5     dq ?    ; fifth parameter

        ; some local space on the stack for temporary data:
                .hStdOut        dq ?
                .dwTemp         dd ?

                align.assume rsp, 16
                align 16 | 8

                .stack_offset := $ - $$
        end virtual
        sub rsp, .stack_offset

        mov rcx, -11 ; STD_OUTPUT_HANDLE
        call [GetStdHandle]
        mov [.hStdOut], rax

        mov [.P5], 0
        lea r9, [.dwTemp]
        mov r8d, _clear_screen.characters
        lea rdx, [_clear_screen]
        mov rcx, [.hStdOut]
        call [WriteConsoleA]

        mov ecx, 0
        call [ExitProcess]
        int3

section '.idata' import data readable

library kernel32, 'kernel32.dll'

import kernel32,\
        ExitProcess, 'ExitProcess',\
        GetStdHandle, 'GetStdHandle',\
        WriteConsoleA, 'WriteConsoleA'    
... there are easier ways, but this code is geared at trying to get up to speed on the environment you're working in.

FYI: Please, use code tags.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup
Post 16 Jun 2023, 15:59
View user's profile Send private message Visit poster's website Reply with quote
Flier-Mate



Joined: 26 May 2023
Posts: 88
Flier-Mate 16 Jun 2023, 17:05
If I remember correctly, in command prompt, the program has to set ENABLE_VIRTUAL_TERMINAL_PROCESSING flag through SetConsoleMode function to enable ANSI escape sequence.

Further reading:
https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences

Thanks for sharing, @Picnic and @bitRAKE, although both approaches are different.
Post 16 Jun 2023, 17:05
View user's profile Send private message Reply with quote
Flier-Mate



Joined: 26 May 2023
Posts: 88
Flier-Mate 16 Jun 2023, 17:08
Example in C provided by Microsoft to enable VT mode:
Code:
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hOut == INVALID_HANDLE_VALUE)
    {
        return false;
    }

    DWORD dwMode = 0;
    if (!GetConsoleMode(hOut, &dwMode))
    {
        return false;
    }

    dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
    if (!SetConsoleMode(hOut, dwMode))
    {
        return false;
    }    
Post 16 Jun 2023, 17:08
View user's profile Send private message Reply with quote
Flier-Mate



Joined: 26 May 2023
Posts: 88
Flier-Mate 16 Jun 2023, 17:11
int0x50 wrote:
thanks Flier-Mate


You're welcomed. Glad that you solved it, but you may want to try ANSI escape code to clear screen as well.
Post 16 Jun 2023, 17:11
View user's profile Send private message Reply with quote
int0x50



Joined: 19 Jul 2019
Posts: 54
int0x50 20 Jun 2023, 06:26
@bitRAKE, what do you mean by " I'd recommend using fasmg." ... how should I read about fasm vs fasmg ?
Post 20 Jun 2023, 06:26
View user's profile Send private message Reply with quote
int0x50



Joined: 19 Jul 2019
Posts: 54
int0x50 20 Jun 2023, 06:32
@bitRAKE, isn't going about high-level with fasmg? I would like to see something at low-level and would like to still have control on many things... in that case is fasmg still useful?
Post 20 Jun 2023, 06:32
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20358
Location: In your JS exploiting you and your system
revolution 20 Jun 2023, 11:06
fasmg is a macro language. To use it to assemble x86 you also need to use the x86 macros to parse the source.

fasm is an assembler proper. Use it stand-alone to assemble x86 code directly.
Post 20 Jun 2023, 11:06
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4046
Location: vpcmpistri
bitRAKE 20 Jun 2023, 20:49
int0x50 wrote:
@bitRAKE, what do you mean by " I'd recommend using fasmg." ... how should I read about fasm vs fasmg ?
Many that start with fasm have challenges which are more easily solved in fasmg, imho. If your only goal is to assemble x86 in a very compatible language fasm is perfectly fine.

I don't know if there is a fasm vs fasmg? fasmg is more general, supporting big signed integers, combined stages, and a secondary macro (CALM). fasm uses {} to block macros. fasmg starts blocks with the <keyword> and terminates blocks with END <keyword>. In many respects they are very similar, but x86 is not built in to fasmg - it can work generally on text in unexpected ways.

int0x50 wrote:
@bitRAKE, isn't going about high-level with fasmg? I would like to see something at low-level and would like to still have control on many things... in that case is fasmg still useful?
fasmg allows redefining the language at a low-level and it scales as needed. HERE is my latest work in Windows. I'm in the process of creating my own PE formatting macros to support all the features of each of the allowed directories with custom syntax (nothing posted, yet).

My aim is creating something featureful - not compatible syntax. Like the advanced align macro, which has very necessary features that make coding easier, imho. Featureful doesn't mean restrictive either - should always be able to manually control code/abstraction generation, imho.

The downside is that I'm not in a hurry to complete anything. The present broken work is like a dozen iterations in. Create something, use it for a while, hate it, try again, ...

Both fasm and fasmg work at several (compatible) abstraction levels with the present Windows macros/includes. I'd try them first until you desire something else.

_________________
¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup


Last edited by bitRAKE on 21 Jun 2023, 10:04; edited 1 time in total
Post 20 Jun 2023, 20:49
View user's profile Send private message Visit poster's website Reply with quote
int0x50



Joined: 19 Jul 2019
Posts: 54
int0x50 21 Jun 2023, 05:43
thank you @bitRAKE ... thanks @revolution ...
Post 21 Jun 2023, 05:43
View user's profile Send private message Reply with quote
int0x50



Joined: 19 Jul 2019
Posts: 54
int0x50 22 Jun 2023, 09:23
"You're welcomed. Glad that you solved it, but you may want to try ANSI escape code to clear screen as well. "

@Flier-Mate, how would you do that?
Post 22 Jun 2023, 09:23
View user's profile Send private message Reply with quote
Flier-Mate



Joined: 26 May 2023
Posts: 88
Flier-Mate 22 Jun 2023, 09:58
As I said above, use SetConsoleMode to enable VT mode, then you can start sending escape character to console.

Code:
format PE console
entry main

include 'win32a.inc'

section '.data' data readable writeable

  _handle dd ?
  _cls    db 27,'[H',27,'[J'
  _len    = $ - _cls
  _mode   dd ?
  _dummy  dd ?

section '.text' code readable executable

  main:

         push   -11
         call   [GetStdHandle]
         mov    dword [_handle], eax
         push   _mode
         push   [_handle]
         call   [GetConsoleMode]
         or     [_mode], 4     ; ENABLE_VIRTUAL_TERMINAL_PROCESSING
         push   [_mode]
         push   [_handle]
         call   [SetConsoleMode]
         push   0
         push   _dummy
         push   _len
         push   _cls
         push   [_handle]
         call   [WriteConsoleA]
         push   0
         call   [ExitProcess]


section '.idata' import data readable

  library kernel32, 'kernel32.dll'

  import kernel32,\
         GetStdHandle, 'GetStdHandle',\
         GetConsoleMode, 'GetConsoleMode',\
         SetConsoleMode, 'SetConsoleMode',\
         WriteConsoleA, 'WriteConsoleA',\
         ExitProcess, 'ExitProcess'    
Post 22 Jun 2023, 09:58
View user's profile Send private message 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.