How to get a human-readable date/time format from Beacon packets - wireless

I want to fetch the human-readable date/time format from IEEE 802.11[a,b,g,n] wireless packets.
We have an open-source project for wireless pen-testing that called Aircrack-ng. This package has a tool that called Airodump-ng.
I found a function in Airodump-ng's source that can convert this timestamp to readable format.
source-code:
https://github.com/aircrack-ng/aircrack-ng/blob/master/src/airodump-ng.c#L3039
https://github.com/aircrack-ng/aircrack-ng/blob/master/src/airodump-ng.c#L3044
#define TSTP_SEC 1000000ULL /* It's a 1 MHz clock, so a million ticks per second! */
#define TSTP_MIN (TSTP_SEC * 60ULL)
#define TSTP_HOUR (TSTP_MIN * 60ULL)
#define TSTP_DAY (TSTP_HOUR
static char *parse_timestamp(unsigned long long timestamp) {
static char s[15];
unsigned long long rem;
unsigned int days, hours, mins, secs;
days = timestamp / TSTP_DAY;
rem = timestamp % TSTP_DAY;
hours = rem / TSTP_HOUR;
rem %= TSTP_HOUR;
mins = rem / TSTP_MIN;
rem %= TSTP_MIN;
secs = rem / TSTP_SEC;
snprintf(s, 14, "%3ud %02u:%02u:%02u", days, hours, mins, secs);
return s; }
In Airodump-ng, I saw below human-readable up-times for access-points:
ADSL-ADSL: 0d 01:04:08
ViroooS: 0d 18:13:10
Python2: 0d 12:50:40
G4_3355: 0d 00:07:34
apple: 4d 12:23:28
Maya: 8d 22:44:50
for example: the up-time of G4_3355 as an Access-Point is ~7 minutes.
for testing, i have a PCAP file and you can parse it with Wireshark.
download link of PCAP file: https://ufile.io/y0cca
a screenshot from Airodump-ng tool:
https://ufile.io/qpv5t
How we can write above function (C codes) in Python !?
the <bsstimestamp>183258624319</bsstimestamp> as input.
ts = 183258624319
result: a Date/Time readable format.
note: the format of timestamps in wireshark is not like as above TS. https://www.epochconverter.com/
Help me to convert the timestamps of this PCAP file to readable format like as above examples.
Thanks a lot.

Simple example:
from scapy.all import *
def print_timestamp(ts):
TSTP_SEC = 1000000
TSTP_MIN = TSTP_SEC * 60
TSTP_HOUR = TSTP_MIN * 60
TSTP_DAY = TSTP_HOUR * 24
days = ts / TSTP_DAY;
rem = ts % TSTP_DAY;
hours = rem / TSTP_HOUR;
rem %= TSTP_HOUR;
mins = rem / TSTP_MIN;
rem %= TSTP_MIN;
secs = rem / TSTP_SEC;
print '%3ud %02u:%02u:%02u'% (days, hours, mins, secs)
pkts = rdpcap('timestamp.cap')
for pkt in pkts:
if pkt.haslayer(Dot11Beacon) or pkt.haslayer(Dot11ProbeResp):
print_timestamp(pkt.timestamp)

Related

How to convert millisecond (uint64) into Time Format RFC3999 with millisecond (string) in GO

