Here's a simple razor: if the people who wrote the rules, upon learning of what you are doing, would likely have written the rules differently to make illegal what you are doing, it's against the "spirit" of the rules.
Example: if some tax code says, people who make below $100k must pay $10k in taxes, and people who make above $100k must pay $20k, paying someone $99,999 to avoid extra taxes is a foreseeable consequence and therefore not an unethical loophole. However, paying someone exactly $100k so they fall in neither bracket and thus need not pay any taxes is probably not what the policymakers intended; they would surely have amended the rules had they had this pointed out to them before enacting this tax code.
Of course, this requires being a judge of intent, but what ethical framework doesn't have grey areas?