Cognitiveradio (CR) technology presents a mechanism for efficient spectrum usage.Spectrum sensing is an essential CR function which includes an intelligentsignal processing algorithm to identify the vacant frequency bands. Cooperativespectrum sensing (CSS) has been widely adopted to improve the performance of CRnetworks. Unfortunately, CR networks are vulnerable to security threats. Inthis study, we propose an optimal threshold selection approach to address oneof the most important attacks called primary user emulation attack (PUEA). InPUEA, a malicious attacker mimics some important primary signal features anddeceives CR sensors to prevent them from accessing the available channels. Inthis study, we assume a malicious PUEA which is relatively located near thepotential user (PU) transmitter and senses the spectrum and accurately detectsthe vacant frequency bands to transmit its fake signal. We estimate attackstrength and then apply the K-out-of-N rule to obtain an optimum and dynamicthreshold K, minimizing the global error probability. Here, the attack strengthis defined as the ratio of the average transmission power of the PUEA to theaverage power of the PU. The achieved simulation results indicate that theperformance of the suggested method is satisfactory in detecting the maliciousPUEA compared with conventional methods.