The derivation of the Hanlon eponym is not definitely known, but a very similar remark ("You have attributed conditions to villainy that simply result from stupidity.") appears in "Logic of Empire", a classic 1941 SF story by Robert A. Heinlein, who calls it the `devil theory' of sociology. Heinlein's popularity in the hacker culture makes plausible the supposition that `Hanlon' is derived from `Heinlein' by phonetic corruption. A similar epigram has been attributed to William James, but Heinlein more probably got the idea from Alfred Korzybski and other practitioners of GeneralSemantics.
"Never ascribe to malice, that which can be explained by incompetence" is a quote from Napoleon.
It's also been attributed to Machievelli. Hanlon as a corruption of Heinlein sounds more plausible, but this stuff is basic folk wisdom that's no doubt been repeatedly thought up and independently expressed over centuries.