Defense lawyers say the prosecution is based on insufficient evidence and dubious police tactics that targeted a mentally unstable man

Receive the latest local updates in your inbox

Ahmed Ferhani, seated in center, appears in court in New York, Tuesday, March 13.

A judge ruled Monday that an unusual state-level terror case against a man charged with plotting to blow up New York synagogues can go forward.

Ahmed Ferhani's lawyers have argued that the prosecution was based on insufficient evidence and dubious police tactics that targeted a mentally unstable man.

But the Manhattan district attorney's office says Ferhani was intent on attacking a synagogue and bought three guns and a grenade to do so. The buy was a sting.

Ferhani and co-defendant Mohamed Mamdouh were arrested last year and have pleaded not guilty.

A grand jury declined to indict the men on a top-level terror conspiracy charge but they still face other terror and hate crime charges that carry up to 32 years in prison.

Ferhani, a 27-year-old Algerian immigrant, and Mamdouh, a 21-year-old American citizen of Moroccan descent, were arrested in May 2011. Authorities said the two planned an attack to avenge what they saw as mistreatment of Muslims around the world.

Ferhani suggested posing as a worshipping Jew so he could infiltrate a synagogue and leave a bomb inside, prosecutors have said in court documents.

"He needed weapons to execute the plan that he had been building toward for weeks and weeks and weeks," Assistant District Attorney Margaret Gandy said at a March hearing.

But Ferhani's lawyers said police manipulated a man they knew had psychological problems to turn ambiguous remarks and a staged weapons buy into a trumped-up terror case.

"They find this man who is mentally ill, who has a criminal record, who has a drug problem and they target him," attorney Elizabeth Fink said at the March hearing. "What's happening in the city of New York is evil, judge. It is wrong, and this case will expose it."

She termed the case a product of what she called the New York Police Department's "unconstitutional and illegal" surveillance of Muslims. In court papers, Fink cited stories by The Associated Press that described efforts to monitor Muslim student groups throughout the Northeast and to compile the names of Moroccan cab drivers. Police officials and Mayor Michael Bloomberg have said the surveillance effort was legal, constitutional and vital to protecting the city after the 2001 terrorist attacks.

Ferhani has been institutionalized for psychiatric problems as many as 30 times — at least five of them after his family called police, his lawyers have said.

While most terror cases are federal, Ferhani and Mamdouh were charged under a state terror law passed shortly after the Sept. 11 attacks.

They are being held without bail.

Get the latest from NBC 4 New York anytime, anywhere. Follow us on Twitter, Facebook and Google+. Get our apps here and sign up for email newsletters here. Get breaking news delivered right to your phone -- just text NYBREAKING to 639710. For more info, text HELP. To end, text STOP. Message and data rates may apply.