Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

!cpuid script #361

Open
xxA1eksandrxx opened this issue May 8, 2024 · 47 comments
Open

!cpuid script #361

xxA1eksandrxx opened this issue May 8, 2024 · 47 comments
Labels
bug Something isn't working enhancement An enhancement to an existing feature question Further information is requested

Comments

@xxA1eksandrxx
Copy link

Hi! I use " !cpuid script { {printf("cpuid : %p\n", @rip);} } " and want to add tr(step-in and registers) after print:
" !cpuid script { {printf("cpuid : %p\n", @rip);} , tr} " but this not working.... how i add debug command in script?

@SinaKarvandi
Copy link
Member

Hi,
Thanks for creating this issue. 't' or 'tr' are commands in HyperDbg and commands are not designed to run as a VMX-root script. However, you can emulate their behavior by using the printf function.

For example:

printf("rax: %llx , rbx: %llx, rcx: %llx, rdx: %llx", @rax, @rbx, @rcx, @rdx); 

And so on...

If you want to know the content registers after the execution of CPUID instruction, you need to use the 'Event calling stage' mechanism.

Please take a look at the explanation available here:
https://docs.hyperdbg.org/tips-and-tricks/misc/event-calling-stage

In short, all you need to do is specify the calling stage on your '!cpuid' command:

!cpuid stage post script {
printf("rax: %llx , rbx: %llx, rcx: %llx, rdx: %llx", @rax, @rbx, @rcx, @rdx); 
}

For more information, take a look at Example 2:
https://docs.hyperdbg.org/tips-and-tricks/misc/event-calling-stage#example-2

Also, another video that might be helpful for your case:
https://www.youtube.com/watch?v=H4lrb5x64Ws&t=3s

Please let me know if you have any other questions.

@SinaKarvandi SinaKarvandi added the question Further information is requested label May 9, 2024
@xxA1eksandrxx
Copy link
Author

can the hypervisor change the register value "tsc" or is it necessary to change the value on the "fly" (tsc - time)

@SinaKarvandi
Copy link
Member

It's possible to change the value of TSC (RDTSC/RDTSCP) but changing these values will make your system unstable and it's kinda tricky to change since the timing needs to be accurate but yes, you can easily change the value of registers in case the '!tsc' command.

More information:
https://research.hyperdbg.org/assets/documents/vm-exit-transparency.pdf

@xxA1eksandrxx
Copy link
Author

!hide automatically change the value of registers after RDTSC?

@SinaKarvandi
Copy link
Member

Yes, but the current implementation might make the system unstable. We might need to re-design this feature in the future.

@xxA1eksandrxx
Copy link
Author

xxA1eksandrxx commented May 9, 2024

using a script !cpuid {file: .../script.txt }, global parameters are saved when calling this script in !cpuid?

@SinaKarvandi
Copy link
Member

using a script !cpuid {file: .../script.txt }, global parameters are saved when calling this script in !cpuid?

What do you mean by 'global parameter'? Global variables?

@xxA1eksandrxx
Copy link
Author

https://docs.hyperdbg.org/commands/scripting-language/variables-and-assignments - global variables assignment
if using ".my_variable = 0" in my script.txt (!cpuid {file: .../script.txt } ), then next call script.txt ".my_variable = 0" is save?

@SinaKarvandi
Copy link
Member

Yes, as long as you use a dot '.' in your variable it will remain unchanged in all commands (including scripts and event scripts) until you unload HyperDbg. Once you unload HyperDbg, global variables will be removed.

Also please keep in mind that if your event is running the same script simultaneously in multiple cores, you might end up with incorrect values (behavior). Usually, those who use global variables in HyperDbg, also use spinlocks and interlocked functions alongside.

Example 1:
https://docs.hyperdbg.org/commands/scripting-language/examples/access-to-a-shared-variable-from-different-cores

Example 2:
https://docs.hyperdbg.org/commands/scripting-language/examples/count-occurrences-of-events

