This paper studies the secure and accurate clock synchronization problem for sensor net-works with time-varying delays and malicious attacks.A novel clock synchronization scheme based on the attack detection mechanism,attack compensation,and maximum consensus protocol is proposed.The proposed scheme starts with the detection of the malicious attacks and the clock data under at-tacks is eliminated.On the basis,software clock parameters are updated so that all the nodes in the network can have the same software skew and offset,so the clock synchronization can be achieved.Furthermore,it is theoretically proved that the proposed scheme can achieve the attack detection cor-rectly,and further can guarantee a secure and accurate clock synchronization.In addition,extensive simulations are also conducted to validate the effectiveness of the proposed scheme.