Wireless Sensor Network (WSN) technology has received a lot of attention and has opened new applications. Examining security assaults on the network layer and coming up with a solution for them is one of the difficult topics in modern networks. Therefore, to achieve our goals, we must develop a technique that can identify an attack, prevent the attacker from accessing the network, and do so while consuming the fewest amounts of battery power. This technique must also use a simple, reliable algorithm. The use of a genetic algorithm is suggested in this research as a detection of wormhole attacks in WSNs. The strategy makes use of a genetic algorithm to investigate and identify the ideal group of parameters for a wormhole detection technique. Even in the case of enormous networks, the search for the most effective parameters may be carried out quickly and effectively by using this genetic algorithm.