Insurance is regulated at the state rather than the federal level in the United States, and the terms of individual insurance contracts also matter.
Most states prohibit insurance of intentional unlawful acts as a matter of public policy, either under common law doctrines or under express statutes or regulations. The public policy is that no one should be insulated from liability for their own intentional acts. And, in insurance policies for individuals who are not employers, this is typically accomplished with an intentional acts exclusion in the language of the policy.
(Negligent acts are almost always insurable. The status of acts that constitute gross negligence, willful and wanton conduct, reckless conduct, or bad faith conduct, varies somewhat from state to state.)
But, the situation is muddier when the insured is an employer or an entity. It frequently is possible for an employer or entity to insure itself against unauthorized misconduct by employees or officers or directors who are breaching their duties to the employer or company, rather than carrying out the official policies of the company.
Factories don't knowingly do anything. People in or connected to factories do things and know things. And, if a factory employee is cutting corners in violation of his job responsibilities and that hurts the company, that could be an insurable risk. But, if a factory employee is acting in accordance with the directions of the CEO and Board of Directors in causing the factory to emit waste violating the law, and if the people giving him orders known that this violates the law, then this is probably not an insurable loss of the company as a matter of public policy.
Another complication involves the question of who is insured. The Town of Greenfield may be able to legally buy insurance that compensates it for the costs of having to deal with intentional environmental law violations by factories undertaken as a matter of corporate policy in the town, even though the corporation couldn't buy this insurance. Indeed, the Town could even impose a tax on the corporation that requires it to pay the premium on the Town's insurance policy. But, this wouldn't relieve the corporation of liability or violate public policy because the insurance company would have a right of "subrogation" to sue to owner of the factory to reimburse it for the full amount of any claims that it had to pay to the Town. This is true even if the corporation and the responsible individuals in the corporation are, in fact, judgment proof or bankrupt, so that the subrogation right is actually meaningless in fact.
It is also probably legal for a company to "self-insure", i.e. to set aside reserves of its own money to pay for it violations of the law in the future without regard to intent, and to have administrators employed to process payment in those situations, because then it is not actually insulated from harm for its own wrongdoing. But, this is risky because a policy of condoning illegal conduct could subject the company to punitive damages and sanctions in addition to compensatory damages.
It also matters what kind of harm the insurance policy pays for. It would not be illegal, in almost any state, to issue an insurance policy that pays for a criminal defense lawyer for someone who is accused of a crime. But, it would be illegal in almost every state to issue an insurance policy that pays for fines and court costs and restitution imposed as punishment for committing a crime. Paying for a criminal defense lawyer, or a bail bond, at a time when you are presumed innocent, is not a form of punishment and hence is an insurable expense.