I'm writing a PHP script to search for a few lines in a pcap file. This pcap file will be piped through tail -> PHP.
I need to find a few lines like (Host: www.google.com) or (Domain: amazon.com) etc..
I'm new with PHP and struggling to get this code working, the actual output of all the fetched data need to be inserted into a SQL DB. I've used regex to filter out the binary stuff from the pcap.
I've tried multiple loops like the wile, foreach, for, but I'm not getting the clue how to do this in my script.
The code that I have so far is:
<?php
$handle = fopen('php://stdin', 'r');
$line = fgets ($handle, 1000);
$search1 = 'Location';
$search2 = 'Host:';
$search3 = 'User';
$search4 = 'Cookie';
$search5 = 'Domain:';
$matches = array();
$regex = '/[^a-zA-Z0-9\s\D\#$%^&*()+=\-\[\]\';,.\/{}|":<>?~\\\\]/';
if ($handle){
while ($handle) {
$buffer = fgets($handle);
if(strpos($buffer, $search1) !== FALSE) {
$res = preg_replace($regex, "", $buffer);
$matches[] = $res;
print_r($res). "\n";
}
}
fclose($handle);
}
?>
I've read many posts on the internet, but couldn't find any solution or I've not enough knowledge about PHP to get this done. Can anyone help me with this?
If it's working for first then loop it think about algorithm always
$handle = fopen('php://stdin', 'r');
$line = fgets ($handle, 1000);
$search = ['Location','Host:','User','Cookie','Domain:'];
$matches = array();
$regex = '/[^a-zA-Z0-9\s\D\#$%^&*()+=\-\[\]\';,.\/{}|":<>?~\\\\]/';
if ($handle){
while ($handle) {
$buffer = fgets($handle);
foreach($search as $seek){
if(strpos($buffer, $seek) !== FALSE) {
$res = preg_replace($regex, "", $buffer);
$matches[] = $res;
print_r($res). "\n";
}
}
}
fclose($handle);
}
?>
Related
I want to detect if a word in a text file exists, and then remove it.. so, this is my code:
<?php
$search = $id;
$lines = file("./user/".$_GET['own'].".txt");
// Store true when the text is found
$found = false;
foreach($lines as $line)
{
if(strpos($line, $search) !== false)
{
$found = true;
// open to read and modify
$file = "./user/".$_GET['own'].".txt";
$fh = fopen($file, 'r+');
$data = fread($fh, filesize($file));
$new_data = str_replace($id."\n", "", $data);
fclose($fh);
// Open to write
$fh = fopen($file, 'r+');
fwrite($fh, $new_data);
fclose($fh);
$status = "has been successfully deleted.";
}
}
// If the text was not found, show a message
if(!$found)
{
$status = "is not exist in your list.";
}
?>
I got this work hours before.. I did some changes to my script and somehow, it didnt work anymore.. can anyone see through the code and tell me what is wrong??
or can anybody give simpler way to do what I want?? my code is messed..
I want to detect if a word in a text file exists, and then remove it..
<?php
$search = $id;
$filename="./user/".$_GET['own'].".txt";
$contents = file_get_contents($filename);
$contents = str_replace($id."\n", "", $contents);
file_put_contents($filename,$contents);
?>
That is all that there is to it.
Edit:
To make this equivalent to your solution, one can use
<?php
$search = $id;
$filename="./user/".$_GET['own'].".txt";
$contents = file_get_contents($filename);
$contents = str_replace($id."\n", "", $contents,$count);
if($count>0)
{
file_put_contents($filename,$contents);
echo "found and removed";
}
else
{
echo "not found";
}
?>
I have a txt file which contains domains and ips looks like this
aaa.bbb.com 8.8.8.8
bbb.com 2.2.2.2
...
...
..
How do I replace bbb.com to 3.3.3.3 but do not change aaa.bbb.com?
Here is part of my function, but not working at all.
First part I search for the match domain by reading it line by line from file
after I got the matched record ,delete it.
Second part I write a new line into it.
$filename = "record.txt";
$lines = file($filename);
foreach($lines as $line)
if(!strstr($line, "bbb.com") //I think here is the problem core
$out .= $line;
$f = fopen($filename, "w");
fwrite($f, $out);
fclose($f);
$myFile = "record.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "bbb.com\n 3.3.3.3\n";
fwrite($fh, $stringData);
fclose($fh);
after I execute my code, both aaa.bbb.com and bbb.com were deleted, how can I solve this issue?I've try "parse_url" but "parse_url" only parse url with "http://" prefix instead of a domain.
Well, sorry for the misunderstanding, this should work:
<?php
$file = "record.txt";
$search = "bbb.com";
$replace = "3.3.3.3";
$open = file_get_contents($file);
$lines = explode(PHP_EOL, $open);
$dump = "";
foreach($lines as $line){
$pos = strpos($line, $search);
if($pos === false){
echo "<b>$line</b>";
$dump .= $line.PHP_EOL;
}else{
if($pos !== 0){
$dump .= $line.PHP_EOL;
}else{
$dump .= $search." ".$replace.PHP_EOL;
}
}
}
$dump = substr($dump,0,-1);
file_put_contents($file, $dump);
?>
The easiest solution I can think of is to use substr($line,0,7) == 'bbb.com' instead of your strstr comparison.
I'm new to learning php and in one of my first programs I wanted to make a basic php website with login capabilities with and array of the user and passwd.
my idea is to store the username as a list parameter and have the passwd as the contents, like this:
arr = array(username => passwd, user => passwd);
now my problem is that I don't know how I can read from the file (data.txt) so I can add it into the array.
data.txt sample:
username passwd
anotherUSer passwd
I've opened the file with fopen and stored it in $data.
You can use the file() function.
foreach(file("data.txt") as $line) {
// do stuff here
}
Modify this PHP example (taken from the official PHP site... always check first!):
$handle = #fopen("/path/to/yourfile.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
to:
$lines = array();
$handle = #fopen("/path/to/yourfile.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
lines[] = $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
// add code to loop through $lines array and do the math...
Be aware that you should not store login details in a textfile that in addition is not encrypted, this approach has severe security issues.
I know you are new from PHP, but the best approach is to store it in a DB and crypting the passwords with an algorithm such as MD5 or SHA1,
You shouldn't store sensitive information as plaintext, but to answer your question,
$txt_file = file_get_contents('data.txt'); //Get the file
$rows = explode("\n", $txt_file); //Split the file by each line
foreach ($rows as $row) {
$users = explode(" ", $row); //Split the line by a space, which is the seperator between username and password
$username = $users[0];
$password = $users[1];
}
Take a look at this thread.
This works for extremely large files as well:
$handle = #fopen("data.txt", "r");
if ($handle) {
while (!feof($handle)) {
$line = stream_get_line($handle, 1000000, "\n");
//Do Stuff Here.
}
fclose($handle);
}
Use file() or file_get_contents() to create either an array or a string.
process the file contents as needed
// Put everything in the file in an array
$aArray = file('file.txt', FILE_IGNORE_NEW_LINES);
// Iterate throug the array
foreach ($aArray as $sLine) {
// split username an password
$aData = explode(" ", $sLine);
// Do something with the username and password
$sName = $aData[0];
$sPass = $aData[1];
}
I am using fopen to reach my PHP file :
$readFd = #fopen($file, 'r+');
I would like to search this file for the function call parent::process();
And if this exists I would then insert a new function call after this.
I have tried using preg_replace but it does not seem to match parent::process();
For example the result I need is this.
public function process() {
parent::process();
$this->newFunction();
}
Then to write the to the file I am using :
fwrite($readFd, $content);
I guess I must be missing something important with regex.
Hopefully someone can point me in the right direction.
I would use the php function fgets to read every in the file one by one until you reach the line you need. And then your pointer will be after that line where you can write your own line.
EDIT
I was wrong, when you write something to a file at a specific point, everything after that point is lost. So I did a little testing and came up with this:
$handle = fopen($file,"r+");
$lines = array();
while(($line = fgets($handle)) !== false) {
$lines[] = $line;
if(strpos($line, 'parent::process()')) {
$lines[] = '$this->newFunction();';
}
}
fseek($handle, 0); // reset pointer
foreach($lines as $line) {
fwrite($handle, $line);
}
fclose($handle);
I hope this solves your problem.
I came up with the solution however your code seems much shorter so I will try your solution tomorrow.
if(! $readFd = #fopen($file, "r+"))
return FALSE;
$buffer = fread($readFd, 120000);
fclose($readFd);
$onDuplicate = FALSE;
$lines = explode("\n", $buffer);
foreach($lines AS $key => $line) {
if(strpos($line, "newFunction()")) {
$onDuplicate = TRUE;
}
if(strpos($line, "parent::process()")) {
$lines[$key] = "\t\tparent::process();\n\t\t//\$this->newFunction();\n";
}
}
if(! $onDuplicate) {
$readFd = fopen($file, "w");
$buffer = implode("\n", $lines)."\n";
fwrite($readFd, $buffer);
fclose($readFd);
} else {
var_dump('changes are already applied');
}
Thanks for all your help!
This code opens a text file, then checks to see if each word in the text file
Exists in a another large 2MB dictionary file.
If it does exist, it stores the line from the dictionary file into a variable.
The code was working, but then began to generate Server 500 errors, and now
It only lists about 7 matches and then loads nothing forever.
It used to list the 1000's of matches and then stop.
$file_handle = fopen("POSdump.txt", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
$words= explode(" ", $line );
foreach ($words as $word) {
$word = preg_replace('#[^\w+>\s\':-]#', ' ', $word);
$subwords= explode(" ", $word );
$rawword = $subwords[0];
$poscode = $subwords[1];
$rawword = strtoupper($rawword);
$handle = fopen("dictionary.txt","r"); //
if ($handle) {
while (!feof($handle))
{
$buffer = fgets($handle, 4096);
if (preg_match('#\b'.$rawword.'\b#',$buffer)) {
echo $rawword;
echo "</br>";
}
}
}
}
}
?>
Try closing the file when you are done.
This seems to be a memory_limit error. use ini_set('memory_limit', -1) before starting the process.