I'd imagine its origin is similar to how old clothes left in storage can end up being eaten apart by moths after not being used for so long and is filled with holes when you finally take it out of storage?
The analogy being that brainworms are what happens when people haven't used their brain in a long time, don't think through the things they believe and so on. Their brain is simply filled with whatever has happened to worm its way in there, like Q-Anon conspiracy shit and the like.