More information:
https://docs.hyperdbg.org/commands/scripting-language/functions/interlocked
and:
https://docs.hyperdbg.org/commands/scripting-language/functions/spinlocks

@xxA1eksandrxx
Copy link
Author

thanks for the answer, I'm checking)

@xxA1eksandrxx
Copy link
Author

how to use !hide for ring 0 (kernel driver)?

@xxA1eksandrxx
Copy link
Author

.my_variable = 100;
if (@rip > fffff804d4bb99aa-11089AA && @rip < fffff804d4bb99aa+384656 )
{
printf("RDTSC : %p %p %p\n", @RDX, @Rax, @rip);
.my_variable = .my_variable-1;
printf("RDTSC : %p\n", .my_variable);
}

it`s my script with global variable, but his not working (the variable does not change )

@SinaKarvandi
Copy link
Member

how to use !hide for ring 0 (kernel driver)?

You need to modify the following lines to check for your kernel driver and then compile your customized version of HyperDbg:

// Check for process id and process name, if not match then we don't emulate it

@SinaKarvandi
Copy link
Member

.my_variable = 100; if (@rip > fffff804d4bb99aa-11089AA && @rip < fffff804d4bb99aa+384656 ) { printf("RDTSC : %p %p %p\n", @RDX, @Rax, @rip); .my_variable = .my_variable-1; printf("RDTSC : %p\n", .my_variable); }

it`s my script with global variable, but his not working (the variable does not change )

Are you assigning .my_variable = 100; each time? If yes, then each time you run the script, it'll be reassigned to 100.

@xxA1eksandrxx
Copy link
Author

Как использовать !hide для кольца 0 (драйвер ядра)?

Вам нужно изменить следующие строки, чтобы проверить наличие драйвера ядра, а затем скомпилировать свою пользовательскую версию HyperDbg:

// Check for process id and process name, if not match then we don't emulate it

@xxA1eksandrxx
Copy link
Author

xxA1eksandrxx commented May 10, 2024

what is the point hwdbg if there is a qemu? (sorry about the topic here)

@SinaKarvandi
Copy link
Member

hwdbg is designed to debug hardware chips (FPGAs), not Windows, Linux, or any operating system.

@xxA1eksandrxx
Copy link
Author

xxA1eksandrxx commented May 10, 2024

hwdbg предназначен для отладки аппаратных микросхем (FPGA), а не Windows, Linux или любой другой операционной системы.

is this an FPGA simulator?

@SinaKarvandi
Copy link
Member

hwdbg предназначен для отладки аппаратных микросхем (FPGA), а не Windows, Linux или любой другой операционной системы.

is this an FPGA simulator?

Nope, it generates a synthesizable Verilog (SystemVerilog) code to run on FPGAs.

@xxA1eksandrxx
Copy link
Author

hwdbg предназначен для отладки аппаратных микросхем (FPGA), а не Windows, Linux или любой другой операционной системы.

это симулятор ПЛИС?

Нет, он генерирует синтезируемый код Verilog (SystemVerilog) для запуска на FPGA.

when will it be possible to test?

@SinaKarvandi
Copy link
Member

It will probably be in July or August if everything goes well.

@xxA1eksandrxx
Copy link
Author

0: kHyperDbg> ? .my_variable = @Rax
Line 0:
.my_variable = @Rax
^
Syntax Error: Invalid Syntax

What am I doing wrong?

@SinaKarvandi
Copy link
Member

You need to add a semicolon to the end of your assignment:

? .my_variable = @rax;

@xxA1eksandrxx
Copy link
Author

? .my=@Rax; then script.txt @Rax = .my + 1000; but @Rax It doesn't change ... script.txt run in !tsc stage port script {file:...}

@SinaKarvandi
Copy link
Member

SinaKarvandi commented May 10, 2024

? .my=@Rax; then script.txt @Rax = .my + 1000; but @Rax It doesn't change ... script.txt run in !tsc stage port script {file:...}

Could you post your full (!tsc) command? I couldn't quite understand the question. 🤨

@xxA1eksandrxx
Copy link
Author

