Hello sir, let me explain how I will do that,
First I will read all the posts from the database by PHP with $wpdb. Then I will foreach all posts and extract the first image from post content and set the image to the post with set_post_thumbnail(). That's it.
Hopefully, you understand the process. Feel free to knock me if you have any question.
Thank you, sir,
Have a nice day