That's a
really hard problem. In many cases there are subtle hints (color, the writing on the studs).
Spectrography might give a hint here due to the different formulation of the plastics but some of the knock-offs are now so good it can be very hard to tell them from the real thing.
I'm not really sure if 'counterfeit' is the right term, the companies selling these are not making pieces labeled 'lego', and in fact the Lego brand started out by copying an English product.
https://en.wikipedia.org/wiki/Kiddicraft
Damaged pieces and discolored pieces are also of interest and a very hard category to detect.