© 2020 IEEE.This paper investigates a moving target defense strategy based on detector threshold switching against stealthy sensor attacks. We model the interactions between the attacker and the defender as a game. While the attacker wants to remain stealthy and maximize its impact, the defender wants to minimize both the cost for investigating false alarms and the attack impact. We define the moving target defense as a mixed strategy Nash equilibrium and are able to formulate an equivalent finite matrix game of the original game. We provide a necessary and sufficient condition for the existence of a moving target defense strategy. A globally optimal moving target defense strategy is obtained via a linear optimization problem by exploiting the structure of the matrix game. Simulations with a four tank system verify that by applying an optimal moving target defense strategy, the defender reduces its cost compared to the optimally chosen fixed detector threshold.