In this paper, we consider the problem of solving a distributed (consensus-based) optimization problem in a network that contains regular and malicious nodes (agents). The regular nodes are performing a distributed iterative algorithm to solve their associated optimization problem, while the malicious nodes inject false data with a goal to steer the iterates to a point that serves their own interest. The problem consists of detecting and isolating the malicious agents, thus allowing the regular nodes to solve their optimization problem. We propose a method to dwarf data injection attacks on distributed optimization algorithms, which is based on the idea that the malicious nodes (individually or in collaboration) tend to give themselves away when broadcasting messages with the intention to drive the consensus value away from the optimal point for the regular nodes in the network. In particular, we provide a new gradient-based metric to detect the neighbors that are likely to be malicious. We also provide some simulation results demonstrating the performance of the proposed approach.