? .my=; тогда script.txt = .my + 1000; но Это не меняется... script.txt запускается в !tsc stage port script {file:...}

Не могли бы вы опубликовать свою полную (!tsc) команду? Я не совсем понял вопрос. 🤨

1: kHyperDbg> ? .my=@Rax;

then
script.txt:
if (@Rax!=0 ){
@Rax = .my + 1000;
printf("new RDTSC : %p\n", @Rax);
}

then
!tsc stage post script {file:C:\Users\Test\Documents\Virtual Machines\Windows 10 x64\release\1.txt}

result
1: kHyperDbg> g
debuggee is running...
new RDTSC : FFFFF804E6E1A9AA
new RDTSC : FFFFF804E6E1A9AA
new RDTSC : FFFFF804E6E1A9AA
new RDTSC : FFFFF804E6E1A9AA

@SinaKarvandi
Copy link
Member

SinaKarvandi commented May 10, 2024

? .my=@rax;
The above statement only assigns for one time. Like next time the @Rax is changed, it won't influence the value of .my.

I think what you need is something like this:

!tsc stage post script {
    if (@rax != 0){
    @rax = @rax + 1000;
    printf("new RDTSC : %p\n", @rax);
    }
}

Please note that if you use a global variable, then it might change from different cores simultaneously. In these cases you need to use a local variable (local to the current core).

? my_local_variable = 0;
Not starting with a '.' which is a global variable:

? .my_global_variable = 0;

Please take a look at:
https://docs.hyperdbg.org/commands/scripting-language/variables-and-assignments

@xxA1eksandrxx
Copy link
Author

xxA1eksandrxx commented May 10, 2024

? .my=@rax; Приведенная выше инструкция присваивается только один раз. Как и в следующий раз, когда перемена будет изменена, это не повлияет на значение ..my

Я думаю, что вам нужно что-то вроде этого:

!tsc stage post script {
    if (@rax != 0){
    @rax = @rax + 1000;
    printf("new RDTSC : %p\n", @rax);
    }
}

Обратите внимание, что если вы используете глобальную переменную, то она может изменяться из разных ядер одновременно. В этих случаях необходимо использовать локальную переменную (локальную для текущего ядра).

? my_local_variable = 0; Не начинаясь с '., которая является глобальной переменной:

? .my_global_variable = 0;

Пожалуйста, взгляните на: https://docs.hyperdbg.org/commands/scripting-language/variables-and-assignments

I need to store register values in a variable and by calling the script assign a new register value to the variable

@xxA1eksandrxx
Copy link
Author

in fact this is little bypass rdtsc check

@SinaKarvandi
Copy link
Member

I need to store register values in a variable and by calling the script assign a new register value to the variable

So, for this, you don't need a global variable, you just need a local variable. But this is exactly what the !hide command does. Why don't you use this command instead?

@xxA1eksandrxx
Copy link
Author

Мне нужно сохранить значения регистров в переменной и, вызвав скрипт, присвоить переменной новое значение регистра

Таким образом, для этого вам не нужна глобальная переменная, вам нужна только локальная переменная. Но это именно то, что делает команда. Почему бы вам не использовать эту команду?!hide

yes, that's right, I'm debugging the ring0 code. and there is no regular option to specify the driver's memory area in the command !hide.

@SinaKarvandi
Copy link
Member

yes, that's right, I'm debugging the ring0 code. and there is no regular option to specify the driver's memory area in the command !hide.

Yes, in this case, you just need to modify the following check, in HyperDbg (e.g. add a check for your special driver address based on its RIP register) and recompile HyperDbg:

// Check for process id and process name, if not match then we don't emulate it

@xxA1eksandrxx
Copy link
Author

xxA1eksandrxx commented Jun 1, 2024

yes, that's right, I'm debugging the ring0 code. and there is no regular option to specify the driver's memory area in the command !hide.

Yes, in this case, you just need to modify the following check, in HyperDbg (e.g. add a check for your special driver address based on its RIP register) and recompile HyperDbg:

// Check for process id and process name, if not match then we don't emulate it

