jm33-m0 / emp3r0r

Linux/Windows post-exploitation framework made by linux user
https://infosec.exchange/@jm33
MIT License
1.26k stars 232 forks source link

`interactive_shell` for Windows: incorrect terminal size #122

Closed jm33-m0 closed 2 years ago

jm33-m0 commented 2 years ago

Overall the shell is working, and you should be able to use Tab and Ctrl in cmd and powershell sessions.

However the terminal size seems wrong, as an example, the when you issue cls to clear screen, on-screen text cannot be cleared correctly.

jm33-m0 commented 2 years ago

The plan is to use SetWindowPos syscall to adjust the size of console window automatically, so it matches the terminal size on C2 side.

The main issue here is we don't have an easy/direct way to obtain the window handle of our child process, one possible solution is to use FindWindow syscall, but it only matches window titles.

jm33-m0 commented 2 years ago

If we use CreateProcess syscall instead of exec.Command wrapper, StartInfo can be adjusted as well as initial window size, but it doesn't solve this issue completely as our terminal size may change.

jm33-m0 commented 2 years ago

For now you can manually set the terminal size in powershell session, if the window gets too small/large

jm33-m0 commented 2 years ago

SetWindowPos seems working, the shell pane (that pops up when you select an agent) looks normal, I am still not sure why it worked.

As a start, SetConsoleScreenBufferSize should be the way to go, but I am afraid that it only resizes the console buffer of current process.

If we use SetWindowPos, it's done in pixels, meaning we might not end up with correct window size (that matches character count)

jm33-m0 commented 2 years ago

FFS, I have solved this issue mostly, now the only issue here is that window resizing ignores title bar width and scroll bar width, so you will end up with a few lines short, meaning the resized console window is always a bit smaller than it should be.

But it should be highly usable, at least when compared with previous versions.

jm33-m0 commented 2 years ago

Now everything should work, open a new issue if you find any other issues