Extractability for Simulator in Malicious Model It is sometimes required while proving the security of a protocol in the malicious setting that the simulator is able to extract the witness set of the adversary, who tends to be the prover of possessing some knowledge, in the ZKP step of the protocol.

I was having issues understanding how the simulator would be able to extract the witness set without the discretion of the adversary?
If the simulator is able to do so, wouldn't that defeat the purpose that the ZKP is zero knowledge, at least to the simulator?
I also read that the witness extraction by simulator doesn't necessarily have to be probabilistic polynomial time. Can someone please elaborate on this?
Let me try. First, one thing needs to be clear: simulation and real execution are two different things. The simulator can learn something does not mean a verifier in real execution can learn the same thing. Thus the fact that a simulator can extract knowledge does not affect the protocol’s zero-knowledge property (which is about the verifier in the real execution). 

Why the simulator (or more accurately the knowledge extractor) can extract the knowledge but the verifier cannot? This is because the simulator has some special ability that the parties in the real world don’t have. One example is “rewinding”: the knowledge extractor can rewind the adversary to a previous state. If you are familiar with the Schnorr protocol, you should know rewinding is used to extract knowledge there. But can a real verifier rewind the prover? No.

Then why the knowledge extractor is given such an ability? It is for the sake of writing security proofs. A secure proof of knowledge protocol means that If the prover convinces the verifier, then it must know and use the witness in the protocol. This is hard to prove in fact. So we allow a knowledge extractor with extra ability to help us so that if the witness is used, the knowledge extractor can extract it from the transcript. Thus if the witness can be extracted, we can be certain the witness is used and  the protocol is proof of knowledge.