Background: Routing in opportunistic networks is subject to attacks such as blackhole and grayhole attacks. A blackhole attack is a type of attack where the malicious node called blackhole node attracts all the data packets by using a forged route to falsely claim that it has a shortest route to the destination, then drops all the data packets that it receives. On the other hand, a greyhole attack is a kind of denial of service attack where a malicious node in the network acts as a normal node, causing eaves dropping and selective forwarding. Blackhole and greyhole attacks in opportunistic networks have been extensively studied in the literature and several defence mechanisms against those attacks have been proposed. This paper adds a novel contribution to this problem by proposing a novel secured protocol (so-called SHBPR) against such attacks.Objective: The objective of this paper is to propose a novel design of a secure routing protocol to defend against blackhole and greyhole attacks in opportunistic networks. Method: The proposed method relies on our recently proposed History Based Routing scheme for Opportunistic Networks (HBPR), which uses the behavioral information of the nodes to make predictions about their movements in the network. This predicted information is used to find and select the best next hop node that can contribute to route the message to the destination based on an utility metric. The protection against blackhole and grayhole attacks is achieved by means of two phases, namely, the training phase and testing phase. In the training phase, all the nodes show normal behavior, i.e. there is no blackhole or greyhole node present in the network. At the end of this phase, each node calculates its average forwarding time and floods it in the network. In the testing phase, blackhole and greyhole nodes are introduced and their detection depends on the trust factors and deviation from the ideal forwarding time respectively. Results: The performance of SHBPR is evaluated using the Opportunistic Network Environment (ONE) simulator, under varying the number of nodes, varying the number of malicious nodes, and varying the message generation interval, considering the number of messages delivered, the number of messages dropped, the average detection time, and the number of false positives as performance metrics. The results obtained show that our proposed SHBPR outperforms the HBPR protocol (chosen as benchmark) in terms of messages delivered, messages dropped, number of malicious nodes detected, and average detection time. In addition, the proposed protocol is effective even when the number of messages propagated in the network is reduced. Conclusion: In this paper, a secure HBPR protocol (called SHBPR) is introduced, which can be used to mitigate blackhole and greyholes attacks in opportunistic networks. A trust factor and a deviation from the average forwarding time of nodes are used as patterns to detect the blackholes and greyholes respectively.