The middle east is a loving mess. Not even just terrorism in general, there are still places (DEVELOPED loving COUNTRIES, AHEM SAUDI ARABIA) which have literally stoned people to death for being gay. Extremism is just completely rampant in the middle east.
A war isn't going to change that. If you target them based on their beliefs they're just going to think "They attack us because they fear us. And they fear us because they know we're doing something right." and they're going to make their ill-held beliefs even stronger. It needs to be more subtle and more political than that, but if military intervention is necessary (like with CIA) then it will need to be done.
obviously political change will need to happen in saudi arabia and iran where they literally sponsor extremism because of all their proxy wars with each other
the borders should probably be redrawn in several middle eastern countries, because a lot of the conflicts are sectarian as well