The increasing complexity of current wireless sensor networks requires efficient methodologies to fulfill the strict constraints typically imposed in terms of power consumption and system performance. Furthermore, security issues are also becoming key features due to their impact on system behavior. As a consequence, new design frameworks are required to enable developers to model and address security risks from the very beginning of the WSN design process, while optimizing system performance. For this purpose, this article presents a design framework for modeling and simulating WSNs under external attacks. In this framework, the WSN is specified by using UML/MARTE models, from which automatic code generation enables fast, host-compiled simulation. The resulting information enables early detection of weaknesses in WSN designs and simplifies further exploration of design solutions. Minor modifications in the UML models are sufficient to automatically simulate and evaluate each design alternative in an iterative way. As a result, designers can develop more secure and optimized WSN systems with reduced design times and effort.