Russian hackers can likely get away with hacking entities in a foreign country. Chinese hackers do the same thing. Cyber militias exist and states sponsor hackers specifically to do that. If your government sponsors you then you have nothing to fear with regard to being caught because you're politically permissioned in a plausibly deniable scenario.
The fact is it is illegal but in warfare the laws get broken by combatants. Soldiers in war don't abide by civilian laws. As far as who is responsible for a specific hack? It's a matter of who would have the most to gain and who benefits from it because it might not be possible to determine exactly who was responsible.