Saudi Arabia is fighting a growing threat from the Islamic State that is both breeding homegrown terrorists and using the kingdom's conservative religious teachings to undermine the monarchy.

Saudi security forces say they detained 17 people this week who belonged to three cells that had ties with IS. The government said the suspects were reportedly planning four major attacks on security and economic targets in the country, according to the state-run Saudi Press Agency.

Hundreds of people with alleged ties to IS have been arrested since IS declared war on the kingdom in 2014. Saudi officials said IS-affiliated terror cells carried out several deadly shootings and bombings, many of them targeting security forces and Shi'ite mosques.

IS "presents a very serious threat, not just to Europe and the United States ... but inside of Saudi Arabia," John Brennan, the director of the U.S. Central Intelligence Agency, said recently.

For Saudi Arabia, battling IS presents a double challenge. Besides undermining the kingdom through violence, IS also wants to undercut the belief system of the monarch known as Wahhabism.

Doctrine's birthplace

Saudi Arabia is the birthplace of Wahhabism, an austere Sunni doctrine credited with inspiring the radical ideology of IS. The Islamic State group accuses the Saudi monarchy of using Wahhabism to legitimize its rule, particularly with its custodianship of Mecca and Medina, Islam's holiest sites. Saudi groups linked to the conservative theology have been accused of sending funds to help IS expand abroad.

FILE - In this image taken from video provided by FILE - In this image taken from video provided by Saudi TV, burned-out cars are seen as investigators collect evidence in the aftermath of a suicide bomb outside the Imam Hussein mosque in the port city of Dammam, Saudi Arabia, May 29, 2015. The Islamic State group said one of its soldiers had carried out the attack. FILE - In this image taken from video provided by Saudi TV, burned-out cars are seen as investigators collect evidence in the aftermath of a suicide bomb outside the Imam Hussein mosque in the port city of Dammam, Saudi Arabia, May 29, 2015. The Islamic State group said one of its soldiers had carried out the attack.

"IS has two primary objectives in Saudi Arabia," said Abdullah Ghawdi, a journalist at the Saudi Okaz newspaper in Riyadh. "One is to undermine the Saudi security forces and the other one is to target religious scholars."

In July, before the start of the pilgrimage season, IS-linked militants staged a suicide attack in Mecca and Medina, killing several security officers. IS has also been behind attacks on mosques belonging to the Shi'ite minority in the eastern part of the country.

"Salafi jihadism was originated in Saudi Arabia," said F. Gregory Gause III, a professor of international affairs at Texas A&M University who monitors developments in the kingdom. "But the Saudi government says that the ideology [IS] has embraced is deviant."

IS's campaign against Saudi interests has increased as the kingdom has become more involved in the U.S.-led coalition fighting IS in Syria and Iraq. Saudi Arabia is a major hub for intelligence-gathering against IS.

And IS also has become active in neighboring Yemen, claiming responsibility for killing dozens in August in a suicide bombing. Saudi Arabia and its allies have intervened with airstrikes and military power in the Yemen conflict in support of the exiled Yemeni government.

Directed from top command

Unlike the majority of lone wolf attacks carried out by IS followers in the West, IS attacks in Saudi Arabia have been engineered from the top IS command in Syria and Iraq, analysts say.

"Most of the terror attacks in the kingdoms and the foiled ones have had direct ties [with militants] in Syria and Iraq," said Abdulaziz Sager, who heads the Gulf Research Center, a Saudi think tank.

IS is finding fertile recruiting ground among jihadists who fought for radical Islamist causes abroad, analysts say.

FILE- In this photo released by the Saudi Press Ag FILE- In this photo released by the Saudi Press Agency SPA, Saudi security forces check the damaged mosque inside a police compound after a suicide bombing attack in the city of Abha, provincial capital of Asir, Saudi Arabia, Aug. 6, 2015. An ISIS affiliate claimed responsibility for the attack. FILE- In this photo released by the Saudi Press Agency SPA, Saudi security forces check the damaged mosque inside a police compound after a suicide bombing attack in the city of Abha, provincial capital of Asir, Saudi Arabia, Aug. 6, 2015. An ISIS affiliate claimed responsibility for the attack.

"Many Saudi radicals had joined terrorist organizations in Iraq and Afghanistan," journalist Ghawdi told VOA. "Some of them have returned to Saudi Arabia."

The government has sponsored a rehabilitation program to help reform jihadists. Many of them have been able to return to society. But the rise of IS led some to radicalize again, Ghawadi said.

"Many have joined IS because it was easy to travel to Turkey and cross the border to Syria," he said. "They have maintained contacts with other radicals back home."

Saudi officials have taken steps to limit IS's influence by initiating reforms in the ultrareligious Saudi education system to remove materials from textbooks that could be used by recruiters to radicalize students.

"Our curriculum is currently clear of any texts that might be misunderstood and misused by citizens," said Sadaqa Fadel, a member of the Saudi parliament, known as the Shura Council.

Anti-terror laws

The government also has passed several anti-terror laws "that are more effective in detecting terror cells and suspects," Fadel said.

The 2016 U.S. State Department report on religious freedom credited Saudi Arabia with "making revisions to remove intolerant passages from textbooks and curriculum." However, the report said the Saudi government still "remains uniquely repressive in the extent to which it restricts the public expression of any religion other than Islam."

Saudi Arabia prohibits any non-Muslim public places of worship, and the report said "a 2014 law classifying blasphemy and advocating atheism as terrorism has been used to prosecute human rights defenders and others."

Still, analysts say Saudi Arabia is likely to be targeted by more IS attacks, though IS's impact is ultimately limited.

IS "poses a threat to Saudi Arabia's stability," Saudi watcher Gause said. "But it can't threaten to overthrow the monarchy."