Yes dogs are able to know when they are in trouble.
Dogs may not know the difference between right and wrong but dogs can tell when they did something wrong though and are in trouble and have displeased their humans.
That's why discipline works with dogs when you tell them that what they did was a bad thing and you scold them then the dog eventually learns that if they do that thing they did it is wrong and punishment will come.
So the dog eventually learns that they should not do those things that are wrong that causes them to be disciplined.
A dog will also sometimes look away from your or go and hide when they get into trouble and are scolded.
That shows they know they are in trouble and want to hide away from you.
Just like kids will sometimes run and hide when they are about to get into trouble or get a spanking.
I used to go hide under the bed or hide in my closet and shut the door when my parents were gonna spank me.
I knew I was in trouble and went and hid.