The openness of Wireless Sensor Networks (WSNs) layout makes them vulnerable to attacks. One of the most threatening attacks is the wormhole attack. The wormhole attack is difficult to be detected since it causes false routing by private tunnels and damages to WSNs in data transmission. Therefore, in order to resist wormhole attacks and improve network performance, this paper proposes a detection algorithm integrated with the node trust optimization model (NTOM-DA) against wormholes in WSNs. First, add the node whose number of neighbors exceeds the threshold to the list of suspicious nodes, and then the exclusive neighbors of suspicious nodes communicate with each other. Mark the path whose hops exceed the wormhole threshold as the path to be tested. Establish a trust model to calculate the node trust and evaluate the path trust. The simulation results show that NTOM-DA is superior in detecting wormhole attacks. Even in the face of a network with high node density and high attack degree, it still has a high detection rate and a low false-positive rate, which effectively guarantees the safe and reliable operation of WSNs.