In the end, it was the prospect of a nap that made 9/11 terror mastermind Khalid Sheik Mohammed sing like a canary.

“Even with waterboarding, he was counting on his fingers, because he knew we would stop at 10, so he wasn’t terribly intimidated by that,” ex-CIA official José Rodriguez Jr., told The Post.

“It was the sleep deprivation that finally got him.”

Still, the man described as the Hannibal Lecter of al Qaeda wasn’t easy to crack.

It took two weeks to break him after he was nabbed in Rawalpindi, Pakistan, in 2003 and quickly squirreled away to a secret CIA prison called a “black site.”

CIA officers started on him slowly, asking the Kuwaiti-born terrorist if he planned to cooperate.

He chanted Koranic verses in defiance.

He was asked about Osama bin Laden.

No answer.

He was asked about future terror plots.

“Soon you will know.” The answer cast a pall on the room.

‘We couldn’t idly sit by and wait for a chance to bond with our detainee or for him to see the error of his ways and open up to us,” Rodriguez, 64, writes in his new book, “Hard Measures.”

FAMILIES OUTRAGED AT TRIBUNAL’S FARCE

9/11 PLOTTER & HENCHMEN RAISE A RUCKUS AT GITMO

Rodriguez, who oversaw the CIA’s interrogation and detention program, stridently defends the techniques used to extract information from detainees, saying the methods were all legally sanctioned by the Bush administration — and thwarted terror plots.

“No one enjoyed doing it, but we were absolutely convinced that [high-level detainees] had information in their heads that would save countless American lives. We were right.”

He says there were no similarities between the interrogation techniques his officers used and the Army troops exposed for illegally abusing Iraqis at the Abu Ghraib prison. Each CIA method was fully cleared by an array of lawyers, who were ubiquitous, Rodriguez explains.

When Mohammed refused to cooperate, the agency’s “enhanced interrogation techniques” kicked in, Rodriguez writes.

Mohammed, 48, was subjected to a range of methods. They may have included dietary restrictions, where prisoners wore diapers and were fed only the dietary shake Ensure. Leaving detainees naked in their cells was also used.

And there was “walling,” in which interrogators pushed him backwards in a small plywood-walled room so that his shoulder blades bounced off the wall with a “boom.”

“We were after the shock value,” Rodriguez says.

Other “corrective techniques” included the “attention grasp,” where a detainee is grabbed on both side of the collar and yanked towards the interrogator; the “facial hold” where an interrogator places a hand on each side of the detainee’s face and holds it immobile; and the “insult slap,” where the person is slapped between the chin and the bottom of his earlobe.

“More than one detainee expressed surprise when slapped, and told the interrogator, ‘Hey, you aren’t supposed to do that!’ The al Qaeda training manual told them that Americans would treat them with kid gloves!” Rodriguez writes.

But KSM was unlike any of the 30 other CIA detainees subject to such techniques: He was “scary smart” and evil, Rodriguez says. He was one of only three prisoners waterboarded by the United States. But Mohammed knew his interrogators would never kill him, so he coolly remained impassive as he was waterboarded 183 times, Rodriguez says.

“He seemed to have figured out that we weren’t going to push things too far,” Rodriguez says.

But Mohammed’s weakness was his inability to resist sleep deprivation.

The first day he was in custody, Mohammed — who attended college in Greensboro, NC — initially pretended to only speak Urdu, fooling no one. Officers forced him to stand, and after hours of questioning, his weakness for shut-eye began to show.

“Here’s the deal,” an interrogator said. “I know you speak English. I want you to politely ask me to let you go to sleep.”

The idea was to demonstrate to Mohammed “that he was no longer in control,” Rodriguez says. Officers would later keep him awake for 180 hours straight — 7 1/2 days. Loud noises and stress positions — where a detainee is shackled and forced to stand, putting intense pressure on the leg muscles — were used.

“It was much kinder than anything he would have done to an American captive, like Danny Pearl,” said Rodriguez, referring to the Wall Street Journal reporter Mohammed admitted to personally beheading in 2002.

When he finally folded, officers couldn’t get him to stop talking.

“Once they understand that they’ve come to the limits of their strength that their god will give them, they cooperate because they cannot resist beyond their strength,” explains Rodriguez, who said information extracted from Mohammed helped foil a range of terror schemes, including 9/11-style attacks on the West Coast in 2003.

Rodriguez contends information obtained from Mohammed and fellow terrorist Abu Zubaydah accounts for more than 50 percent of the terror-plot section of the 9/11 Commission report.

Mohammed was arraigned yesterday at Guantanamo Bay on charges of terrorism and murder in the 9/11 attacks that killed 2,976 innocent people. He faces the death penalty if convicted.

Although he acted defiantly in court, Rodriguez said KSM would like nothing more than a forum to preach radical Islam.

“This is a process that will continue for a long time,” Rodriguez said. “I have heard he may plead not guilty, and if he does, he’ll use the [legal] process as his platform . . . to talk about his jihadist beliefs.”

One of Mohammed’s frequently stated goals was to be put on trial in civilian court in New York — which nearly happened until Congress last year blocked the Justice Department from transferring any Guantanamo Bay detainees to the United States.

“It seemed to us that he was looking for a platform from which he could spout his hatred for all things American, and a trial would certainly present that opportunity,” Rodriguez writes. “It strikes me as more than a little ironic that several years later, Attorney General Eric Holder almost granted KSM his wish.”

Once he became compliant, Mohammed developed a rapport with his interrogators, watching PG-rated movies with them in his cell, and offering a religion-themed overview of the “history of the world.”

“A few months later, he reported that he was ready to continue and build on his earlier presentation. He had one requirement, however. Only those officers who sat through the prerequisite first session should be invited to the [second] session,” Rodriguez recalls.

He even penned “playful” notes to them, Rodriguez says. “Unless you are trying to manipulate me, could you turn up the heat a bit?” the terrorist asked in one missive.

But it was all a facade.

After telling an officer to have a “safe trip” before he left for home, Mohammed continued, “It is not that I wish you well. But if I ever get out of here, I want to personally be the one to kill you.”

By his own admission, Mohammed’s done it before.

“In a confession he later submitted for a potential tribunal in Guantanamo Bay, Cuba, KSM wrote: ‘I decapitated with blessed right hand the head of the American Jew, Daniel Pearl, in the city of Karachi, Pakistan,’ adding, ‘For those who would like to confirm, there are pictures of me on the Internet holding his head.’”

Washington analysts had Mohammed pose holding a sack with a bowling ball in it, so they could compare his arm to those in the video cutting Pearl’s throat.

“Those photos compared to the actual video showed that KSM was not lying to us,” Rodriguez writes. “Just when you thought he had a human face, he would disappoint you.”

The former director of the CIA’s National Clandestine Service told The Post that any claims that Mohammed was illegally tortured are “bulls–t,” and that the reputed terror goon had a master plan all along.

In his book, Rodriguez recalls an unsettling prediction: “At least two of our people at the black site told me that KSM made an observation that would later prove eerily accurate. Talking about his interrogation and that of his colleagues, he said: “You know, some day your government is going to turn on you.”

Rodriguez, the Puerto Rican-born son of two teachers has admitted to destroying 92 tapes showing the agency’s interrogations and was cleared of any criminal charges after a three-year investigation. He says he did it to thwart a propaganda campaign against the United States, but mostly to protect the safety of his officers.

“This is America. We have a right to disagree. That doesn’t bother me,” says Rodriguez, who retired from the CIA in 2007 after 31 years. “I would do it all over again.”