How do i convert millisecond (uint64) into Time Format RFC3999 with millisecond (string) in GO?
For example:
var milleSecond int64
milleSecond = 1645286399999 //My Local Time : Sat Feb 19 2022 23:59:59
var loc = time.FixedZone("UTC-4", -4*3600)
string1 := time.UnixMilli(end).In(loc).Format(time.RFC3339)
Actual Result: 2022-02-19T11:59:59-04:00
Expected Result(should be): 2022-02-19T11:59:59.999-04:00
You are asking for an RFC3339 formatted string, with seconds reported to the nearest millisecond. There's no format string in the time package for this (only with whole seconds and nanosecond accuracy), but you can make your own.
Here's the string for seconds to the nearest nanosecond, copied from the standard library:
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
You can make a millisecond version of this easily enough by removing the .999999999 (report time to the nearest nanosecond, removing trailing zeros) to .000 (report time to the nearest millisecond, don't remove trailing zeros). This format is documented under time.Layout in the package docs https://pkg.go.dev/time#pkg-constants:
RFC3339Milli = "2006-01-02T15:04:05.000Z07:00"
Code (playground link):
package main
import (
"fmt"
"time"
)
const RFC3339Milli = "2006-01-02T15:04:05.000Z07:00"
func main() {
ms := int64(1645286399999) //My Local Time : Sat Feb 19 2022 23:59:59
var loc = time.FixedZone("UTC-4", -4*3600)
fmt.Println(time.UnixMilli(ms).In(loc).Format(RFC3339Milli))
}
Output:
2022-02-19T11:59:59.999-04:00

Stopwatch in Roblox Lua using a while loop

I am making a Roblox game and I want it to have a stopwatch. The stopwatch works, but it counts very slowly for some reason.
Here's my ScreenGui in StarterGui:
Here's the code inside the LocalScript:
local timer = script.Parent.Timer
local tms = 00
local ts = 00
local tm = 00
local tt
local tts
local y = 0
local whichtower = game.Players.LocalPlayer:FindFirstChild("WhichTower")
while true do
wait(0.01)
if whichtower.Value == "" then
tms = 00
ts = 00
tm = 00
tts = 0
else
tms = tms + 1
if tms == 100 then
ts = ts + 1
tms = 0
tts = tts + 1
if ts == 60 then
tm = tm + 1
ts = 0
end
end
tt = tostring(tm)..":"..tostring(ts)..":"..tostring(tms)
timer.Text = tt
game.Players.LocalPlayer:FindFirstChild("Time").Value = tt
end
end
The arbitrary wait() and loop is a possible cause of timing issues, although I can't see anything specific that might be slowing it down. Are you sure that :FindFirstChild on WHichTower is always returning results? Add a print statement there, so your debug window has a constant stream of values, and you can confirm if it's finding a suitable tower.
Also, you are only updating the text if there's a Tower; for the code where you set the values to 0, there's no timer.Text update.
But if you don't think that's the issue:
I'd try put your code in a Heartbeat function, called regularly and tied to the refresh rate (I think). Then you don't need the while loop, nor the wait() commands. The Heartbeat only runs as fast as the refresh rate, therefore, there's no point trying to running anything quicker than that because the screen won't update.
local lPlayers = game:GetService("Players")
local lRunSvc = game:GetService("RunService")
local function onPlayerAdded(pPlayer) -- pPlayer (variable name is up to you) is the ref to the joined player.
print(pPlayer.Name .. " joined the game.")
lRunSvc.Heartbeat:Connect(function()
print("whichtower.value is:" .. whichtower.Value) -- View prints in the Output console
if whichtower.Value == "" then
tms = 00
ts = 00
tm = 00
tts = 0
else
tms = tms + 1
if tms == 100 then
ts = ts + 1
tms = 0
tts = tts + 1
if ts == 60 then
tm = tm + 1
ts = 0
end
end
tt = tostring(tm)..":"..tostring(ts)..":"..tostring(tms)
timer.Text = tt
game.Players.LocalPlayer:FindFirstChild("Time").Value = tt
end
end)
end
lPlayers.PlayerAdded:Connect(onPlayerAdded) -- This is called when a player joins
Just as Vexen Crabtree has pointed out, the time that wait() will actually pause a script is based on a system clock's best guess of how much time has passed. Rather than counting up the milliseconds, a more reliable method for calculating passed time is to use the tick() function.
tick() will give you the current epoch time, which is the number of milliseconds that have passed since January 1st, 1970.
So, if you know a starting time, you can subtract it from the current time and get the number of milliseconds that have passed. This method doesn't rely on loop timings and will give a more accurate measure of the actual time that has passed. The amount of time that you chose with wait() will only reflect the speed at which the value will be updated.
local timer = script.Parent.Timer
local whichtower = game.Players.LocalPlayer:FindFirstChild("WhichTower")
local playerTime = game.Players.LocalPlayer:FindFirstChild("Time")
local startingTime = 0
local isTiming = false
-- attach a listener to know when to start the clock
whichTower.Changed:Connect(function(newValue)
-- reset the clock to zero when the value is empty
isTiming = newValue ~= ""
if not isTiming then
startingTime = 0
else
-- start the clock!
startingTime = tick()
end
end)
local refreshTime = 0.01
while true do
wait(refreshTime)
if isTiming then
-- calculate the time that has passed
local ms = tick() - startingTime
-- calculate how many minutes have passed
local tm = ms - (ms % (60 * 1000))
ms = ms - tm
tm = tm / 1000
-- calculate how many seconds have passed
local ts = ms - (ms % 1000)
ms = ms - ts
ts = ts / 1000
-- format the remainder
local tms = ms / 1000
if #tostring(tms) > 2 then
tms = string.sub(tostring(tms), 3)
else
tms = "0"
end
-- format the time into mm:ss.ss ex) 123:01.123
local tt = string.format("%.2d:%.2d.%s", tm, ts, tms)
timer.Text = tt
playerTime.Value = tt
end
end
I have figured it out using a different set of code. Roblox limits the wait parameters to a minimum of 0.03 seconds, so that is why my previous code was not working.

