Town officials in Rockport, Maine, were closing up shop on Friday, April 13, when they realized they couldn’t open files on their computers.

After fielding messages from town workers, local information-technology contractor Gus Natale said he “went straight to the town office and started yanking plugs.”

An unknown hacker had snuck malicious...