The legal troubles at the scandal-plagued bank Wells Fargo  (WFC) - Get Report keep adding up -- with the cost of settlements and forgone revenue over the past three years now at $4.5 billion and counting.

In fact, the oversight provided by executives and the board of directors was so bad that shareholders are now getting money back -- from the bank's insurers.

A regulatory filing this week showed that San Francisco-based Wells Fargo has reached an agreement to resolve shareholder derivative lawsuits alleging "breach of duty claims, among others, against current and former directors and officers for their alleged failure to detect and prevent sales practice issues."

Wells Fargo's insurance carriers will "pay the company approximately $240 million for alleged damage to the company," according to the filing. In other words, the money theoretically would go back into the bank's corporate treasury for the benefit of shareholders.

The matter stems from Wells Fargo's notorious admission that it opened millions of unauthorized accounts in the names of customers, part of an effort by employees to hit aggressive sales targets set by management. The activity went on for years before erupting in 2016, leading to the departure of former CEO John Stumpf and eventually draconian sanctions from the Federal Reserve.  

The legal and regulatory woes have weighed on Wells Fargo's stock price, with a total return including dividends of just 15% in the past three years, vs. 52% for the Standard & Poor's 500 Index of large U.S. stocks and 97% for the country's biggest bank, JPMorgan Chase  (JPM) - Get Report .   

Partly in response to the Federal Reserve's order, Wells Fargo has had to shake up its board, leading to the departure of several directors who failed to prevent the abusive customer practices. They include Enrique Hernandez Jr., a private-security company owner who, after landing a seat on the board, successfully lobbied former CEO Richard Kovacevich for lucrative side deals guarding some of the bank's branches in California.       

Under the latest agreement, Wells Fargo will pay the plaintiffs' attorney fees under the agreement, the disclosure showed. 

In an e-mail, Wells Fargo spokesman Peter Gilchrist said the bank wouldn't provide additional details on the agreement beyond what was disclosed in the filing, which didn't specify the attorneys' fees or the names of the insurance companies.   

Kevin LaCroix, who tracks management-liability issues as executive vice president of the claims advisor RT ProExec, said in a phone interview that he reviewed the underlying settlement documents and that they stipulate $68 million of attorneys' fees. 

The payment from the insurers, as disclosed by Wells Fargo, would represent the second-biggest recovery on record for a shareholder-derivative lawsuit of its kind, after Activision Blizzard Inc.'s (ATVI) - Get Report $275 million settlement in 2014, LaCroix said.

"It is fairly portrayed as a benefit back to the shareholders," LaCroix said. "No matter how you slice it, it's a lot of money." 

A pyrrhic victory, for sure. But finally, a little something back for the shareholders.