stoll function(c++11 string) giving ambiguous output

I encountered this problem :
https://www.urionlinejudge.com.br/judge/en/problems/view/1193
/*input
3
101 bin
101 dec
8f hex
*/
/*************************************************************
* Purpose : https://www.urionlinejudge.com.br/judge/en/problems/view/1193
* Author: Sahil Arora
* Version: 1.0
* Date: 22/10/15
* Bugs : None
*************************************************************/
#include<bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
/* code */
std::ios_base::sync_with_stdio(false);
int test;
cin>>test;
for(int i=1;i<=test;++i){
cout<<"Case "<<i<<":\n";
string str,base;
long long num,j;
cin>>str>>base;
if(base=="dec"){
num = stoll(str,nullptr);
cout<<hex<<num<<" hex\n";
bitset<32> bin(num);
for(j=31;bin[j]==0 && j>=0;--j)
;
while(j>=0)
cout<<bin[j--];
cout<<" bin\n\n";
}
else if(base=="hex"){
str = "0x" + str;
num = stoll(str,nullptr,16);
cout<<dec<<num<<" dec\n"; // <--focus on this line
bitset<32> bin(num);
for(j=31;bin[j]==0 && j>=0;--j)
;
while(j>=0)
cout<<bin[j--];
cout<<" bin\n\n";
}
else{
num = stoll(str,nullptr,2);
cout<<dec<<num<<" dec\n"<<hex<<num<<" hex\n\n";
}
}
return 0;
}
Now on changing line 45 to :
cout<<num<<" dec\n";
My output for hex changes. It gives the same output as input for hex to dec.I fail to understand why it gives such an error. Also, if I enter only 1 test case, it gives correct output for a hex, but still gives 20% Wrong answer on submission!
Input :
3
101 bin
101 dec
8f hex
Expected output :
Case 1:
5 dec
5 hex
Case 2:
65 hex
1100101 bin
Case 3:
143 dec
10001111 bin
My Output without using dec in cout :
Case 1:
5 dec
5 hex
Case 2:
65 hex
1100101 bin
Case 3:
8f dec
10001111 bin
I believe your program behavior is expectional and has nothing to do with stoll itself.
So, std::hex, std::dec (and std::oct) are manipulators that modify output base. Once applied they will not reset (this changes them from std::setw for example). That is why you should reapply manipulators every time you want to change output base.
So you just have to write
std::cout<<std::dec<<num<<" dec\n";
because you may have applied
std::cout<<std::hex<<num<<" hex\n";
earlier

