* Guards allowed prisoners leave cells

* Inmates must pay bribes for even basic goods

* Mexico hopes new prisons will quell overcrowding

MEXICO CITY, Aug 18 (Reuters) - The drug hitmen accused of gunning down dozens of people in gruesome attacks in northern Mexico weren't worried they would get locked up for years in prison -- they were already behind bars.

Officials say prison guards lent guns and vehicles to the inmates, jailed for links to powerful drug cartels, and opened the doors of the jail in Durango state so they could carry out revenge attacks before returning to their cells for the night.

The scandal uncovered last month laid bare the corruption and chaos that reigns inside Mexico's prison system and undercuts President Felipe Calderon's faltering war on powerful drug cartels.

Experts say Calderon needs to get entrenched problems in the penal and judicial system under control if he is to have any chance of winning a war that has claimed more than 28,000 lives since late 2006 and sparked fears that the cartels could turn Mexico into a lawless narco state.

At the heart of the problem is the river of bribes coursing through Mexican jails, from the few pesos inmates pay each day to get food and toilet paper to the fortunes that jailed drug lords pay to live in luxury or escape when they please.

"The authorities no longer control the prisons -- the drug lords do," said Pedro Arellano, a veteran prisoner rights activist. "The prisons have become officials' petty cash box."

In many of Mexico's overcrowded prisons, drug suspects use money and influence to run their businesses from the inside, and to recruit new cartel members among fellow inmates.

"On the outside we do jobs for the bosses who are in prison," said one drug dealer in border city of Tijuana. "A lot of people think that when the big guys are arrested, it's over. But no. They are even more protected (in jail)."

In 2008, the government passed a law designed to overhaul the inefficient justice system, including a clause to separate dangerous criminals charged with federal crimes, like drug trafficking, from lesser offenders in Mexican jails.

More than 40,000 inmates charged with federal crimes are locked in state jails, where cartel hitmen are often housed aside petty thieves, underage inmates, or those awaiting trial. Experts believe this breeds more hardened criminals.

The government plans 15 new modern federal prisons by 2012 to ease overcrowding in Mexican jails, where the number of inmates has more than doubled since 1997 to 227,000 inmates.

Family members say prisoners in Mexico City are forced to sleep on the floor or create makeshift hammocks from sheets.

The U.S. government, worried about increasing drug violence in its backyard, is spending up to $29 million to support prison guard training and other assistance. A new police academy opened last year in Veracruz state with U.S. help.

But low low salaries and intimidation from inmates are incentives for even the most well-trained guards to go astray.

"When you have someone who is struggling to make ends meet, for the most part has little education, and then also put the component of 'bullet or bribe,' you start weighing the odds," a U.S. official in Mexico said.

PESOS FOR PRIVILEGES

In one notorious case, Mexico's top drug trafficker escaped from a maximum security prison in 2001 hidden in a laundry van. Over 70 guards were investigated for taking part in the escape by Joaquin "Shorty" Guzman, who reportedly paid 'salaries' to penitentiary officials of up to $4,500 a month.

At least 134 inmates have escaped Mexican jails since 2008, many of them drug gang members sprung by heavily armed convoys or by complicit guards who let them walk out the front door.

The flip side of wealthy capos' privilege is the plight of the anonymous poor inmates, often held for long periods without trial, struggling to scrape together funds to survive.

A 2007 study by the Open Society Institute found almost half of Mexican inmates are held without being convicted. The study's author, Guillermo Zepeda, said prisoners in pre-trial detention pay about 539 million pesos ($43 million) a year in bribes for everything from cell space to air conditioners.

"The jails are money-making factories, but the cost is prisoners' dignity," said Hector Cerezo, who spent more than seven years in a maximum security jail.

Small-time offenders often must earn money by working for wealthy traffickers, cleaning cells or washing clothes, said Diana Rodriguez, who runs a prisoners' family support group.

She is skeptical new efforts to improve prison guards will suffice. "You can't just throw one good apple in with all the rotten ones. You would have kick out the entire bunch." (Editing by Kieran Murray) ($1=12.60 pesos) (Additional reporting by Lizbeth Diaz in Tijuana; editing by Missy Ryan and )