With the rapid development of science and technology, the application of wireless sensor networks (WSNs) is more and more widely. It has been widely concerned by scholars. Viruses are one of the main threats to WSNs. In this paper, based on the principle of epidemic dynamics, we build a SEIR propagation model with the mutated virus in WSNs, where E nodes are infectious and cannot be repaired to S nodes or R nodes. Subsequently, the basic reproduction number R0, the local stability and global stability of the system are analyzed. The cost function and Hamiltonian function are constructed by taking the repair ratio of infected nodes and the repair ratio of mutated infected nodes as optimization control variables. Based on the Pontryagin maximum principle, an optimal control strategy is designed to effectively control the spread of the virus and minimize the total cost. The simulation results show that the model has a guiding significance to curb the spread of mutated virus in WSNs.