This is a consequence of enforcing the letter of the law rather than the spirit of the law. Bad actors keep finding loopholes to exploit and politicians fix them with patchwork resulting in overly complex monstrosities of legalese with a million little exceptions. Of course this just adds to the loophole problem.
The only way to reliably fix this is to institute some form of judgment of whether or not an actor was following the spirit of the law. If they weren't they should have any advantages gained by skirting the law removed by force.
Of course this leads to a different set of problems with the potential for overreaching state actors. One way to combat this could be making the judicial branch much more independent than it usually is in most countries.
The only way to reliably fix this is to institute some form of judgment of whether or not an actor was following the spirit of the law. If they weren't they should have any advantages gained by skirting the law removed by force.
Of course this leads to a different set of problems with the potential for overreaching state actors. One way to combat this could be making the judicial branch much more independent than it usually is in most countries.