how to make an app crash
-
You might be surprised by my question
I have a system to detect whether the application has been cracked, here I use if and else conditions if the application has been cracked I want the application to crash or not responding when closing the application, is that possible to do in Qt5 c++? -
You might be surprised by my question
I have a system to detect whether the application has been cracked, here I use if and else conditions if the application has been cracked I want the application to crash or not responding when closing the application, is that possible to do in Qt5 c++?how to make an app crash
There are many programmers you could employ where this would be the outcome :)
Well there's nothing particular for this in Qt but plenty of possibilities in C++. Write code to divide by 0, dereference a
nullptr
, I think the library function_abort()
/std::abort()
will "crash" your app even in release compilation, etc. Or executewhile (true) ::sleep(1);
, that's not going anywhere. -
You might be surprised by my question
I have a system to detect whether the application has been cracked, here I use if and else conditions if the application has been cracked I want the application to crash or not responding when closing the application, is that possible to do in Qt5 c++?@Blackzero If this is some sort of protection mechanism then you should not cause the program to crash at the same time/place as the integrity check that is failing. You could do something like:
if (cracked_program_detected()) QTimer::singleshot(QRandomGenerator::global()->bounded(100, 2000), this, &Foo::crashMeNow);
So the program dies some random time later, while it is executing something other than the check code. You need to scatter checks like this throughout the executable. Each check should be coded differently, preferrably not in a single check function (inline code). Each should crash or exit the program in a different way. You need to make sure that these bits of code do not get merged or optimized out. Makes is harder to find the checks but is not going to stop a determined attempt.
Signing the executable will flag attempts at altering the executable permanently.
-
how to make an app crash
There are many programmers you could employ where this would be the outcome :)
Well there's nothing particular for this in Qt but plenty of possibilities in C++. Write code to divide by 0, dereference a
nullptr
, I think the library function_abort()
/std::abort()
will "crash" your app even in release compilation, etc. Or executewhile (true) ::sleep(1);
, that's not going anywhere.@JonB I have tried using the code while (true) sleep(1) but it stops and there is no affected loop still forced to stop by qt I think there is another way to make the application crash, I tried this code but it doesn't seem to happen anything
char* crashPtr = nullptr; *crashPtr = 0xDE; std::memset(reinterpret_cast<void*>(0xDEADBEEF), 0, 1024);
-
@JonB I have tried using the code while (true) sleep(1) but it stops and there is no affected loop still forced to stop by qt I think there is another way to make the application crash, I tried this code but it doesn't seem to happen anything
char* crashPtr = nullptr; *crashPtr = 0xDE; std::memset(reinterpret_cast<void*>(0xDEADBEEF), 0, 1024);
@Blackzero said in how to make an app crash:
but it stops and there is no affected loop still forced to stop by qt
Dunno what you mean. You asked for e.g. " or not responding when closing the application,", such a loop should do that.
Keep trying whatever if you want code to crash. Surprised that code attempts you show don't "crash", but there you are. Try what @ChrisW67 said.
On a separate matter I cannot imagine why you are worried about anyone trying to "hack/crack" your code. Who can be bothered? If someone really wants to crack your code they are clever enough to deal with your "protection" mechanisms, seems a waste of time to me, but there you are.
-
@JonB I have tried using the code while (true) sleep(1) but it stops and there is no affected loop still forced to stop by qt I think there is another way to make the application crash, I tried this code but it doesn't seem to happen anything
char* crashPtr = nullptr; *crashPtr = 0xDE; std::memset(reinterpret_cast<void*>(0xDEADBEEF), 0, 1024);
#include <QCoreApplication> #include <QTimer> #include <cstring> int main(int argc, char **argv) { QCoreApplication app(argc, argv); QTimer::singleShot( 3000, // [](){ char* crashPtr = nullptr; *crashPtr = 0xDE; } [](){ std::memset(reinterpret_cast<void*>(0xDEADBEEF), 0, 1024); } ); return app.exec(); }
Both lambdas crash here as expected.
My compiler just warns when given
[](){ int xyzzy = 271828/0; }
and optimizes out the unused variable. So, you need to be careful. -
#include <QCoreApplication> #include <QTimer> #include <cstring> int main(int argc, char **argv) { QCoreApplication app(argc, argv); QTimer::singleShot( 3000, // [](){ char* crashPtr = nullptr; *crashPtr = 0xDE; } [](){ std::memset(reinterpret_cast<void*>(0xDEADBEEF), 0, 1024); } ); return app.exec(); }
Both lambdas crash here as expected.
My compiler just warns when given
[](){ int xyzzy = 271828/0; }
and optimizes out the unused variable. So, you need to be careful. -
try
#include <signal.h> .... raise(SIGSEGV);
it simulates a standard crash when access invalid memory. You can of course use other macros, instead of SIGSEV. See here for more information:
https://www.tutorialspoint.com/c_standard_library/signal_h.htm -
@Blackzero If this is some sort of protection mechanism then you should not cause the program to crash at the same time/place as the integrity check that is failing. You could do something like:
if (cracked_program_detected()) QTimer::singleshot(QRandomGenerator::global()->bounded(100, 2000), this, &Foo::crashMeNow);
So the program dies some random time later, while it is executing something other than the check code. You need to scatter checks like this throughout the executable. Each check should be coded differently, preferrably not in a single check function (inline code). Each should crash or exit the program in a different way. You need to make sure that these bits of code do not get merged or optimized out. Makes is harder to find the checks but is not going to stop a determined attempt.
Signing the executable will flag attempts at altering the executable permanently.
@ChrisW67 said in how to make an app crash:
f this is some sort of protection mechanism then you should not cause the program to crash at the same time/place as the integrity check that is failing. You could do something like:
Yes, you understand what I mean.
I don't understand what the code is doing but I want to make the crash as if it is pure without having to create a class to make the crash. -
#include <QCoreApplication> #include <QTimer> #include <cstring> int main(int argc, char **argv) { QCoreApplication app(argc, argv); QTimer::singleShot( 3000, // [](){ char* crashPtr = nullptr; *crashPtr = 0xDE; } [](){ std::memset(reinterpret_cast<void*>(0xDEADBEEF), 0, 1024); } ); return app.exec(); }
Both lambdas crash here as expected.
My compiler just warns when given
[](){ int xyzzy = 271828/0; }
and optimizes out the unused variable. So, you need to be careful.@ChrisW67 said in how to make an app crash:
Both lambdas crash here as expected.
My compiler just warns when given { int xyzzy = 271828/0; } and optimizes out the unused variable. So, you need to be careful.
Your code works but it doesn't work until the application is closed,
see my codeMainWindow::~MainWindow() { if(cracked_program_detected()) { QTimer::singleShot( 3000, // [](){ char* crashPtr = nullptr; *crashPtr = 0xDE; } [](){ std::memset(reinterpret_cast<void*>(0xDEADBEEF), 0, 1024); } ); } delete ui; }
-
@ChrisW67 said in how to make an app crash:
Both lambdas crash here as expected.
My compiler just warns when given { int xyzzy = 271828/0; } and optimizes out the unused variable. So, you need to be careful.
Your code works but it doesn't work until the application is closed,
see my codeMainWindow::~MainWindow() { if(cracked_program_detected()) { QTimer::singleShot( 3000, // [](){ char* crashPtr = nullptr; *crashPtr = 0xDE; } [](){ std::memset(reinterpret_cast<void*>(0xDEADBEEF), 0, 1024); } ); } delete ui; }
@Blackzero
You set off a "3-second delayed crash" in main window's destructor. If the main window is the last window visible and is being closed and/or you exit the main'sapp.exec()
before the 3 seconds are up it won't get executed. -
You might be surprised by my question
I have a system to detect whether the application has been cracked, here I use if and else conditions if the application has been cracked I want the application to crash or not responding when closing the application, is that possible to do in Qt5 c++?@Blackzero said in how to make an app crash:
I have a system to detect whether the application has been cracked
Would be interesting to hear how it works ;-)
If someone really wants to hack/crack your app, which I generally doubt, your "crack detection" mechanism would get bypassed as well somehow.Edit:
Also, I think if you want to let your app crash in a "controlled" way, as soon as you have "detected something suspicious", you can throw exceptions instead of accessing an invalid pointer or perform some other "illegal" actions to make your program crash randomly (in an uncontrolled way).
-
how to make an app crash
There are many programmers you could employ where this would be the outcome :)
Well there's nothing particular for this in Qt but plenty of possibilities in C++. Write code to divide by 0, dereference a
nullptr
, I think the library function_abort()
/std::abort()
will "crash" your app even in release compilation, etc. Or executewhile (true) ::sleep(1);
, that's not going anywhere.@JonB said in how to make an app crash:
how to make an app crash
Write code to divide by 0, dereference a
nullptr
,Not a good idea. This us undefined behavior, and the compiler can do what it wants. It could e.g. optimize the whole checking function away. I liked your other suggestions better :-)