The former US Marine who shot dead a dozen people inside a packed California bar late Wednesday was using a .45-caliber handgun that was legally purchased — but illegally modified, officials said.

Ian David Long, 28, stormed into the Borderline Bar & Grill, a country music-themed venue popular with college students in Thousand Oaks, Calif., around 11:20 p.m. and opened fire into the crowd with his Glock 21, said Ventura County Sheriff Geoff Dean.

The weapon, which was legally purchased in Ventura, was designed to hold 11 rounds, but had been fitted with an extended magazine, allowing Long to unleash more carnage before stopping to reload.

Such a modification is illegal in California, said Dean, who couldn’t immediately say how many rounds Long’s Glock was capable of holding.

Long killed 12 people, including a married sheriff’s sergeant looking forward to retirement, before he took his own life.