The odds are stacked against any state or city that guns for Fannie and Freddie. The companies have become extra-legal institutions since they were bailed out and nationalized in 2008. It’s not obvious that the state has the law on its side, but that’s what makes the public act of taking on Fannie and Freddie in a Boston courtroom so important. The nationalized mortgage giants need to be shamed into doing the right thing, because it’s not clear they can be reined in any other way.

The city of Chicago mounted the last serious bid at restraining Fannie and Freddie, in a fight that began in late 2011. At the time, the city was overrun by foreclosed properties that were radiating blight. So the city enacted a tough ordinance that established a citywide database of vacant properties, required that banks keep the properties they’d taken from homeowners in good condition, and levied stiff fines against lenders that didn’t comply.

Fannie and Freddie responded by suing Chicago, arguing that their nationalization made them exempt from local regulations and levies. A federal judge agreed. Fannie and Freddie later agreed to voluntarily register their vacant properties with Chicago, but the message behind the legal challenge was clear: The companies believe they’re above the law, until they decide otherwise.

The lawsuit Coakley filed last week is the most aggressive bid at reeling in Fannie and Freddie since the Chicago fight. Coakley’s lawsuit made Boston the front line in a years-long, nationwide effort to humanize the approach Fannie and Freddie take to foreclosures.

At first blush, it looks like it’s up against the same kind of hurdles that doomed Chicago’s attempt to regulate the companies. But the state doesn’t necessarily have to win in court to effect change. In picking the public fight, Coakley is opening a window on the absurdity of a federal housing apparatus that won’t hold itself to the same standards it sets for others.

Mortgage principal forgiveness and nonprofit-led buybacks are at the heart of a $25 billion foreclosure relief settlement that federal officials forced on the country’s biggest banks two years ago. But Fannie and Freddie refuse to follow suit. They’re currently in federal court, fighting a lawsuit brought by a Dorchester homeowner who claims that Freddie Mac’s years-long refusal to sell his foreclosed home to a local nonprofit violates a 2012 Massachusetts foreclosure prevention law.

The nonprofit, Boston Community Capital, sells foreclosed homes back to their former owners. Fannie and Freddie have a blanket policy against selling to entities that are engaged in foreclosure buybacks, but Massachusetts law explicitly forbids owners of foreclosed properties from attaching any conditions to the sale of a foreclosure when dealing with nonprofits. Lawyers for Freddie have been fighting the Dorchester case on narrow legal grounds, but court filings show Freddie representatives idly threatening to sue to overturn the Massachusetts law. Coakley’s lawsuit makes a preemptive strike against such a challenge. It throws needed light on the rules Fannie and Freddie make for themselves. And, hopefully, it will shame them into behaving.

Paul McMorrow is an associate editor at Commonwealth Magazine. His column appears regularly in the Globe.