The rapid propagation of computer virus is one of the greatest threats to current cybersecurity. This work deals with the optimal control problem of virus propagation among computers and external devices. To formulate this problem, two control strategies are introduced: (a) external device blocking, which means prohibiting a fraction of connections between external devices and computers, and (b) computer reconstruction, which includes updating or reinstalling of some infected computers. Then the combination of both the impact of infection and the cost of controls is minimized. In contrast with previous works, this paper takes into account a state-based cost weight index in the objection function instead of a fixed one. By using Pontryagin's minimum principle and a modified forward-backward difference approximation algorithm, the optimal solution of the system is investigated and numerically solved. Then numerical results show the flexibility of proposed approach compared to the regular optimal control. More numerical results are also given to evaluate the performance of our approach with respect to various weight indexes.