Thank you! it is very cool. but I ran into another problem
Debug Sinlge step... after tsc call exception - Debug Exception (#DB) w/ TF (https://secret.club/2020/04/13/how-anti-cheats-detect-system-emulation.html#debug-exception-db-w-tf)(https://secret.club/2020/04/13/how-anti-cheats-detect-system-emulation.html)
the hypervisor goes to debugging step by step and does not exit:
tsc : FFFFF80507FBE726
tsc : FFFFF80507F99953
fffff805`07f99956 68 93 F2 0F 9E push 0xFFFFFFFF9E0FF293
are there any settings for the hypervisor in this case?

@SinaKarvandi SinaKarvandi reopened this Jun 1, 2024
@SinaKarvandi
Copy link
Member

I'm not sure if I quite understand it correctly. 🤨

What do you mean by #DB after tsc? You mean single-stepping through the RDTSC/RDTSCP?

@xxA1eksandrxx
Copy link
Author

Я не уверен, что понимаю это правильно. 🤨

Что вы имеете в виду под #DB после tsc? Вы имеете в виду одношаговое прохождение RDTSC/RDTSCP?

does the hypervisor handle single step exceptions correctly? https://howtohypervise.blogspot.com/2019/01/a-common-missight-in-most-hypervisors.html

@SinaKarvandi
Copy link
Member

This is a good point, I'll try to fix it. Thank you for mentioning it

@SinaKarvandi SinaKarvandi added bug Something isn't working enhancement An enhancement to an existing feature labels Jun 3, 2024
@xxA1eksandrxx
Copy link
Author

Это хороший момент, постараюсь исправить. Спасибо, что упомянули об этом

your product is great, it's strange that it's free

@SinaKarvandi
Copy link
Member

Это хороший момент, постараюсь исправить. Спасибо, что упомянули об этом

your product is great, it's strange that it's free

HyperDbg needs a lot of effort from the community, because of this, it's free and available to everyone.

@xxA1eksandrxx
Copy link
Author

This is a good point, I'll try to fix it. Thank you for mentioning it

and when will it appear approximately fix?

@SinaKarvandi
Copy link
Member

This is a good point, I'll try to fix it. Thank you for mentioning it

and when will it appear approximately fix?

I try to fix it this month with the v0.9 release. You could also fix it and send a PR. Contributing to HyperDbg is always super appreciated.

@xxA1eksandrxx
Copy link
Author

xxA1eksandrxx commented Jun 4, 2024

This is a good point, I'll try to fix it. Thank you for mentioning it

and when will it appear approximately fix?

I try to fix it this month with the v0.9 release. You could also fix it and send a PR. Contributing to HyperDbg is always super appreciated.

having no experience in hypervisor development, this may take time... tell me, where is the exception handling in your hypervisor?

@SinaKarvandi
Copy link
Member

So, in that case, let me fix it. It probably needs lots of tests to make sure it won't break other functionalities.

@SinaKarvandi
Copy link
Member

I encountered this issue, but resolving it is not straightforward. A quick fix could potentially disrupt the functionality of the stepper, necessitating additional testing. Therefore, I've deferred this task to future versions (not v0.9).

@xxA1eksandrxx
Copy link
Author

Я столкнулся с этой проблемой, но решить ее непросто. Быстрое исправление потенциально может нарушить функциональность степпера, что потребует дополнительного тестирования. Поэтому я отложил эту задачу до будущих версий (не v0.9).

what is a quick solution?

@SinaKarvandi
Copy link
Member

A quick solution is to change the cpuid vm-exit handler and synchronize it with the stepping mechanism.

@xxA1eksandrxx
Copy link
Author

A quick solution is to change the cpuid vm-exit handler and synchronize it with the stepping mechanism.

I would like to get a better understanding of the work of the hypervisor, do you have any other contacts? thank you

@SinaKarvandi
Copy link
Member

You can also join the telegram group t.me/hyperdbg , we discuss HyperDbg at the Telegram group too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement An enhancement to an existing feature question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants