Security [SOLVED]: How to protect threads (of my process) from termination

Security [SOLVED]: How to protect threads (of my process) from termination

Home Forums Security Security [SOLVED]: How to protect threads (of my process) from termination

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36326

    Anonymous

    QuestionQuestion

    I have written a Windows service that does a critical job.
    My program has 5 threads that do a critical job and I don’t want them to be terminated.

    I use process hacker , in threads tab I can terminate any of the 5 thread without causing the whole program to get terminated (because programs are just container)
    and the critical job is not done when user or a hacker (my program is a security program and users may want to make it not work) terminates one the threads (i resume the thread if they get suspended but have no strategy preventing or fixing termination of thread)

    How can I protect those 5 threads from termination?
    (I’ve made the program itself critical but is does have no value when thread can be easily terminated)

    #36327

    Anonymous

    Accepted AnswerAnswer

    i found a solution for my problem

    fist let me explain how i achieved this (for those how don’t want to read :go to the bottom and see final solution)
    {

    1-i made 2 watchdog threads that first one recreates the 5 threads and WatchDogThread2 when terminated and second one does the same to watchdog 1

    (this took me to long because Thread.IsAlive was always returning true even when thread was terminated finally used Thread.Join() and join was working)

    when running after termination of one of the watchdogs other one succefully re created it .

    but after 10-30 seconds and exception was thrown in a part that had no ralation to watchdogs

    i did this for many times and was debugging it
    after 30-50 trys and edit i saw that this exception is thrown in random parts of code not only the particular part

    2-so i researched about this exception

    ExecutionEngineException

    i found out that CRL throws this when some thing in the code has gone wrong
    first i thought this a because of the watchdogs so i changed them a few times but the same only the time differed,
    so i removed them completely and started the program and terminated one the 5 thread
    this time nothing happed

    i did this for a few more times with edition and saw this exception happens when not using watchdogs too , but in 2-3 min instead of 10-30 seconds

    3- i researched again in the web i read in MSDN that this was happening for some one when Garbage Collection was done so he had disabled Garbage Collation and no loner received this exception

    4- so i wrote another thread that forced Garbage to collate every 0.5 second
    and amazingly saw after a thread is terminated the program receives ExecutionEngineException and crashes

    5- YEAH this was the solution do fore garbage collation so the entire process crashes and restarts (or a BSOD happens in my case(my program is critical ))

    }

    FINAL solution (for those who did not read above and jumped here)

    {
    if a thread is terminated
    when garbage collection happens CRL throws ExecutionEngineException and process crashes
    so we do a manual Garbage Collation every 0.5 sec so the program will crash and restart (the critical job start and hacker/user won’t be able to disturb app’s job by terminating one it’s threads

    }

    code : {
    Thread t1 = new Thread(delegate () { while (true) { System.GC.Collect(); Thread.Sleep(500); } }); t1.Start();

    you can start two one these threads for more security (if user terminates t1 itself garbage collection happens after 0-10 mins and in this time he will do what ever he wants

    }

    thanks for reading 😀

    hope this can be of use for you

    Source: https://stackoverflow.com/questions/47876884/how-to-protect-threads-of-my-process-from-termination
    Author: emaditaj
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.