Kissfftr different results x86 - Atheros AR9331

This is my first question on stackoverflow and my englsich is unfortunately poor. But I want to try it.
A customized routine of twotonetest of kissfft brings on two different systems very different results.
The under ubuntu translated with gcc on x86 program brings the correct values. That with the openWRT SDK translated for the Arduino YUN (Atheros AR9331) program displays incorrect values​​. It seems as if since the definition of FIXED_POINT is ignored.
Defined is:
#define FIXED_POINT 32
the function:
double GetFreqBuf( tBuf * io_pBuf, int nfft)
{
kiss_fftr_cfg cfg = NULL;
kiss_fft_cpx *kout = NULL;
kiss_fft_scalar *tbuf = NULL;
uint32_t ptr;
int i;
double sigpow=0;
double noisepow=0;
long maxrange = SHRT_MAX;
cfg = kiss_fftr_alloc(nfft , 0, NULL, NULL);
tbuf = KISS_FFT_MALLOC(nfft * sizeof(kiss_fft_scalar));
kout = KISS_FFT_MALLOC(nfft * sizeof(kiss_fft_cpx));
/* generate the array from samples*/
for (i = 0; i < nfft; i++) {
//nur einen Kanal, eine Krücke, würde nun auch mit 2 kanälen gehen, aber so ist schneller
if (io_pBuf->IndexNextValue >= (i*2))
ptr = io_pBuf->IndexNextValue - (i*2);
else
ptr = io_pBuf->bufSize - ((i*2) - io_pBuf->IndexNextValue);
tbuf[i] = io_pBuf->aData[ptr] ;
}
kiss_fftr(cfg, tbuf, kout);
for (i=0;i < (nfft/2+1);++i) {
double tmpr = (double)kout[i].r / (double)maxrange;
double tmpi = (double)kout[i].i / (double)maxrange;
double mag2 = tmpr*tmpr + tmpi*tmpi;
if (i!=0 && i!= nfft/2)
mag2 *= 2; /* all bins except DC and Nyquist have symmetric counterparts implied*/
/* if there is power between the frq's, it is signal, otherwise noise*/
if ( i > nfft/96 && i < nfft/32 )
noisepow += mag2;
else
sigpow += mag2;
}
kiss_fft_cleanup();
//printf("TEST %d Werte, noisepow: %f sigpow: %f noise # %fdB\n",nfft,noisepow,sigpow,10*log10(noisepow/sigpow +1e-30) );
free(cfg);
free(tbuf);
free(kout);
return 10*log10(noisepow/sigpow +1e-30);
}
As input samples of 16-bit sound from the same file be used. Results differ for example from-3dB to-15dB. AWhere could you start troubleshooting?
Possibility #1 (most likely)
You are compiling kissfft.c or kiss_fftr.c differently than the calling code. This happens to a lot of people.
An easy way to force the same FIXED_POINT is to edit the kiss_fft.h directly. Another option: verify with some printf debugging. i.e. place the following in various places:
printf( __FILE__ " sees sizeof(kiss_fft_scalar)=%d\n" , sizeof(kiss_fft_scalar) )
Possibility #2
Perhaps the FIXED_POINT=16 code works but the FIXED_POINT=32 code does not because something is being handled incorrectly either inside kissfft or on the platform. The 32 bit fixed code relies on int64_t being implemented correctly.
Is that Atheros a 16 bit processor? I know kissfft has been used successfully on 16 bit platforms, but I'm not sure if FIXED_POINT=32 real FFTs on a 16 bit fixed point has been used.
viel Glück,
Mark

