Two Brooklyn toughs on trial in a horrifying torture, rape and murder case made a brazen escape attempt Thursday — lunging for court officers' guns and stabbing a lawyer in the neck as terrified jurors and spectators ran shrieking.

"He's going for the gun!" officers shouted as Troy Hendrix, 22, vaulted over the defense table and co-defendant Kayson Pearson, 23, lunged backward toward the gallery.

Relatives of the victim sitting behind the men said the two exchanged winks before all hell broke loose. Court officers immediately flooded the room, tackling both men.

Pearson stabbed his lawyer, Mitchell Dinnerstein, under the chin with a concealed Plexiglas shank.

Bleeding profusely, Dinnerstein managed to knock Pearson to the floor, helping officers foil the escape.

Sgt. Tom Reilly said the men had hidden shanks in their underwear.

About half the spectators — most of them relatives of the victim — fled the second-floor room in Brooklyn Supreme Court.

"One defendant attacked the attorney with a weapon to cause a diversion, while the other came after me to grab my weapon," said Sgt. James Gorra. "I hit his arm as hard as I could and at the same time grabbed him and twisted him and threw him to the ground. We were rolling around. I remember screaming out, 'He's going for my gun!'"

Court officers kicked, and shot pepper spray at Pearson, who was subdued near the holding cells.

Hendrix was punched and also hit with pepper spray on the ground near the bench where Justice Albert Tomei was presiding.

Jurors — filing out after hearing the first day of testimony — hit the deck.

Elle Carmichael, the mother of murder victim, Ramona Moore, had testified just minutes earlier. She collapsed and wailed hysterically after the melee.

"They could have killed everybody in that room!" she cried. "Hang them right now by the neck!"

Joan Rafferty, the mother of Brooklyn Detective Patrick Rafferty, who was murdered in a double-cop shooting in 2004, was in the courtroom next door at the trial of her son's alleged killer. She offered words of comfort to Carmichael.

Hendrix, shackled and cuffed, wearing just shorts and a T-shirt and breathing through an oxygen mask, looked dazed as he was wheeled to an ambulance. But he was alert enough to flip his finger at reporters and photographers.

Pearson came out shackled on a gurney, but breathing on his own.

Dinnerstein was taken to Long Island College Hospital, where he received stitches and was released.

Seven court officers were slightly hurt. Courts spokesman David Bookstaver said the inmates would be charged with assault and attempted escape and resisting arrest.

Correction spokesman Tom Antenen said a makeshift metal handcuff pick was found in the rectal area of one of the men.

He said defendants are strip-searched and go through a metal detector before they leave Rikers Island, the New York City's maximum-security jail, and again at the courthouse. Officials will investigate how the two sneaked weapons into the court.

Pearson and Hendrix face life in prison for allegedly abducting Moore, a 21-year-old Hunter College psychology student, on April 24, 2003. They allegedly raped, beat and tortured her in Hendrix's basement before killing her with a hammer and dumping her body under a truck.