According to a RAND study I wrote several years ago, insurgencies that received support from external states achieved their aims more than 50 percent of the time, while those with no support won only 17 percent of the time. But that’s not all. Insurgents have been successful approximately 43 percent of the time when they enjoyed a sanctuary. Afghan insurgents enjoy both outside support and sanctuary, a doubly difficult hurdle for the United States and its allies to overcome.

Islamabad’s rationale for supporting Afghan insurgents is straightforward and, in many ways, understandable. Hemmed in by its archenemy, India, to the east, Pakistan wants an ally to the west. It doesn’t have one at the moment. Instead, New Delhi has a close relationship with the Afghan government. Feeling strategically encircled by India, Islamabad has resorted to proxy warfare to replace the current Afghan government with a friendlier regime.

With its focus on Pakistan, “The Wrong Enemy” is a valuable contribution to a hefty body of work on the American war in Afghanistan that has become stale and somewhat hackneyed. It provides a raw, unvarnished and important look at one of the darkest and least understood parts of the Afghan war.

“The Wrong Enemy” is not the first book to grapple with Pakistan’s role in Afghanistan. Others have done so, including Ahmed Rashid’s “Descent Into Chaos” (2008) and Barnett R. Rubin’s “Afghanistan From the Cold War Through the War on Terror” (2013).

But Ms. Gall’s treatment of Pakistan’s role is the most comprehensive. She does not, however, let the Afghan or American governments off the hook. She excoriates President Hamid Karzai of Afghanistan for failing to offer the Taliban a serious peace plan after 2001. She also criticizes him for a litany of faults, from wantonly tolerating government corruption to unnecessarily micromanaging government decisions. And she disparages the United States military for conducting airstrikes that unnecessarily killed civilians and for detaining too many Afghans who were wrongly arrested, falsely accused by rivals, or simply in the wrong place at the wrong time.

While Ms. Gall’s rich anecdotes from interviews with Afghan villagers, Taliban fighters and officials of many governments provide poignant realism to the book, they also highlight one of its few drawbacks. Unlike some of the pre-eminent books on the region, like Steve Coll’s well-sourced Pulitzer Prize-winning “Ghost Wars” (2004), “The Wrong Enemy” occasionally veers into reporting unsubstantiated second- or thirdhand accounts from unnamed sources without providing adequate context — or skepticism — when appropriate. On several occasions, for example, she cites unnamed Afghan security officials, who have a proclivity to blame Pakistan for everything, in highlighting ISI mischievousness in Afghanistan.

Still, “The Wrong Enemy” arrives at an auspicious time, just as the United States is ending its combat mission in Afghanistan. In a passionate plea, Ms. Gall argues in the final chapter that the United States is turning its back on Afghanistan because American leaders are tired of war and mistakenly view Afghanistan as lost. “That is the wrong way to look at the problem,” she writes with palpable emotion. “Pakistan is still exporting militant Islamism and terrorism, and will not stop once foreign forces leave” Afghanistan.

These words have a nostalgic ring. In 1979, the Pakistani leader Gen. Muhammad Zia ul-Haq tellingly remarked to the director-general of the ISI that “the water in Afghanistan must boil at the right temperature.” As the United States ends its combat mission, the cold reality is that Afghanistan’s future hinges just as much on Pakistan as it does on what happens inside Afghanistan’s borders.