How to read large matrix from a csv efficiently in Octave

There are many reports of slow performance of Octave's dlmread. I was hoping that this was fixed in 3.2.4, but when I tried to load a csv file that has a size of ca. 8 * 4 mil (32 mil in total), it also took very, very long time. I searched the web but could not find a workaround for this. Does anybody know a good workaround?
I experienced the same problem and had R handy, so my solution was to use "read.csv" in R, and then use the R package "R.matlab" to write a ".mat" file, and then load that in Octave.
"read.csv" can be pretty slow too, but this worked very well in my case.
The reason is that Octave has a bug that adding data to a very large matrix takes more time then adding the same amount of data to a small matrix.
Below is my try. I choose to save data each 50000 lines, so meanwhile I could already take a look instead of being forced to wait. It is slower for small files, but much faster for larger files.
function alldata = load_data(filename)
fid = fopen(filename,'r');
s=0;
data=[];
alldata=[];
save "temp.mat" alldata;
if fid == -1
disp("Couldn't find file mydata");
else
while (~feof(fid))
line = fgetl(fid);
[t1,t2,t3,t4,d] = sscanf(line,'%i:%i:%i:%i %f', "C"); #reading time as hh:mm:ss:ms and data as float
s++;
t = (t1 * 3600000 + t2 * 60000 + t3 * 1000 + t4);
data = [data; t, d];
if (mod(s,10000) == 0)
#disp(s), disp(" "), disp(t), disp(" "), disp(d), disp("\n");
disp(s);
fflush(stdout);
end
if (mod(s,50000) == 0)
load "temp.mat";
alldata=[alldata; data];
data=[];
save "temp.mat" alldata;
disp("data saved");
fflush(stdout);
end
end
disp(s);
load "temp.mat";
alldata=[alldata; data];
save "temp.mat" alldata;
disp("data saved");
fflush(stdout);
end
fclose(fid);
Here is a workaround that I am using.
I did not find that sscanf will parse input lines as indicated above. Also, I didn't use the temp file.
My .csv file has a large number of rows. They begin with a header of 18 lines and are followed by a data block, each of which has 135 columns. The following code has been tested. My file also begins each row with a dd/mm/yyyy hh:mm field. This will also catch poor lines and indicate where they are by using try/catch.
My .csv file came from a customer who dumped his PARCView load in an Excel file.
function [tags,descr,alldata] = fbcsvread(filename)
fid = fopen(filename,'r');
s = 0;
data=[];
alldata=zeros(1,135);
if fid==-1
disp("Couldn't find file %s\n",filename);
else
linecount = 1;
while (~feof(fid))
line = fgetl(fid);
data2 = zeros(1,135);
if linecount == 1
tags = strsplit(line,",");
elseif linecount == 2
descr = strsplit(line,",");
elseif linecount >= 19
data = strsplit(line,",");
datetime = strsplit(char(data(1))," ");
modyyr = strsplit(char(datetime(1)),"/");
hrmin = strsplit(char(datetime(2)),":");
year1 = sscanf(char(modyyr(3)),"%d","C");
day1 = sscanf(char(modyyr(2)),"%d","C");
month1 = sscanf(char(modyyr(1)),"%d","C");
hour1 = sscanf(char(hrmin(1)),"%d","C");
minute1 = sscanf(char(hrmin(2)),"%d","C");
realtime = datenum(year1,month1,day1,hour1,minute1);
data2(1) = realtime;
for location = 2:134
try
data2(location) = sscanf(char(data(location)),"%f","C");
catch
printf("Error at %s %s\n",char(datetime(1)),char(datetime(2)) );
fflush(stdout);
end_try_catch
endfor
alldata(linecount-18,:) = data2;
if mod(linecount,50) == 0
printf(".");
fflush(stdout);
endif
endif
linecount = linecount + 1;
endwhile
fclose(fid);
endif
endfunction

Resources