A federal judge Thursday barred the guards at Guantanamo Bay, Cuba, from conducting genital searches of detainees before meetings with their attorneys and described the practice as an “exaggerated response” to security concerns.

“The choice between submitting to a search procedure that is religiously and culturally abhorrent or foregoing counsel effectively presents no choice for devout Muslims like petitioners,” U.S. District Judge Royce C. Lamberth wrote.

The decision to search the groins of detainees was introduced in May by Col. John V. Bogdan, the commander of Joint Task Force Guantanamo Detention Group, after the military said it found contraband in cells, including homemade weapons and prohibited electronic devices, such as iPods. The searches entailed a guard placing his hand “as a wedge between the [detainee’s] scrotum and thigh . . . and using [a] flat hand to press against the groin to detect anything foreign attached to the body.”

Lamberth, chief judge in the federal courthouse in Washington, noted that previous commanders at the detention facility had avoided such intrusive searches and that guards were “limited to grasping the waistband of the detainee’s trousers and shaking the pants to dislodge any contraband.”

Lawyers for detainees had argued that the motivation for the search procedure was not to enhance security but to isolate detainees from their attorneys in an effort to crush a growing hunger strike at the base. The hunger strike began in February as a reaction to guards searching detainees’ Korans. More than two-thirds of the 166 detainees at Guantanamo are participating in the protest, with more than 40 being force-fed.

Lamberth said the military’s action had to be judged in light of previous actions that limited the ability of attorneys to meet with their clients.

“As petitioners’ counsel correctly noted during this Court’s hearing, ‘[t]he government is a recidivist when it comes to denying counsel access,’ ” Lamberth wrote.

Attorney David Remes, who represents one of the detainees who sought emergency relief from the courts, welcomed the judge’s decision.

“The decision is another sharp rebuke to the government,” he said. “The government has no credibility with the court when it comes to policies that restrict counsel access.”

Lt. Col Samuel House, a spokesman for Joint Task Force Guantanamo, said, “We are aware of Judge Lamberth’s ruling, and JTF will continue to follow the law.”

A Justice Department spokesman said that the agency is “reviewing the court’s decision.”

Sari Horwitz contributed to this report.