AbstractWireless sensor network (WSN) works with a collection of multiple sensor nodes to fetch the data from the deployed environment to fulfill the application whether it is agricultural monitoring, industrial monitoring, etc. The agricultural region can be monitored by deploying sensor nodes to multiple verticals where continuous human presence is not feasible. These devices are equipped with limited resources and are easily vulnerable to various cyber‐attacks. The attacker can hack the sensor nodes to steal critical information from WSN devices. The cluster heads in the WSN play a vital role in the process of routing data packets and attackers launch malicious codes through sender nodes to hack or damage the cluster heads to shut down the entire deployed network of agricultural regions. This research paper proposes a framework to improve the security of WSNs by providing a shield to the cluster heads of the network using machine learning techniques. The experimental study of the paper includes the comparative analysis of three machine learning techniques decision tree classifier, Gaussian Naïve Bayes, and random forest classifier for predicting WSN attacks like flooding, gray hole, blackhole, and TDMA that are deployed to support the proposed WSN security framework on the attack dataset. The random forest classifier achieves an accuracy of 98%, Precision of 97.6%, Recall of 97.6%, and F1 score of 97.8% which is the maximum among the deployed machine learning techniques.