Wireless sensor networks (WSNs) are susceptible to numerous security threats due to their reliance on open environments and broadcast communication methods. Among these, the selective forwarding attack is notably challenging to detect. This difficulty arises from the ability of malicious nodes to imitate the behavior of normal nodes, and selectively drop data packets, which makes them virtually indistinguishable from normal ones, particularly under conditions of poor channel quality. To address this challenge with harsh environments, we introduce a novel methodology termed GD3N. This approach is underpinned by the design of a unique type of data point that encapsulates both short-term and long-term forwarding behaviors of nodes. It combines a refined version of the Gradient Diffusion Density-based Spatial Clustering of Applications with Noise (GD-DBSCAN) algorithm, with a novel Double-Parameter Neighbor Voting (DP-NV) method based on the data set. These innovations contribute to a significant enhancement in detection accuracy and a reduction in computational complexity when compared to traditional DBSCAN and NV methods. Simulation results show that our GD3N achieves a false detection rate (FDR) of less than 2%, a missed detection rate (MDR) of below 10%, and an overall detection accuracy rate (DAR) of over 95% across various testing scenarios.