To make the rule setting more convenient, a self-learning method for the generation and optimization of industrial firewall rules was presented. According to packet capturing and analyzing, the data preprocessing was accomplished. The firewall rule set was generated by the self-learning module. The rule set was optimized to reduce the number of rules and shorten the packet matching length. Experimental and comparative results show that the proposed method reduces the packet matching time and has more obvious advantages with the number of rules increases.