Is there any difference between these two questions? Is the second one grammatically right?
How do I know?
How am I to know?
Dan Bron's opening comment is correct, but I'd like to explain a bit more about why that's the case.
Basically, it comes down to tense and modality, or conditionality. Using "to be" (or in this case, "am") with an infinitive like "to know" is one of the future tenses English uses*, and it's a tense that expresses intention and plan. So you could rephrase "How am I to know?" as "What plan would you expect me to use to know this?"
Since that's a fairly common sort of thing to say, but "How am I to …" is a fairly formal pattern, it's not surprising that "How do I …?" has been adapted to that use as well informally. Formally, though, "do" is not conditional at all here. It's just asking for a explanation that matches current reality. So you could rephrase it as "Right here, right now, how is it that I know this?"
Obviously, both sentences (and both verbs) are very flexible, so there's a lot of variations possible, and a lot of different subtle implications depending on context. But that's the basic breakdown.
*To the extent that English has future tense, at least, which is the subject of some scholarly debate. Close enough, though.
Let's say you are solving a math problem You are stuck somewhere for hours and after hours of struggle you came to know the reason, that is you were unaware of a formula which happens to be out of scope for you. At that point you can say "how am i to know?" According to me, "How do I know" is more like a statement Let's say after hours of struggle you went for a drink and after returning you started again and you solved it. (rare scenario) Next day you find the solved problem in your bed. You checked and find a strange formula which solved it. At that point you may say "how do I know" since you don't remember solving it.