In this paper, we study the spread of virus/worm in computer networks with a view to addressing cyber security problems. Epidemic models have been applied extensively to model the propagation of computer viruses, which characterize the fact that infected machines may spread malware to other hosts connected to the network. In our framework, the dynamics of hosts evolves according to a modified inhomogeneous Susceptible-Infectious-Susceptible $\left( \text{SIS} \right)$ epidemic model with time-varying transmission rate and recovery rate. The infection of computers is subject to direct attack as well as propagation among hosts. Based on optimal control theory, optimal attack strategies are provided by minimizing the cost (equivalently maximizing the profit) of the attacker. We present a threshold function of the fraction of infectious hosts, which captures the dynamically evolving strategies of the attacker and reflects the persistence of virus spreading. Moreover, our results indicate that if the infectivity of a computer worm is low and the computers are installed with antivirus software with high reliability, the intensity of attacks incurred will likely be low. This agrees with our intuition.