WASHINGTON — Images posted to social media after the attack in Iraq on Wednesday that killed three service members, two American and one British, showed a dilapidated white flatbed truck affixed with primitive rocket tubes tucked among desert shrubbery.

These types of photographs have often appeared in the past 18 years of United States wars in the Middle East as insurgent groups have relied on rudimentary but effective tactics to maim and kill their technologically superior enemies.

But in recent months, with relatively few American troops still in Iraq, militias there with ties to Iran seem to have perfected a strategy that has left U.S. forces with little recourse to defend themselves, according to American officials, who are scrambling to put effective countermeasures in place.

The attack in Taji, Iraq, home to a sprawling base roughly 15 miles north of Baghdad, involved about 30 rockets. More than a dozen hit the base, killing and wounding nearly 20 people.