From Donatus’ Life of Vergil (Vita Vergiliana 71 and taken from a lost Suetonian vita):
Once when he [Virgil] held Ennius in his hand and was asked what he was doing, he replied that he was gathering gold from Ennius’ dung, for this poet has outstanding ideas buried under not very polished words.
cum is aliquando Ennium in manu haberet rogareturque quidnam faceret, respondit se aurum colligere de stercore Ennii. habet enim poeta ille egregias sententias sub verbis non multum ornatis.
Similarly reported in Cassiodorus (Inst. 1.1.8):
To whom [Origen] that too could conveniently be applied, namely what Virgil, while reading Ennius, answered when asked by someone what he was doing: “I am looking for gold in the dung.”
cui et illud convenienter aptari potest quod Vergilius, dum Ennium legeret, a quodam quid ageret inquisitus, respondit: aurum in stercore quaero.
The phrase seems to have become at least semi-proverbial in antiquity, though apparently without a crystallized form. In late antiquity it morphed into an occasional reference image for christians reading christian-heretical (or non-christian) works. The best instance in this vein is Saint Jerome’s famous letter 107, A Girl’s Education (De Institutione Filiae). Note the softening from stercus to lutum.
Let her avoid all the apocryphal books, and if she ever wishes to read them, not for the truth of their doctrines but out of respect for their wondrous tales, let her realize that they are not really written by those to whom they are ascribed, that there are many faulty elements in them, and that it requires great skill to look for gold in mud.
Caveat omnia apocrypha et, si quando ea non ad dogmatum veritatem, sed ad signorum reverentiam legere voluerit, sciat non eorum esse, quorum titulis praenotantur, multaque his admixta vitiosa et grandis esse prudentiae aurum in luto quaerere.
In another lesser known letter (98) that is rather a translation of correspondence sent to Jerome by Theophilus of Alexandria we find an interesting expanded variant:
Therefore those who delight in Origen’s errors should not despise the preaching of the Lord’s feast. Nor should they seek ointments, gold and pearls in the mire.
unde, qui Origenis erroribus delectantur, festivitatis dominicae non spernant praeconia nec unguenta, aurum et margaritas quaerant in luto.
Whether originating with Theophilus or reflecting a variant phrasing, I’ve found this version with pearls making a couple of later appearances. First is a letter of Marsilio Ficino’s from 1457 (in Kristeller’s Supplementum ad Ficinum II.82 but recalled from a footnote in Arthur Fields’ The Origins of the Platonic Academy of Florence):
You are happy in the midst of calamities. Fear does not make you lose heart; sadness excruciates not; pleasure does not corrupt nor desire inflame. In the thickest thorns you gather delicate and fair flowers, from dung you extract pearls, in the deepest darkness you see, impeded and held by chains you run like one who is free.
Felicem te puto . . . qui in mediis quoque calamitatibus sis beatus, quem nec metus exanimat nec dolor excruciat nec voluptas corrumpit nec libido inflammat, qui inter densissimas spinas molles ac candidos flores legas, qui ex putrido stercore margaritas eruas atque effodias, qui in profundis tenebris videas, qui compedibus gravatus et vinculis circumstrictus velut liber solutusque percurras.
Fields’ translation doesn’t follow Ficino’s exaggerated style in qualifying stercus as putridus (rotten, decayed) and doubling the verbal action with eruo (dig or pluck out) and effodio (dig out, unearth). There’s of course no way of knowing whether Ficino is here intentionally splicing the variants (with a possible added recollection of a phrase from Plautus’ Casina – ex sterculino effosse / dug from a dung-heap) or is recalling an earlier intermediary source that had already done the same.
A similar curious blend comes from a minor work of Blaise Pascal (Entretien avec M. de Saci sur Epictete et Montaigne – A Conversation with M. de Saci on Epictetus and Montaigne) where we see the aurum ex stercore version referenced as Jerome’s (presumably) and glossed with pearls (perles):
M. de Saci could not refrain from testifying to M. Pascal that he was surprised to see how well he knew how to interpret things; but he acknowledged at the same time that every one had not the secret of making on these readings such wise and elevated reflections. He told him that he was like those skilful physicians, who by an adroit method of preparing the most deadly poisons knew how to extract from them the most efficacious remedies. He added, that though he saw clearly, from what he had just said, that these readings were useful to him, he could not believe however that they would be advantageous to many people of slow intellect, who would not have elevation of mind enough to read these authors and judge of them, and to know how to draw pearls from the midst of the dunghill, aurum ex stercore, as said one of the Fathers. This could be much better said of these philosophers, the dunghill of whom, by its black fumes, might obscure the wavering faith of those who read them. For this reason he would always counsel such persons not to expose themselves lightly to these readings, for fear of being destroyed with these philosophers, and of becoming the prey of demons and the food of worms, according to the language of the Scripture, as these philosophers have been.
M. de Saci ne put s’empêcher de témoigner à M. Pascal qu’il était surpris comment il savait tourner les choses, mais il avoua en même temps que tout le monde n’avait pas le secret comme lui de faire des lectures des réflexions si sages et si élevées. Il lui dit qu’il ressemblait à ces médecins habiles qui, par la manière adroite de préparer les plus grands poisons, en savent tirer les plus grands remèdes. Il ajouta que, quoiqu’il vît bien, parce qu’il venait de lui dire, que ces lectures lui étaient utiles, il ne pouvait pas croire néanmoins qu’elles fussent avantageuses à beaucoup de gens dont l’esprit se traînerait un peu, et n’aurait pas assez d’élévation pour lire ces auteurs et en juger, et savoir tirer les perles du milieu du fumier aurum ex stercore, disait un Père. Ce qu’on pouvait bien plus dire de ces philosophes, dont le fumier, par sa noire fumée, pouvait obscurcir la foi chancelante de ceux qui les lisent. C’est pourquoi il conseillerait toujours à ces personnes de ne pas s’exposer légèrement à ces lectures, de peur de se perdre avec ces philosophes et de devenir l’objet des démons et la pâture des vers, selon le langage de l’Écriture, comme ces philosophes l’ont été.