A sensor node carries out a specific function in a wireless sensor network. Wireless sensor networks are less bearable but more vital to the industry and the people because of threats. As a result, a node's battery life will be drastically reduced, and the node will be rendered completely inoperable, which is the most extreme form of denial of service attack. A vampire attack, one of the denials of service attacks, may inflict extensive harm to the network, making it harder to detect and using more energy than necessary. This article has proposed a novel vampire attack detection and prevention by the energy consumption prediction in the data path with the least error as low as 1e −3 . In combination with the social spider optimized Gaussian mixture model, the gray prediction model is used to detect and prevent the attack. The energy prediction scheme calculates a cooperative trust score and is categorized by the optimized Gaussian mixture model. The algorithm is validated with recent state-of-the-art schemes and the detection accuracy improvement of up to 35.27% is achieved.